0

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

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);
  }
}
4

6 回答 6

1

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

`while (math != exit)`

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

于 2012-12-27T19:24:08.853 回答
1

利用

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

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

于 2012-12-27T19:25:15.830 回答
1

尝试使用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(至少在这个问题中不需要)。

于 2012-12-27T19:26:22.817 回答
0

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

只需添加

System.exit(0);

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

像 if(math==1)

{

...

System.exit(0);

}

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

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

于 2014-05-20T10:55:00.493 回答
0

那是因为您只从控制台读取输入一次..您需要使用类似的东西保持控制台while(true) {}或监视控制台的退出条件,例如 (" 0 = exit ").

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

于 2012-12-27T19:25:38.563 回答
0

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

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

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

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

于 2012-12-27T19:43:37.440 回答