【发布时间】:2018-10-10 11:06:05
【问题描述】:
我需要将此程序的用户输入字符串的值添加到一起作为 Java 项目。我尝试过解析,但它似乎不起作用,我想不出许多其他简单的方法来查找字符串值的总和并打印它们。
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
int length;
int counter;
int sum = 0;
Scanner input = new Scanner(System.in);
System.out.println("How many numbers will you enter"); // input prompt
length = input.nextInt();
String[] number = new String[length];
for (counter = 0; counter < length; counter++) {
System.out.print("Number " + (counter + 1) + ": ");
number[counter] = input.next();
}
input.close();
System.out.print("The summation of ");
for (counter = 0; counter < length - 1; counter++) {
System.out.print(number[counter] + ", ");
}
System.out.print("and " + number[counter] + " is: ");
System.out.print(""); // How would I go about printing a sum ofthe values of the string?
}
}
【问题讨论】: