打印两次 [英] Printing Out Twice

查看:27
本文介绍了打印两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a simple program for teaching purposes and everything works except for the part where it prints out the name and the answer to the computation you chose. The if-statement seems to executing twice, as if it were taking a step backwards before going forward.

它会打印出Would you like to continue",但它不会提示用户输入是/否,而是会打印出计算的答案,然后是他们是否愿意继续的问题,除了第二次它实际上会等待输入.

It will print out the "Would you like to continue", but instead of prompting the user for a Y/N it will print out the answer to the computation AGAIN followed by the question of whether they would like to continue except the second time it will actually wait for input.

提前致谢.

这是我的代码:

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;

            }
        }
    }
}

推荐答案

问题在于 nextDouble() 函数.是的,这会读取一个双精度值,但它也会将换行符 ( ) 读取到缓冲区中.因此,下次您调用 nextLine() 时,它会立即解析换行符,而不是等待您的输入.只需在再次请求输入之前调用另一个 reader.nextLine() 即可清除换行符的缓冲区.

The issue is the nextDouble() function. Yes, this reads a double, but it also reads the newline character ( ) into the buffer. So next time you call nextLine(), it is parsing the newline off right away instead of waiting for your input. Just call another reader.nextLine() before you ask for your input again, to clear the buffer of the newline char.

这篇关于打印两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆