My if (choice.isEmpty()) {} 不起作用



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

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

其他部分有效,但 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

您只需要在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();
}

当我按回车键时,它只显示空白行,我没有输入任何内容。

scanner.next()不读取新行,因此您需要将choice = sc.next();替换为choice = sc.nextLine();

假设你像这样修改代码。

System.out.print ("n" + "Continue (y/n): "); //promt users for input
        sc.useDelimiter("[;rn]+");
        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();
        } /

最新更新