-1

嗨,这是我的代码:

    import java.util.Scanner;

    public class Calc {

    public static void main(String[] args) {

       Scanner sc = new Scanner (System.in);
       int a,b;
       String rep="YES";

       while (rep.equals("YES")){
          System.out.println("Please enter your first number: ");
          a=sc.nextInt();
          System.out.println("Please enter you second number: ");
          b=sc.nextInt();
          int  c=a+b;
          System.out.println("The sum of a and b is: "+c);
          rep=" ";
          while (!rep.equals("YES") && !rep.equals("NO")){
             System.out.println("Would you retry ( YES/NO): ");        
             sc.nextLine();
             rep=sc.nextLine();
          }       
       }
       System.out.println("Thanks to use mY proggram!");          
}}

我在这段代码中的问题:当我运行这个应用程序时,首先:

请输入您的第一个号码:

45

请输入您的第二个号码:

45

a和b之和为:90

你会重试吗(是/否):

我写了一些像“dgdg”这样的东西

请输入您的第一个号码:

45

请输入您的第二个号码:

45

a和b之和为:90

你会重试吗(是/否):

dgdg

你会重试吗(是/否):

这次当我写“是”时,它出现了另一行写这样的东西:

请输入您的第一个号码:

45

请输入您的第二个号码:

45

a和b之和为:90

你会重试吗(是/否):

dgdg

你会重试吗(是/否):

是的

是的

请输入您的第一个号码:

希望找到解决办法

4

5 回答 5

1

试试这个(如果答案是 YES,则继续计算,否则对于 NO 或垃圾输入它会停止程序) - 也取消了 while 循环中的分支

public class Calc {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int a, b;
        String rep = "YES";

        while (rep.equals("YES")) {
            System.out.println("Please enter your first number: ");
            a = sc.nextInt();
            System.out.println("Please enter you second number: ");
            b = sc.nextInt();
            int c = a + b;
            System.out.println("The sum of a and b is: " + c);
            do {
                System.out.println("Would you retry ( YES/NO): ");
                rep = sc.next();
            } while(!("YES".equals(rep) || "NO".equals(rep)));
            if("NO".equals(rep)) {
                break;
            }
        }
        System.out.println("Thanks to use mY proggram!");
    }
}

我已经更新了答案,以便它继续用户输入“是”,停止用户输入“否”并不断提示任何其他用户输入。

于 2013-05-31T10:16:22.777 回答
0

代替

            sc.nextLine();
            rep = sc.nextLine();

           rep = sc.nextLine();

因为当你给 sc.nextLine() 两次时,它会期望两行。

于 2013-05-31T10:21:12.497 回答
0

一些事情:

  1. sc.nextInt()不会消耗新行标记,因此如果您使用rep = sc.nextLine();它将返回空字符串。为了摆脱这个问题,你可以使用Integer.parseInt(sc.nextLine),或者sc.nextLine()在每个之后添加sc.nextInt(),或者只是在你使用sc.next()时使用sc.nextLine()

  2. 可能是因为第 1 点你不得不使用

    sc.nextLine();
    rep = sc.nextLine();
    

    现在您可以先跳过sc.nextLine();并使用rep = sc.nextLine();

  3. 用于rep.equalsIgnoreCase("YES")也接受Yes, yes,yEs等等。

所以你的代码看起来像这样

Scanner sc = new Scanner(System.in);
int a, b;
String rep = "YES";

while (rep.equalsIgnoreCase("YES")) {
    System.out.println("Please enter your first number: ");
    a = sc.nextInt();

    System.out.println("Please enter you second number: ");
    b = sc.nextInt();

    int c = a + b;
    System.out.println("The sum of a and b is: " + c);
    rep = " ";

    while (!rep.equalsIgnoreCase("YES") && !rep.equalsIgnoreCase("NO")) {
        System.out.println("Would you retry (YES/NO): ");
        rep = sc.next();
    }
}
System.out.println("Thanks to use mY proggram!");
于 2013-05-31T10:26:36.483 回答
0

请更改代码以使用以下代码。

公共静态无效主要(字符串[]参数){

       Scanner sc = new Scanner (System.in);
       int a,b;
       String rep="YES";

       while (rep.equals("YES")){
          System.out.println("Please enter your first number: ");
          a=sc.nextInt();
          System.out.println("Please enter you second number: ");
          b=sc.nextInt();
          int  c=a+b;
          System.out.println("The sum of a and b is: "+c);
          rep="";
          while (!rep.equals("YES") && !rep.equals("NO")){
             System.out.println("Would you retry ( YES/NO): ");        
             rep=sc.next();
             System.out.println("Rep is : "+rep);
          }       
       }
       System.out.println("Thanks to use mY proggram!");          
}
于 2013-05-31T11:11:23.450 回答
0

谢谢大家!这

 i.nextLine();

不应置于此方法下方:

System.out.println("Would you retry ( YES/NO): ");    

它应该在第二个whil语句之后

这是正确的代码:

导入 java.util.Scanner;

public class Calc {

public static void main(String[] args) {

   Scanner sc = new Scanner (System.in);
   int a,b;
   String rep="YES";

   while (rep.equals("YES")){
      System.out.println("Please enter your first number: ");
      a=sc.nextInt();
      System.out.println("Please enter you second number: ");
      b=sc.nextInt();
      int  c=a+b;
      System.out.println("The sum of a and b is: "+c);
      rep=" ";
      sc.nextLine();
      while (!rep.equals("YES") && !rep.equals("NO")){
         System.out.println("Would you retry ( YES/NO): ");        

         rep=sc.nextLine();
      }       
   }


    System.out.println("Thanks to use mY proggram!");          
}}
于 2013-06-01T07:31:44.930 回答