[英]Java loop scanner input until enter or “e” without .split
public class test {
private static Scanner userpress = new Scanner(System.in);
public static void main(String[] args) {
int r = 0;
int h = 0;
System.out.println("---------------------------------");
System.out.println("write your two numbers (numerator, denominator)");
userpress.useDelimiter("\\s"); // here
while (userpress.hasNextInt()) {
r = userpress.nextInt();
h = userpress.nextInt();
// userpress.nextLine(); // remove
int x = r / h;
System.out.print(x + " ");
}
}
private static void message() {
System.out.println("user pressed e");
}
}
This program asks the user for minimum of 2 inputs, the first input is numerator and the second will be denominator, then it will give the value of numerator/denominator and print it out.该程序要求用户至少输入 2 个输入,第一个输入是分子,第二个输入是分母,然后它会给出分子/分母的值并打印出来。 Here is what i want to be able to do: I want to be able to write down as many numbers as possible, for example
10 5 20 4
, the output should be 2, 5
and if i write 10 5 20 4 30 5
the output should be 2, 5, 6
.这是我想要做的:我希望能够写下尽可能多的数字,例如
10 5 20 4
,output 应该是2, 5
如果我写10 5 20 4 30 5
output 应该是2, 5, 6
。 however this only works for even numbers.但是这只适用于偶数。 if i write uneven numbers for example
10 5 3
, then the program crashes because 2 is not being divided by anything.如果我写奇数,例如
10 5 3
,那么程序会崩溃,因为 2 没有被任何东西除。 I want the program to delete the last input if its uneven, for example if input is 10 5 3
, then output should be 2
since 10/5=2 and the last input should be disgarded.如果最后一个输入不均匀,我希望程序删除它,例如如果输入是
10 5 3
,那么 output 应该是2
,因为 10/5=2 并且最后一个输入应该被忽略。 How do I solve that issue?我该如何解决这个问题? problem number 2: If i write an
e
after the numbers, for example;问题2:例如,如果我在数字后写一个
e
; 10 5 20 4 e
, then I want the output to be 2, 5 user pressed e
I dont want to use.split and i have to use hasnext...
and next()
10 5 20 4 e
,然后我希望 output 为2, 5 user pressed e
我不想使用.split,我必须使用hasnext...
和next()
You can use regex for checking spaces and than take the only first part before space.您可以使用正则表达式来检查空格,而不是在空格之前取唯一的第一部分。 Like this:
像这样:
import java.util.Scanner;
public class Main {
private static Scanner userpress = new Scanner(System.in);
public static void main(String[] args) {
String first;
String second;
System.out.println("---------------------------------");
System.out.println("write your two numbers (numerator, denominator)");
userpress.useDelimiter("\\s"); // here
while (userpress.hasNextLine()) {
first = userpress.nextLine();
second = userpress.nextLine();
if(first.contains(" ") || second.contains(" ")) {
String[] firstParts = first.split(" ");
String[] secondParts = second.split(" ");
first = firstParts[0];
second = secondParts[0];
}
// userpress.nextLine(); // remove
int x = Integer.valueOf(first) / Integer.valueOf(second);
System.out.print(x + " ");
}
}
private static void message() {
System.out.println("user pressed e");
}
}
The problem with your code is that you are scanning two integers with a single check of userpress.hasNextInt()
.您的代码的问题是您通过一次检查
userpress.hasNextInt()
来扫描两个整数。 This check should be there for every scan of an integer.每次扫描 integer 时都应进行此检查。
The working code:工作代码:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int r = 0;
int h = 0;
System.out.print("Write your two numbers (numerator, denominator): ");
Scanner userpress = new Scanner(System.in);
userpress.useDelimiter("\\s");
while (userpress.hasNextInt()) {
r = userpress.nextInt();
if (userpress.hasNextInt()) {
h = userpress.nextInt();
int x = r / h;
System.out.print(x + " ");
} else {
break;
}
}
String s = userpress.next();
if ("e".equalsIgnoreCase(s)) {
message();
}
}
private static void message() {
System.out.println("user pressed e");
}
}
A sample run:示例运行:
Write your two numbers (numerator, denominator): 10 5 3 e
2 user pressed e
Another sample run:另一个示例运行:
Write your two numbers (numerator, denominator): 10 5 3
2
Another sample run:另一个示例运行:
Write your two numbers (numerator, denominator): 10 5 20 5
2 4
Another sample run:另一个示例运行:
Write your two numbers (numerator, denominator): 10 5 20 5 e
2 4 user pressed e
Alternatively, if you would not mind using a little bit more space, you could come up with a simple algorithm, like:或者,如果您不介意使用更多空间,您可以提出一个简单的算法,例如:
public class Main {
private static Scanner userpress = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("---------------------------------");
System.out.println("write your two numbers (numerator, denominator)");
userpress.useDelimiter("\\s");
List<Integer> numbers = new ArrayList<>();
while(userpress.hasNextInt()) {
numbers.add(userpress.nextInt());
}
for (int i=0; i<numbers.size()-1; i+=2) {
int x = numbers.get(i)/numbers.get(i+1);
System.out.print(x + " ");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.