【问题标题】:method - keep going方法——继续
【发布时间】:2012-11-11 12:22:23
【问题描述】:
import java.util.Scanner;
public class InteractiveRectangle
{
public static void main(String[] args)
{
    do 
    { 
        int length = readInteger ("For Length ");
        System.out.println();
        int width = readInteger ("For Width ");
        printRectangleDetails(length,width);// existing code goes here 
    } 
    while (keepGoing()); 

    System.out.println("Goodbye, friend"); 
}


/**
 * returns the details of the rectangle
 * @param height the height of the rectangle
 * @param width the width of the rectangle
 */
public static void printRectangleDetails (int length, int width)
{
    System.out.println ("This is the length of the rectangle " + length);

    System.out.println ("This is the width of the rectangle " + width);

    System.out.println (("This is the perimeter of the rectangle " + (length + width)));

    System.out.println (("This is the area of the rectangle " + (length * width)));
}

/**
 * Read in an integer and return its value
 * @param the prompt to be shown to the user
 */
public static int readInteger(String prompt)
{
    System.out.println (prompt);
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer");

    while (!scan.hasNextInt()) // while non-integers are present
    {
        scan.next();
        System.out.println ("Bad input. Enter an integer.");
    }
    int input = scan.nextInt();
    return input;
}

/**
 * Read a positive integer and return its value
 * @param the prompt to be shown to the user
 */
public static int readPositiveInteger(String prompt)
{
    System.out.println (prompt);
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer");
    boolean positive = false;

    while (scan.hasNextInt() && positive == false)
    {
        int input = scan.nextInt();
        if (input > 0)
        {
            positive = true;
            {
                return input;
            }
        }
        else
        {
            System.out.println ("Bad input enter an integer.");
            positive = false;
            scan.nextLine();

        }

    }
    return 0;
}

/**
 * Ask the user whether or not to spawn another rectangle
 * and returns the result as a boolean
 */
public static boolean keepGoing()    
{
    Scanner scan = new Scanner(System.in);
    boolean inputRead = false;
    boolean result = false;
    System.out.println ("Do you want to process another rectangle?"); 
    scan.next();
    String input = scan.next();

    if (input == "y")
    {
        inputRead = true;
        result = true;

    }
    else if (input == "n")
    {
        inputRead = true;
        result = false;

    }
    else
    {
        System.out.println("Bad input please try again!");
        scan.nextLine();
    }
    return result;

}

}

我希望程序询问用户是否要生成另一个矩形,并继续运行直到用户用“n”回答这个问题。 Atm 运行程序时,矩形只生成一次,所以我认为我的 keepGoing 方法有问题。 任何帮助,将不胜感激, 谢谢!

【问题讨论】:

    标签: java loops methods


    【解决方案1】:

    if (input == "y")

    始终将字符串与equals() 进行比较

    你需要

    if ("y".equals(input))

    if ("y".equalsIgnoreCase(input)) // will also allow Y

    相应地更改其他检查。

    【讨论】:

      【解决方案2】:

      是的,有几个问题:-

      • 首先,您使用== 运算符比较字符串,这始终是错误的。使用equals方法:-

        if (input.equals("y"))   // or,   if (input.equalsIgnoreCase("y"))
        
      • 其次,你不应该使用scan.next() 方法,你正在使用的方式。您的第一个 scan.next 应分配给 input,因为您的第二个 scan.next 包含换行符:-

        System.out.println ("Do you want to process another rectangle?"); 
        // scan.next();  // Should not be here.
        String input = scan.next();
        scan.next();     // Change the order
        

        或者,只需使用scan.nextLine():-

        System.out.println ("Do you want to process another rectangle?"); 
        String input = scan.nextLine();
        
      • 第三,在您的else 部分,您可以再次调用您的keepGoing 方法,而不是在那里读取输入:-

        else
        {
            System.out.println("Bad input please try again!");
            return keepGoing();
        }
        
      • 另外,在您的if-else 中,您可以直接从那里返回,而不是将布尔值设置为变量。

      所以,总而言之,您可以将您的 if-else if-else 更改为:-

      if (input.equals("y")) {
          return true;
      }
      else if (input.equals("n")) {
          return false;
      }
      else
      {
          System.out.println("Bad input please try again!");
          return keepGoing();
      }
      

      然后,您不需要那些boolean 变量:-inputReadresult。只需删除它们。并从method 末尾删除return 语句。因为现在是unreachable code

      【讨论】:

      • 谢谢,你说的很有道理。然而有一件事,当它询问我是否要生成另一个矩形时,我必须输入“y”,然后再输入“y”才能工作,有什么想法吗?
      • 是的,因为您没有在输入中存储first scan.next()。见我的第二点。当您使用scan.next() 阅读时,第一个包含input,第二个scan.next() 将包含newline
      • @AndyGault.. 你最好使用scan.nextLine 来读取字符串输入。如果您将比较作为字符串。查看我编辑的帖子。
      【解决方案3】:

      总是用 .equals() 比较字符串

       if (input == "y")
      

      替换为

       if (input.equals("y"))
      

      【讨论】:

        【解决方案4】:

        您正在使用== 运算符检查两个字符串是否相等。始终使用 equals 方法检查字符串是否相等。

        if (input == "y")
        

        应该是

        if (input.equals("y"))
        

        在其他地方也是如此,

        == 运算符检查两个字符串引用是否指向同一个字符串对象。 equals 方法确定两个 String 对象是否有意义地相等。

        【讨论】:

          【解决方案5】:

          这段代码:

          if (input.equals("y"))
          {
              inputRead = true;
              result = true;
          
          }
          else if (input.equals("n"))
          {
              inputRead = true;
              result = false;
          
          }
          

          应该能解决问题。请记住,Java 中的对象是通过引用而不是值进行比较的。

          【讨论】:

            猜你喜欢
            • 2012-01-31
            • 2011-12-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-23
            • 2013-02-09
            相关资源
            最近更新 更多