打印出两次

时间:2013-12-18 20:21:27

标签: java if-statement while-loop

我为教学目的写了一个简单的程序,除了打印出你选择的计算名称和答案的部分外,一切都有效。 if语句似乎执行了两次,好像它在前进之前向前退了一步。

它会打印出“你想继续吗”,但不是提示用户输入Y / N,而是打印出计算结果的答案,然后是他们是否愿继续除了第二次它实际上会等待输入。

提前致谢。

这是我的代码:

import java.util.Scanner;

public class Numbers
{
    public static void main(String[] args)
    {
        Scanner reader = new Scanner(System.in);

        boolean cont;
        String name, yorn;
        double num1, num2, answer;
        int choice, iterator = 0;

        System.out.print("What is your name? ");
        name = reader.nextLine();

        System.out.print("Please enter a number: ");
        num1 = reader.nextDouble();
        if(num1%2 != 0)
        {
            System.out.println(name + ", the number uou entered, " + num1 + ", is odd");
        }
        else
        {
            System.out.println(name + ", the number uou entered, " + num1 + ", is even");
        }

        System.out.print("Please enter a second number: ");
        num2 = reader.nextDouble();
        if(num2%2 != 0)
        {
            System.out.println(name + ", the second number  you entered, " + num2 + ", is odd");
        }
        else
        {
            System.out.println(name + ", the second number you entered, " + num2 + ", is even");
        }

        System.out.println("1. Add");
        System.out.println("2. Subtract");
        System.out.println("3. Multiply");
        System.out.println("4. Divide");
        System.out.print("Please enter the number for the operation you would like to perform on your numbers: ");
        choice = reader.nextInt();

        cont = true;
        while(cont)
        {

            while(choice != 1 && choice != 2 && choice != 3 && choice != 4)
            {
                System.out.print("The number entered is not one of the options. Please choose one of the operations: ");
                choice = reader.nextInt();
            }

            if(choice == 1)
            {
                answer = num1 + num2;
                System.out.println(name +" the sum of " + num1 + " and " + num2 + " is: " + answer);
            }
            else if(choice == 2)
            {
                answer = num1 - num2;
                System.out.println(name +" the difference between " + num1 + " and " + num2 + " is: " + answer);
            }
            else if(choice == 3)
            {
                answer = num1 * num2;
                System.out.println(name +" the product of " + num1 + " and " + num2 + " is: " + answer);
            }
            else //if(choice == 4)
            {
                answer = num1/num2;
                System.out.println(name +" the quotient of " + num1 + " and " + num2 + " is: " + answer);
            }


            System.out.print("Would you like to do anything else (Y/N)? ");
            yorn = reader.nextLine();

            if(yorn.equals("Y"))
            {
                System.out.println("1. Add");
                System.out.println("2. Subtract");
                System.out.println("3. Multiply");
                System.out.println("4. Divide");
                System.out.print("Please enter the number for the operation you would like to perform on your numbers: ");
                choice = reader.nextInt();

            }
            else if (yorn.equals("N"))
            {
                System.out.println("Thank you for using this prgram. Have a good day");
                cont = false;

            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

问题是nextDouble()功能。是的,这会读取一个double,但它也会将换行符(\n)读入缓冲区。因此,下次调用nextLine()时,它会立即解析换行符,而不是等待您的输入。在再次请求输入之前,只需调用另一个reader.nextLine(),清除换行符的缓冲区。

答案 1 :(得分:0)

as @ adback03表示Scanner(obj)。 nextDouble()缓冲区“\ n”并且在 nextLine()期间从缓冲区读取;因此,循环继续

[关闭主题]我建议您使用 switch-case

switch(choice) {
    case 1: System.out.println(num1 + num2); break;
    case 2: System.out.println(num1 - num2); break;
    case 3: System.out.println(num1 * num2); break;
    case 4: System.out.println(num1 / num2); break;
    default: System.out.println("Invalid choice");
}

更容易阅读:)