[英]How do I accept the user input and put it into the while condition
This is my code, the while loop does not have an input and the rep
variable does not accept an input:这是我的代码,while 循环没有输入,
rep
变量不接受输入:
import java.util.Scanner;
public class MixedData {
public static void main(String[] args) {
String rep = "";
do {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your full name");
String name = keyboard.nextLine();
System.out.print("Enter your GPA: ");
double gpa = keyboard.nextDouble();
System.out.println("Name: " + name + ", GPA: " + gpa);
System.out.println("Do you want to enter the data for another student?(y/n)");
rep = keyboard.nextLine();
} // This does not accept input
while (rep.equals("y"));
}
}
Either just add one more keyboard.nextLine()
before rep = keyboard.nextLine();
要么在
rep = keyboard.nextLine();
keyboard.nextLine()
( in order to clear the newline character ), or read your double gpa value with: (为了清除换行符),或者读取你的双 gpa值:
double gpa = Double.parseDouble(keyboard.nextLine());
Important point to understand here (especially if you're a novice Java developer), about why your code doesn't work, is, that you invoke nextDouble()
as a last method on your Scanner instance, and it doesn't move the cursor to the next line .这里要理解的重要一点(特别是如果你是一个新手 Java 开发人员),关于为什么你的代码不起作用,是你调用
nextDouble()
作为你的 Scanner 实例的最后一个方法,它不会移动cursor 到下一行。
A bit more details:更多细节:
All the methods patterned nextX()
(like nextDouble()
, nextInt()
, etc.), except nextLine()
, read next token you enter, but if the token isn't a new line character, then the cursor isn't moved to the next line.所有模式为
nextX()
的方法(如nextDouble()
、 nextInt()
等),除了nextLine()
,读取您输入的下一个标记,但如果标记不是换行符,则 cursor 不是移到下一行。 When you enter double value and hit Enter
, you actually give to the input stream two tokens: a double value , and a new line character , the double value is initialized into the variable, and the new line character stays into input stream.当您输入双精度值并按
Enter
时,您实际上给输入 stream 两个标记:双精度值和换行符,双精度值被初始化到变量中,换行符保留在输入 stream 中。 The next time you invoke nextLine()
, that very new line character is read, and that's what gives you an empty string.下次您调用
nextLine()
时,会读取那个非常新的行字符,这就是给您一个空字符串的原因。
You need to skip blank lines.您需要跳过空白行。
public static void main(String[] args) {
String rep;
Scanner keyboard = new Scanner(System.in);
do {
System.out.print("Enter your full name");
String name = keyboard.nextLine();
System.out.print("Enter your GPA: ");
double gpa = keyboard.nextDouble();
System.out.println("Name: " + name + ", GPA: " + gpa);
System.out.println("Do you want to enter the data for another student?(y/n)");
rep = keyboard.next();
keyboard.skip("\r\n"); // to skip blank lines
}
while (rep.equalsIgnoreCase("y"));
keyboard.close();
}
Here's the same code using a while loop instead of do-while.这是使用 while 循环而不是 do-while 的相同代码。 It works the way you want it to.
它按您希望的方式工作。
import java.util.Scanner;
public class MixedData {
public static void main(String[] args) {
String rep = "y";
while (!rep.equals("n")) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your full name: ");
String name = keyboard.nextLine();
System.out.print("Enter your GPA: ");
double gpa = keyboard.nextDouble();
System.out.println("Name: " + name + ",GPA: " + gpa);
System.out.println("Do you want to enter the data for another student?(y/n)");
rep = keyboard.next();
}
}
}
Use nextLine instead of nextDouble :使用nextLine代替nextDouble :
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String rep = "";
do {
System.out.println("Enter your full name:");
String name = keyboard.nextLine();
System.out.println("Enter your GPA:");
// double gpa = keyboard.nextDouble();
double gpa = Double.parseDouble(keyboard.nextLine());
System.out.println("Name: " + name + ", GPA: " + gpa);
System.out.println("Do you want to enter the data for another student?(y/n)");
rep = keyboard.nextLine();
} while (rep.equals("y"));
keyboard.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.