gpt4 book ai didi

java - 扫描仪仅读取第一行

转载 作者:行者123 更新时间:2023-11-30 07:36:27 24 4
gpt4 key购买 nike

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,它统计我的扫描仪输入命令有问题,但我不确定它是什么,感谢您的帮助。

最佳答案

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

关于java - 扫描仪仅读取第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35328647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com