问题描述:

It executes correctly the first time, but:

  1. It keeps printing "Please try again (Y/N)?" no matter what the

    input is after asking to continue.

  2. I am unsure if != is appropriate to use for String comparison. I want to say while

    loopChoice "is not" Y or N, keep asking.

    while(isLoop) {

    // Ask for user input

    System.out.print("Enter hours worked: ");

    hoursWorked = scn.nextInt();

    System.out.print("Enter rate per hour: ");

    payRate = scn.nextInt();

    scn.nextLine();

    // Call functions to compute stuff

    ...

    // Print results

    ...

    System.out.print("\nDo you want to continue (Y/N)? ");

    loopChoice = scn.nextLine().toUpperCase();

    while(loopChoice != "Y" || loopChoice != "N") {

    System.out.print("\nPlease try again (Y/N)? ");

    loopChoice = scn.nextLine().toUpperCase();

    }

    switch(loopChoice) {

    case "Y":

    isLoop = true;

    System.out.print("\n");

    break;

    case "N":

    isLoop = false;

    System.out.println("Terminating program...");

    scn.close();

    System.exit(0);

    break;

    default:

    System.out.println("Your input is invalid!");

    isLoop = false;

    System.out.println("Terminating program...");

    scn.close();

    System.exit(0);

    break;

    }

    }

网友答案:

That's not how you compare strings in Java.

There is also a logical error in your code, as the string can't be both Y and N at the same time, you have to use && instead of ||. As long as the choice is neither Y or N, you want to continue the loop. If it is any of them, you want to stop. So && is the correct choice here.

To check if two strings are equal, you have to use .equals(obj)

while (!loopChoice.equals("Y") && !loopChoice.equals("N")) {

The reason for this is that == compares object references, and two Strings are most often not the same object reference. (Technically, they can be the same object reference, but that's not something you need to worry about now) To be safe, use .equals to compare Strings.

To avoid a possible NullPointerException in other situations, you can also use:

while (!"Y".equals(loopChoice) && !"N".equals(loopChoice)) {
网友答案:

You should compare with String equals

while (!loopChoice.equals("Y") && !loopChoice.equals("N"))

Also, replace the or operator with and operator

网友答案:

You need to know that OR Operation will return true if one of the two condition is true , so logically if you Enter Y , so you ask if the input is not equal Y so the answer is false then you will go to the next part in your condition if the input not equal N so the answer is True , so your finally result will be (True || False = True ) and then you will entered to while loop again

so the true condition is (the input not equal Y && not equal N)

网友答案:

You cannot use loopChoice != "Y", since "Y" is a String. Either use:

  • loopChoice != 'Y', or
  • "Y".equals(loopChoice)

Alternatively, use "Y".equalsIgnoreCase(loopChoice).

Case switching is also not possible for Strings if you use Java 1.6 or earlier. Be careful.

网友答案:

You have fallen into the common early gap between checking equality of objects versus the values of objects. (You can see a quick list of string comparison information [here] (http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html)

What you wrote asks whether the object loopChoice is the same object as the string constant "Y" or the string constant "N" which will always return false. You want to ask whether the value of object loopChoice is the same as the value of string constant "Y".

You could rewrite your code as follows:

System.out.print("\nDo you want to continue (Y/N)? ");
// get value of next line, and trim whitespace in case use hit the spacebar
loopChoice = scn.nextLine().trim();


while (!("Y".equalsIgnoreCase(loopChoice) || "N".equalsIgnoreCase(loopChoice)) {
    System.out.print("\nPlease try again (Y/N)? ");
    loopChoice = scn.nextLine().toUpperCase();
}

Note, I like to put the constant value first for clarity. The general form for determining whether the value of two strings is the same is String1.equalsIgnoreCase(String2).

相关阅读:
Top