为什么这个do / while循环卡在inf中。环?

时间:2013-11-10 00:28:00

标签: java loops

public class Tester
{
    // instance variables - replace the example below with your own
    Scanner operation = new Scanner(System.in);
    IntegerCalc calc = new IntegerCalc();



    public Tester()
    {

      char choice;
      String value;
      char again;
    }

    public void test()
    {
     char again = operation.next().charAt(0);
        do{
         System.out.println("Please pick one of the following operations: 1. Addition 2. Subtraction" +
          "3. Multiplication 4.Division 5. Modulo 6. Square 7.Square Root");

            int num = operation.nextInt();
        int ch;
        ch = operation.nextInt();


           String choice;
       choice = operation.next();


          switch (ch){
              case 1: calc.addition(num,num);
              break;
              case 2: calc.subtraction(num, num);
              break;
              case 3: calc.multiplication(num, num);
              break;
              case 4: calc.division(num, num);
              break;
              case 5: calc.modulo(num, num);
              break;
              case 6:calc.square(num);
              break;
              case 7: calc.squareRoot(num);
              break;
              default: System.out.println("Please enter a valid choice number.");
            }
              System.out.println("Type x to exit");



        }while(again != 'x' && again != 'X');
      {

      System.out.println("Thank you for using my program!");




    }

//这段代码的问题在于它甚至没有提示用户输入任何数据,它似乎只是陷入循环并且什么都不做。任何人都知道我能做些什么才能让它发挥作用?

3 个答案:

答案 0 :(得分:2)

您在char again = operation.next().charAt(0);循环之外有do/while,因此如果again满足while条件,您将获得无限循环。

答案 1 :(得分:0)

您需要接受来自用户的角色

again = operation.next().charAt(0);

在此声明之后

System.out.println("Type x to exit");

答案 2 :(得分:0)

System.out.println("Type x to exit");
char again = operation.next().charAt(0);

这应该有效