在JAVA中添加我的输入
我有点困惑如何添加我输入的输入,例如,如果我输入(1,2,3,4),我怎么能得到10在JAVA中添加我的输入,java,Java,我有点困惑如何添加我输入的输入,例如,如果我输入(1,2,3,4),我怎么能得到10 int n1, n2; Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt(); System.out.print1n("The sum of those two numbers is"); System.out.
int n1, n2;
Scanner keyboard = new Scanner(System.in);
n1 = keyboard.nextInt();
n2 = keyboard.nextInt();
System.out.print1n("The sum of those two numbers is");
System.out.print1n(n1 + n2);
这样可以添加特定数量的输入,但如果我想添加随机输入,例如,有时我可能输入3个输入,有时7个输入。。。
我希望你明白我的意思,并感谢任何帮助:)谢谢如果你的目标是阅读用户输入的
int
,你可以使用:
如果需要在循环完成后在循环内部更改的int
值,请在循环之前声明int
:
inta代码>错误告诉您确切的问题。尝试打印时,变量shots
不在范围内
for (countd = 1; countd<= CupsOfCoffee; countd++)
{
System.out.println("How many coffee shots in cup "+ countd);
int shots = keyboard.nextInt();
}
用于(countd=1;countd你是如何定义kbd的?
?这是什么编程语言?这是最重要的问题,所以有正确技能的人会读它。对不起。它是java,我还是个初学者,这是我在大学的第三周,所以我没有那么多信息。它是java你问我们关于语法和compi的问题你故意发布垃圾代码,而不是真实的垃圾代码。对于(i=0….i++){
或(i=0,…i++)没有
或sysout…
在Java中。我对我的代码做了一些编辑,我写了出错的部分,当我尝试打印时,它说COUNT它说找不到符号是的,我尝试通过键入(int shots=0;)在循环外声明它在for循环之前..但我没有得到我想要的..我得到的是所有输出=到我输入的最后一个数字..例如(如果我输入3,4,5),我在5之后得到所有3个答案,这是错误的:/
for (countd = 1; countd<= CupsOfCoffee; countd++)
{
System.out.println("How many coffee shots in cup "+ countd);
int shots = keyboard.nextInt();
}