我的 if (choice.isEmpty()) {} 不工作

My if (choice.isEmpty()) {} is not working

我想在用户没有输入值时显示一条错误消息,所以我使用了这个。如果您能帮助我在用户未输入值时显示我的错误,那就太好了。

while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
    { //start of while

        if (choice.isEmpty()) {
            System.out.println("Error! This entry is required. Try again."); //display error message when the user enters invalid response


       }


        else {
            System.out.println("Error! Entry must be 'y' or 'n'. Try again."); // display error message when the user enters no response
        }

        System.out.print ("Continue (y/n): "); //promt users for input
        choice = sc.next();
} // End of while

else 部分有效,但 if 部分无效。当我按回车键时它只显示空行,我什么也没输入。这是整个程序的完整代码。这是一个平均击球率应用程序。

import java.util.*;
import java.text.NumberFormat;

public class BattingAverage

{



public static void main(String[] args)
{

System.out.println("Welcome to the Batting Average Calculator.");
System.out.println();

Scanner sc = new Scanner(System.in); // scanner for input
String choice = "y"; // initialize string
while (choice.equalsIgnoreCase("y")) //start of while to continue while the user answers y to the continue prompt below

{

int numberOfTimesAtBat;


System.out.print("Enter Number of times at bat: ");
numberOfTimesAtBat = sc.nextInt();

System.out.println("\n" + "0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run");

   int[] atBats = new int[numberOfTimesAtBat];

   for(int counter = 0; counter < numberOfTimesAtBat; counter++){
    System.out.print("Result for at-bat "+(counter+1)+": ");
    atBats[counter] = sc.nextInt();

} //End of for



int sum = 0;


for (int i=0; i < atBats.length; i++) {

sum += atBats[i];

}
double Avg = sum / atBats.length;



// System.out.print("Batting average:" );
System.out.print("Slugging percent: " + Avg);


System.out.print ("\n" + "Continue (y/n): "); //promt users for input
    choice = sc.next(); //make choice the next input


    while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
    { //start of while

        if (choice.isEmpty()) {
            System.out.println("Error! This entry is required. Try again."); //display error message when the user enters invalid response


       }


        else {
            System.out.println("Error! Entry must be 'y' or 'n'. Try again."); // display error message when the user enters no response
        }

        System.out.print ("Continue (y/n): "); //promt users for input
        choice = sc.next();
} // End of while




} // end of while




} //end of main
} // end of class

It just shows blank lines when I hit enter and I don't enter anything.

scanner.next()不读新行,所以需要将choice = sc.next();替换为choice = sc.nextLine();

您只需在 While 循环之前将 sc.next() 更改为 sc.nextLine()

choice = sc.nextLine(); //make choice the next input


    while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
    { //start of while

        if (choice.isEmpty()) {
            System.out.println("Error! This entry is required. Try again."); //display error message when the user enters invalid response


       }


        else {
            System.out.println("Error! Entry must be 'y' or 'n'. Try again."); // display error message when the user enters no response
        }

        System.out.print ("Continue (y/n): "); //promt users for input
        choice = sc.next();
}

假设你这样修改代码。

System.out.print ("\n" + "Continue (y/n): "); //promt users for input
        sc.useDelimiter("[;\r\n]+");
        choice = sc.next(); //make choice the next input


            while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
            { //start of while


                if (choice.trim().isEmpty()) {
                    System.out.println("Error! This entry is required. Try again."); //display error message when the user enters invalid response


               }else {
                    System.out.println("Error! Entry must be 'y' or 'n'. Try again."); // display error message when the user enters no response
                }

                System.out.print ("Continue (y/n): "); //promt users for input
                choice = sc.next();
        } /