【问题标题】:Looping in Java(Beginners)Java中的循环(初学者)
【发布时间】:2013-03-29 23:43:53
【问题描述】:

我必须在我的代码中使用循环,这样当有人输入“是”时,他们可以根据需要多次重新输入他们的姓名,但我不知道该怎么做。任何帮助表示赞赏,这是我的代码:

public static void main(String [] args)
  {
    // Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);

    //Get the user's name.
    System.out.print("What is your name?");
    String name = keyboard.nextLine();
    System.out.println("Hello there," + name);

    System.out.println("Would you like to enter another name? Please enter Yes Or No.");
    String reply = keyboard.nextLine();

    if (reply == "yes")
    {

    }
  }
}

【问题讨论】:

    标签: java loops if-statement boolean


    【解决方案1】:

    这个reply == "yes" 不是你在Java 中比较Strings 的方式。这比较的是内存位置,而不是内容(内存位置不太可能相等)。

    您需要改用reply.equals("yes"),或者如果您不关心进行案例比较,您可以改用reply.equalsIgnoreCase("yes")

    do {
        // The remainder of your code...
    } while (reply.equalsIgnoreCase("yes"));
    

    更新

    您可能还希望阅读 The while and do-while statementsThe for Statement,其中涵盖了 Java 循环的基础知识

    【讨论】:

    • 这不是 OP 所要求的。他们不是在寻求一种比较字符串的方法,而是在寻求一种重复循环的方法,直到用户要求停止。
    • @Telthien 而且,除非他们更正 String 比较,否则他们所做的任何事情都行不通
    • 即使纠正了一个侧面错误,仍然没有回答问题。
    • @Telthien 我仍然认为这是一个重大错误,大多数人似乎都在避免解决(有解释),但这只是我 ;)
    【解决方案2】:

    使用do-while 循环:

    public static void main(String[] args) {
    
            // Create a Scanner object to read input.
            Scanner keyboard = new Scanner(System.in);
            do {
                //Get the user's name.
                System.out.print("What is your name?");
                String name = keyboard.nextLine();
                System.out.println("Hello there," + name);
                System.out.println("Would you like to enter another name? Please enter Yes Or No.");
    
            } while (keyboard.nextLine().equalsIgnoreCase("yes"));
    
            System.out.println("Bye!");
            keyboard.close();
          }
        }
    

    【讨论】:

      【解决方案3】:

      使用这个

      import java.util.*;
      public class prob13 {
      public static void main(String [] args)
        {
          // Create a Scanner object to read input.
          Scanner keyboard = new Scanner(System.in);
      
          //Get the user's name.
          while(true){
              System.out.print("What is your name?");
              String name = keyboard.nextLine();
              System.out.println("Hello there," + name);
      
              System.out.println("Would you like to enter another name? Please enter Yes Or No.");
              String reply = keyboard.nextLine();
      
              if(reply.equals("no"))
                  break;
          }
       }
      }
      

      这样做的原因是只要答案不是“否”就循环。

      或者,如果您希望答案始终是肯定的,您可以使用它

      import java.util.*;
      public class prob13 {
      public static void main(String [] args)
        {
          // Create a Scanner object to read input.
          Scanner keyboard = new Scanner(System.in);
              String reply="yes";
          //Get the user's name.
          while(reply.equals("yes")){
              System.out.print("What is your name?");
              String name = keyboard.nextLine();
              System.out.println("Hello there," + name);
      
              System.out.println("Would you like to enter another name? Please enter Yes Or No.");
              reply = keyboard.nextLine();
      
          }
       }
      }
      

      【讨论】:

        【解决方案4】:

        我认为这会起作用(未经测试):

        public static Scanner keyboard = new Scanner(System.in); // global
        
            public static void main(String [] args)
            {
                getName();
            }
        
            public static void getName()
            {
                System.out.print("What is your name?");
                String name = keyboard.nextLine();
                System.out.println("Hello there," + name);
                rerun();
            }
        
            public static void rerun()
            {
                System.out.println("Would you like to enter another name? Please enter \"yes\" or \"no\".");
                String reply = keyboard.nextLine();
        
                if (reply.equals("yes")) getName();
                else System.exit();
            }
        
        }
        

        首先我们调用getName() 方法并运行一次。然后我们调用rerun() 方法。此方法将测试我们是否要重新运行程序。如果用户输入“是”,那么我们重复整个过程。如果我们输入“yes”以外的任何内容,程序就会退出。

        除了您的代码未完成这一事实之外,您的代码唯一真正的问题是您尝试使用== 运算符比较字符串。请参阅 MadProgrammer 的回答,了解为什么这是错误的。

        【讨论】:

        • 虽然我很欣赏你的回答的有效性,但也许你想花时间解释为什么你的答案是(更)正确的然后 OP 做了什么,OP 做错了什么以及什么您回答的总体好处是 - 恕我直言
        • 就个人而言,我会讨论为什么reply == "yes" 不好。方法调用的使用可以隔离责任并减少混乱,但您朝着正确的方向前进;)
        • 使用 System.exit 控制流并不是一个好主意(因为它会中止整个程序的执行,这通常是不希望的)。
        • @meriton 我的意思是中止程序的执行;如果 OP 想在那里做一些不同的事情,他们可以。
        【解决方案5】:

        最简单(也可能是最清晰)的方法是将要重复的内容包装在 do-while statement 中:

        public static void main(String [] args)
          {
            // Create a Scanner object to read input.
            Scanner keyboard = new Scanner(System.in);
        
            String reply;
            do {
                //Get the user's name.
                System.out.print("What is your name?");
                String name = keyboard.nextLine();
                System.out.println("Hello there," + name);
        
                System.out.println("Would you like to enter another name? Please enter Yes Or No.");
                reply = keyboard.nextLine();
        
            } while ("yes".equals(reply));
          }
        }
        

        reply 变量必须在块之前声明,因为它是在循环条件中访问的(一个变量只在它声明的块中可见,所以如果在循环中声明了reply,它将不可用循环条件)。

        我更改了循环条件,因为 == 运算符通过引用比较字符串,即它将检查双方是否指向同一个字符串对象。相反,equals 方法检查字符串的内容是否相等(即它们包含相同顺序的相同字符)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-04
          • 2013-03-05
          • 2016-04-14
          • 1970-01-01
          • 2013-06-16
          • 2012-11-19
          相关资源
          最近更新 更多