gpt4 book ai didi

java - 如何对单行中任意数量的用户输入数字求和

转载 作者:行者123 更新时间:2023-12-01 13:13:32 25 4
gpt4 key购买 nike

试图弄清楚如何从用户那里获取任意数量的输入数字并将它们加在一起

用户输入示例:1 2 3 4总和 = 10

用户可以输入任意数量的数字,而不是指定数量,因此如果他想添加 1 2 3 4 5 6 7 8 9 10 11 12 13,则所有数字的总和将为 91

感谢您提前提供的帮助。

import java.util.Scanner;

public class test
{
public static final int SENTINEL = -1;
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int score = 0;
int sum = 0;

System.out.println("Enter numbers here");
while (score >= 0) {
if (score <= -1) {
score = kb.nextInt();
sum += score;
score = 0;
}
System.out.println(sum);
}
}
}
<小时/>

感谢 libik 的所有时间和帮助,这是完成的代码。

import java.util.Scanner;

public class JavaApplication1156 {

public static void main(String[] args) {
System.out.println("Enter numbers here");
int sum;
do {
Scanner kb = new Scanner(System.in);
int score = 0;
sum = 0;
String line = kb.nextLine();
kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
while (kb.hasNextInt()) {
score = kb.nextInt();
sum += score;
}
if (sum <= -1)
System.out.println("Application ended");
else if (sum >= 0)
System.out.println("Sum = " + sum);

} while (sum != -1);
}

}

最佳答案

其实很简单

import java.util.Scanner;

public class JavaApplication115 {

public static void main(String[] args) {
System.out.println("write numbers, if you write zero, program ends");
Scanner input = new Scanner(System.in); //just copy-and paste this line, you dont have to understand it yet.
int number;
int sum = 0;
do {
number = input.nextInt(); //this reads number from input and store its value in variable number
sum+= number; //here you add number to the total sum
} while(number != 0); //just repeat cycle as long as number is not zero

System.out.println("Sum is : " + sum);
}

}
<小时/>

基于您的代码的工作代码:

public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int score = 0;
int sum = 0;

System.out.println("Enter numbers here");
String line = kb.nextLine();

kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
while (kb.hasNextInt()) {
score = kb.nextInt();
sum += score;
}
System.out.println(sum);
}
<小时/>

此外,如果您对“最小”版本感兴趣,它与之前的版本相同,但使用尽可能少的代码,这里是:

public static void main(String[] args) {
int sum = 0;
System.out.println("Enter numbers here");
Scanner kb = new Scanner((new Scanner(System.in)).nextLine()); //has to do this to make the kb.hasNexInt() work.
while (kb.hasNextInt()) {
sum += kb.nextInt();
}
System.out.println(sum);
}
<小时/>

只要总和不为零,就求每行的总和(基于第二个代码块):

public static void main(String[] args) {
System.out.println("Enter numbers here");
int sum;
do {
Scanner kb = new Scanner(System.in);
int score = 0;
sum = 0;
String line = kb.nextLine();
kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
while (kb.hasNextInt()) {
score = kb.nextInt();
sum += score;
}
System.out.println("Sum = " + sum);
} while (sum != 0);
}

关于java - 如何对单行中任意数量的用户输入数字求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22647352/

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