簡體   English   中英

如何接受用戶輸入並將其放入 while 條件

[英]How do I accept the user input and put it into the while condition

這是我的代碼,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"));
    }
}

要么在rep = keyboard.nextLine(); keyboard.nextLine() 為了清除換行符),或者讀取你的雙 gpa值:

double gpa = Double.parseDouble(keyboard.nextLine());

這里要理解的重要一點(特別是如果你是一個新手 Java 開發人員),關於為什么你的代碼不起作用,是你調用nextDouble()作為你的 Scanner 實例的最后一個方法,它不會移動cursor 到下一行

更多細節:

所有模式為nextX()的方法(如nextDouble()nextInt()等),除了nextLine() ,讀取您輸入的下一個標記,但如果標記不是換行符,則 cursor 不是移到下一行。 當您輸入雙精度值並按Enter時,您實際上給輸入 stream 兩個標記:雙精度值換行符,雙精度值被初始化到變量中,換行符保留在輸入 stream 中。 下次您調用nextLine()時,會讀取那個非常新的行字符,這就是給您一個空字符串的原因。

您需要跳過空白行。

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();
}

這是使用 while 循環而不是 do-while 的相同代碼。 它按您希望的方式工作。

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();
        }
    }
}

使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM