【问题标题】:Java output errorJava 输出错误
【发布时间】:2012-12-27 19:17:37
【问题描述】:

我无法让它正常工作。它按预期运行,并进行数学运算,但随后循环一次,然后结束。我需要它循环直到用户决定结束它,或者只运行一次。

import java.util.Scanner;

public class java {
  public static void main(String args[]) {
    System.out.println("Welcome to the simple Calculator program");
    System.out.println("Please type what type of math you would like to do: ");
    System.out.println("1=Addition");
    System.out.println("2=Subtraction");
    System.out.println("3=Multiplication");
    System.out.println("4=Division");
    System.out.println("5=Sqrt");
    Scanner input = new Scanner(System.in);
    int math = input.nextInt();
    if (math == 1) {
      Scanner a = new Scanner(System.in);
      int a1;
      int a2;
      int asum;
      System.out.print("Please enter the first number:  ");
      a1 = a.nextInt();

      System.out.print("Please enter the second number:  ");
      a2 = a.nextInt();

      asum = a2 + a1;
      System.out.print("The sum is: " + asum + "Thank You for using this program");
    }

    Scanner number = new Scanner(System.in);
    int number1;
    int number2;
    int sum;
    System.out.print("Enter first number:  ");
    number1 = number.nextInt();

    System.out.print("Enter Second number:  ");
    number2 = number.nextInt();

    sum = number1 + number2;

    System.out.printf("Sum is %d\n", sum);
  }
}

【问题讨论】:

  • 到底为什么要为要解析的每个令牌重新打开Scanner
  • @user1644742,您需要从更简单的开始,例如在循环中循环 while(inp != 'q'),在 de 循环中使用扫描仪(只有一个)读取 var inp 和打印输入,您将看到 a == 'q' 应用程序何时结束,否则将永远迭代。

标签: java output


【解决方案1】:

使用

   do{
      // do something.
   } while(some condition);

然后重复相同的扫描仪以获取输入。您还可以在菜单中再添加一个选项,以使用 while 条件重复和评估该选项。

【讨论】:

    【解决方案2】:

    它正在正常工作。 如果您希望它根据某些用户输入循环,则必须使用任何循环结构,如 while。 而不是if (math == 1) 使用

    `while (math != exit)`
    

    为退出创建一个新条目,如 0

    【讨论】:

      【解决方案3】:

      尝试使用while 循环。给用户一个退出程序的选项。

      import java.util.Scanner;
      public class java
      {
          public static void main(String args[])
          {
              Scanner a = new Scanner(System.in);
              System.out.println("Welcome to the simple Calculator program");
              while(true)
              {
                  System.out.println("Please type what type of math you would like to do: ");
                  System.out.println("1=Addition");
                  System.out.println("2=Subtraction");
                  System.out.println("3=Multiplication");
                  System.out.println("4=Division");
                  System.out.println("5=Sqrt");
                  System.out.println("6=Quit"); // added an option to quit the program
                  int math = a.nextInt();
                  if (math == 1)
                  {
                      int a1,a2,asum;
                      System.out.print("Please enter the first number:  ");
                      a1 = a.nextInt();
                      System.out.print("Please enter the second number:  ");
                      a2 = a.nextInt();
                      asum = a2 + a1;
                      System.out.println("The sum is: " + asum + "Thank You for using this program");
                  }
      
                  // Include actions for math = 2 to 5
      
                  if(math == 6)
                  {
                      System.out.println("Thank You for using this program");
                      System.exit(0);
                  }
              }
          }
      }
      

      每次计算后,选项会一次又一次地显示,直到用户想通过输入6退出程序。
      如果您希望程序只运行一次,则应省略外部 while 循环。其他一切都保持不变。
      PS - 你不需要一次又一次地重新打开Scanner(至少在这个问题中不需要)。

      【讨论】:

        【解决方案4】:

        那是因为您只从控制台读取输入一次..您需要使用类似 while(true) {} 的内容来保持控制台正常运行,或者监控控制台是否有类似 (" 0 = exit ") 的退出条件。

        另外,我认为您不需要像现在一样一次又一次地读取两个数字。

        【讨论】:

          【解决方案5】:

          1) 您可以使用 do-while 循环和您希望执行的条件。

          2) 使用 switch case 并使用不同的运算符在 switch case 内执行数学运算。到目前为止,您正在尝试仅执行加法。所以你是一个可以执行所有操作的 switch case。

          3) 在 switch 案例中有一个调用 exit(0) 方法的选项。这样您就可以运行程序,直到用户希望退出。

          4) 通过使用开关盒,您可以让用户选择自己的选项。

          【讨论】:

            【解决方案6】:

            你的整个程序都是正确的,伙计。

            只需添加

            System.exit(0);

            在每个 if(math==1) ,if(math==2)...在它们的语句结束之前。

            喜欢 if(math==1)

            {

            ...

            System.exit(0);

            }

            您可以修复您的错误...

            如果您的错误得到解决,请像我一样。如果不告诉我错误

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2023-04-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-04-24
              • 1970-01-01
              相关资源
              最近更新 更多