简体   繁体   English

如何根据用户输入创建一个持续运行的循环

[英]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.对于 class,我必须编写一个简单的程序,允许用户输入 package 的重量,然后告诉用户与该重量相关的运费。 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.这不是家庭作业所必需的,但我正在尝试添加一个 do-while 循环,如果用户想输入另一个权重,则要求用户在循环末尾输入字母 y,然后 while 部分测试是否 keepGoing 变量等于 y 重新开始循环。 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.即使用户输入y程序刚刚结束,任何人都可以帮忙。 import java.util.Scanner;导入 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).在 Java 中,只能使用==运算符对原始数据类型( intcharboolean等,基本上以小写开头的数据类型)进行测试。 Strings and other data types are stored much differently and the == operator calls the variables memory addresses, rather than the data they hold.字符串和其他数据类型的存储方式大不相同, ==运算符调用变量 memory 地址,而不是它们保存的数据。 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.因此,您不能使用相等运算符,因为无论您如何努力, keepGoing将始终存储在与"y"或您尝试将其设置为的任何其他变量不同的 memory 地址中。 (Unless of course you set them both to the same reference on purpose, but that would always result in true). (当然,除非您故意将它们都设置为相同的引用,但这总是会导致 true)。 So what you need to do is use String's builtin equals() function as is written below:所以你需要做的是使用 String 的内置equals() function,如下所示:

TLDR: Change keepGoing == y to keepGoing.equals(y) TLDR:keepGoing == y更改为keepGoing.equals(y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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