简体   繁体   中英

How can I make a loop that keeps going based on user input

for a class I have to make a simple program that allows a user to enter the weight of a package and then it tells the user the shipping price associated with that weight. It's not required for the homework but I am trying to add a do-while loop that asks the user to enter the letter y at the end of the loop if they would like to enter another weight and then the while section tests if the keepGoing variable is equal to y to restart the loop. I don't know why but no matter what the user enters, the loop does not restart. Even if the user enters y the program just ends, can anyone help please. import java.util.Scanner;

public class HwkChp4 {

    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {

        double packageWeight;
        double underTwo = 1.10;
        double twoToSix = 2.20;
        double sixToTen = 3.70;
        double overTen = 3.80;
        String msg1 = "How much does the package weigh?";
        String msg2 = "The shipping cost is ";
        String keepGoing = "y";

        packageWeight = getDouble(msg1);

        do {
            if(packageWeight <= 2) {
                System.out.println(msg2 + "$" + underTwo);
            }

            if(packageWeight > 2 && packageWeight <= 6) {
                System.out.println(msg2 + "$" + twoToSix);
            }

            if(packageWeight > 6 && packageWeight <= 10) {
                System.out.println(msg2 + "$" + sixToTen);
            }

            if(packageWeight > 10) {
                System.out.println(msg2 + "$" + overTen);
            }
            System.out.println("Please enter y if you would like to enter another weight");
            System.out.println("otherwise please enter n");
            keepGoing = keyboard.next();
        } while (keepGoing == "y");

    }

    public static double getDouble(String msg1) {
        double decimal;
        System.out.println(msg1);
        decimal = keyboard.nextFloat();
        return decimal;
    }
}

In Java, equality can only be tested using the == operator on primitive data types ( int , char , boolean , etc., basically data types that start lowercase). Strings and other data types are stored much differently and the == operator calls the variables memory addresses, rather than the data they hold. Therefore, you cannot use the the equality operator as no matter how hard you try, keepGoing will always be stored in a different memory address than "y" or any other variable you would try to set it to. (Unless of course you set them both to the same reference on purpose, but that would always result in true). So what you need to do is use String's builtin equals() function as is written below:

TLDR: Change keepGoing == y to keepGoing.equals(y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM