简体   繁体   English

如何接受用户输入并将其放入 while 条件

[英]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.

相关问题 如何使用另一个类的实例变量接受用户输入? - How do I accept user input with a instance variable of another class? 我如何循环直到用户在 java 上输入正确的输入? - How do I loop until the user put the correct input on java? 如何过滤用户输入以接受 char 和 int,但不接受 specialchar。 然后只将 int 放入一个数组中 - How do I filter a user input to accept char and int, but not specialchar. Then only takes the int into an array 使用Java,如何接受用户输入中包含数字和字母的短语并对其进行分类? - Using Java, how do I accept and categorize a phrase that has numbers and letters in user input? 如何让 java 只接受用户输入的数字 1-5? - How do I get java to only accept the user input of numbers 1-5? 询问用户输入时如何打破 while 循环? - how do I break a while loop when asking for user input? 如何将用户输入用于while循环中的字符串数组? - How do i use user input for a string array that is in a while loop? 如何在 while 循环内返回用户输入值? - How do I return the user input value inside of a while loop? 如何在黄瓜上接受用户输入 - How to accept user input on cucumber 在 Java 中,如何在没有扫描仪的情况下将用户输入放入字符串数组? - How do I put user input into an array of strings without a scanner in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM