扫描仪只读取第一行

时间:2016-02-11 00:25:08

标签: java input

import java.util.Scanner;

public class ArithmeticDemo {
    public static void main(String[] args)
    {
        int firstNumber;
        int secondNumber;
        int sum;
        int difference;
        int average;
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a double >> 14");
        firstNumber = input.nextInt();
        System.out.print("Please enter another double >> 11 ");
        secondNumber = input.nextInt();
        sum = firstNumber + secondNumber;
        difference = firstNumber - secondNumber;
        average = sum / 2;
        System.out.println(firstNumber + " + " + secondNumber + " is " + sum);
        System.out.println(firstNumber + " - " + secondNumber + " is " + difference);
        System.out.println("The average of " + firstNumber + " and " + secondNumber + " is " + average);
    }
}

我正在做学校作业,一切看起来都不错,我甚至休息一下然后回来但是代码应该在执行时显示

Please enter an integer >> 14
Please enter another integer >> 11
14 + 11 is 25
14 - 11 is 3
The average of 14 and 11 is 12

但是当我执行它时,它只会读取

Please enter an integer >> 14

我正在使用Eclipse和它的统计数据我的扫描仪输入命令有问题,但我不确定它是什么,谢谢你的帮助。

3 个答案:

答案 0 :(得分:1)

您的print语句不应包含该号码。它应该只读" System.out.print("Please enter a double >> ")"。这是第一个问题。此外,您的提示读取"请输入双",但您正在读取整数。将该行更改为input.nextDouble()。否则,它会按预期完全运行。它打印"请输入双>> ",我键入一个数字,它打印下一行,我输入另一个数字,然后输出信息。请查看java.util.Scanner here

的文档

答案 1 :(得分:0)

它按预期运行

Please enter a double >> 1414
Please enter another double >> 11 11
14 + 11 is 25
14 - 11 is 3
The average of 14 and 11 is 12

该号码在提示中出现两次。我建议你把代码更改为

    System.out.print("Please enter a double >> ");
    firstNumber = input.nextInt();
    System.out.print("Please enter another double >> ");
    secondNumber = input.nextInt();

答案 2 :(得分:0)

您是否正在为您的计划提供继续所需的输入?正如彼得斯回答的那样,你已经打印了14并且还没有输入它。我知道这有点傻,但这可能就是你的节目悬而未决的原因。