Java 如何从长度未知的用户处读取输入?
我想输入一个由“,”分隔的数字字符串。我不知道要多久。输入将被传递到程序,并以字母“x”结束 爪哇 请帮忙!:) //////////////Java 如何从长度未知的用户处读取输入?,java,loops,dynamic,input,delimiter,Java,Loops,Dynamic,Input,Delimiter,我想输入一个由“,”分隔的数字字符串。我不知道要多久。输入将被传递到程序,并以字母“x”结束 爪哇 请帮忙!:) ////////////// public class fromUserSum { /// input : 1,2,4x from user /// output : 7 public static void main(String[] args) { Scanner scan = new Scanner(System.in); Strin
public class fromUserSum {
/// input : 1,2,4x from user
/// output : 7
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String userInput;
do {
System.out.println("Please enter a list of numbers in following format: 1,2,3,4x");
userInput = scan.next();
} while (!userInput.matches("(?:\\d+(?:,\\d+)*)x") || !userInput.matches("\\d+( \\d+)*x"));
scan.close();
String[] numberStrings;
if (userInput.contains(",")) {
numberStrings = userInput.replace("x", "").split(","); // 4x is now 4 and split by ','
} else {
numberStrings = userInput.replace("x", "").split(" ");
}
int sum = 0;
for (String i : numberStrings) {
sum += Integer.valueOf(i);
}
System.out.println("The sum of all numbers in the list is: " + sum);
}
}
试试这个
public static void main(String[] args) {
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value");
String string = sc.nextLine();
string = string.replace("x", "");
String[] strArray = string.split(",");
for (String str : strArray) {
result += Integer.valueOf(str);
}
System.out.println("Result is " + result);
}
输入为1,2,3,4x
输出为
结果为10
这也可以完成工作。此外,它还告诉用户预期的输入以及输入的列表是否与格式不匹配
public static void main(final String[] args) {
final Scanner userInputScanner = new Scanner(System.in);
String userInput;
do {
System.out.println("Please enter a list of numbers in following format: 1,2,3,4x or 1 2 3 4x");
userInput = userInputScanner.nextLine();
} while (!(userInput.matches("\\d+(,\\d+)*x") || userInput.matches("\\d+( \\d+)*x")));
userInputScanner.close();
final String[] numberStrings = userInput.replace("x", "").split("[, ]");
int sum = 0;
for (final String numberString : numberStrings) {
sum += Integer.valueOf(numberString);
}
System.out.println("The sum of all numbers in the list is: " + sum);
}
啊!请使用一个自包含的示例,说明您的现有代码如何无法处理堆栈跟踪(如果适用)。非常好,谢谢,请您进一步解释一下userInput。matches((?:\\d+(?:,\\d+*)x)您选择了什么符号?我看到了x,但另一个呢?这是一个正则表达式。它可以写得更简单:
\d+(,\d+)*x
<代码>\d+表示一个或多个数字,\d+
表示逗号后跟一个或多个数字()*
意味着括号内的内容可以无限重复,但不一定要重复x
表示一个单独的“x”。好吧,如果我这样做的话!userInput.matches((?:\\d+(?:\\d+*)x)是否意味着我需要放置一个类似于1 2 3 4x的表达式?它引入了一个非捕获组。但是,因为您只想匹配正则表达式,所以不需要它。是的,!userInput.matches((?:\\d+(?:\\d+*)x”)
接受类似于1234x的内容。但最好写得更短:!userInput.matches(“\\d+(\\d+)*x”)
。我编辑了我的帖子,你可以看到我试图这么做,但我得到了错误的答案,因为它只捕获最后一个整数:1 3 4 5x返回5
public static void main(String[] args) {
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value");
String string = sc.nextLine();
string = string.replace("x", "");
String[] strArray = string.split(",");
for (String str : strArray) {
result += Integer.valueOf(str);
}
System.out.println("Result is " + result);
}
public static void main(final String[] args) {
final Scanner userInputScanner = new Scanner(System.in);
String userInput;
do {
System.out.println("Please enter a list of numbers in following format: 1,2,3,4x or 1 2 3 4x");
userInput = userInputScanner.nextLine();
} while (!(userInput.matches("\\d+(,\\d+)*x") || userInput.matches("\\d+( \\d+)*x")));
userInputScanner.close();
final String[] numberStrings = userInput.replace("x", "").split("[, ]");
int sum = 0;
for (final String numberString : numberStrings) {
sum += Integer.valueOf(numberString);
}
System.out.println("The sum of all numbers in the list is: " + sum);
}