【问题标题】:How to end this program when user inputs "end"?当用户输入“结束”时如何结束这个程序?
【发布时间】:2020-04-22 07:03:50
【问题描述】:

所以基本上我想弄清楚如何使用Integer.valueOf(String s)Integer.parseInt(String s) 通过为用户输入键入“end”来停止这个程序

import java.util.Scanner;

public class monkey1{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = 0;
        int lol = 1;
        do { 
            System.out.print("Enter a number: ");
            lol = s.nextInt();
            sum += lol;
            if (lol > 0) {
                System.out.println("Sum is now: " + sum);
            }
        } while (lol>0);
    }
}

【问题讨论】:

    标签: java if-statement while-loop


    【解决方案1】:

    首先 - 您需要将 lol=sc.nextInt() 更改为 String tmp = s.nextLine()

    第二个 - 做

    try {
       if (tmp.equals("END")) {
           break;  // This will exit do-while loop
       }
       lol = Integer.parseInt(tmp);
    } catch (NumberFormatException e) {
      e.printStackTrace();
      // Here you can also exit loop by adding break. Or just ask user to enter new text.
    }
    
    

    【讨论】:

    【解决方案2】:

    您应该学习如何使用 try/catch。 我们开始:

    String text = "";
        int lol = 1;
        do {
            System.out.print("Enter a number: ");
            text = s.nextLine();
            try {
                lol = Integer.valueOf(text);
            }catch (Exception e){
                System.out.println("Exit");
                System.exit(0);
            }
    

    【讨论】:

      【解决方案3】:

      这是你的代码

      package Phone;
      
      import java.util.Scanner;
      
      public class monkey1{
          public static void main(String[] args){
              Scanner s = new Scanner(System.in);
              int sum = 0;
              int hi = 1;
              do { 
                  System.out.print("Enter a number: ");
                  String lol = s.nextLine();
                  if(lol.equals("end")) {
                      break;
              }
      
              hi = Integer.parseInt(lol);
      
              sum += hi;
              if (hi > 0) {
                  System.out.println("Sum is now: " + sum);
              }
          } while (hi > 0);
      }
      

      我在这里所做的是我将 lol 更改为 hi 以添加并接受 String 输入,以便您可以输入 end...

      【讨论】:

      【解决方案4】:

      我建议在转换为 int 之前检查 if s.equals("end") 是这样的:

      do { 
              System.out.print("Enter a number: ");
              String userValue = s.nextLine();
              if(userValue.equals("end")
                  break;
              lol = Integer.parseInt(userValue);
              sum += lol;
              if (lol > 0){
                  System.out.println("Sum is now: " + sum);
              }
          }while (lol>0);
      

      【讨论】:

      • 请注意,如果用户输入的文本不是end 并且不是数字,则此示例将失败并显示NumberFormatException
      • 是的,重要提示@VladislavVarslavans,用户提供正确的输入是理所当然的。
      猜你喜欢
      • 1970-01-01
      • 2020-02-15
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2022-11-30
      • 2019-06-30
      相关资源
      最近更新 更多