简体   繁体   English

不知道我在做什么错(布尔标志)

[英]Not sure what i'm doing wrong here (Boolean flag)

Here's a snippet of my code for an assignment. 这是我的作业代码段。 I can't seem to get the boolean flag to work properly. 我似乎无法使布尔标志正常工作。 When i try to figure it out, either every name gets the discount or no name gets the discount. 当我尝试找出答案时,每个名称都会获得折扣,或者每个名称都不会获得折扣。 To clarify the name Mike or Diana should give a discount. 为了澄清名称,Mike或Diana应该给予折扣。

String firstName;     //user's first name  
boolean discount = false;  //flag, true if user is eligible for discount 
int inches;       //size of the pizza
char crustType;     //code for type of crust
String crust; //name of crust
double cost = 12.99;    //cost of the pizza
final double TAX_RATE = .08; //sales tax rate
double tax;       //amount of tax
char choice;      //user's choice
String input;      //user input
String toppings = "Cheese "; //list of toppings 
int numberOfToppings = 0;  //number of toppings

//prompt user and get first name
System.out.println("Welcome to Mike and Diane's Pizza");
System.out.print("Enter your first name:  ");
firstName = keyboard.nextLine();

if (firstName == "mike" || firstName == "diana" || firstName == "Mike" || firstName == "Diana" ||
firstName == "MIKE" || firstName == "DIANA")
{
discount = true;
}

if (discount = true)
{
cost  -= 2.0;
System.out.println ("You are eligible for a $2 discount.");

First of all, in order to compare Strings, you don't use == . 首先,为了比较字符串,请不要使用== You need to use the String#equals() method as per this SO question . 您需要按照此SO问题使用String#equals()方法。

if (firstName == "mike" || firstName == "diana" || firstName == "Mike" || firstName == "Diana" || firstName == "MIKE" || firstName == "DIANA")

would be replaced by 将被替换

if (firstName.equals("mike") || firstName.equals("diana") || firstName.equals("Mike") || firstName.equals("Diana") || firstName.equals("MIKE") || firstName.equals("DIANA"))

However, as Gavin says in the comments to your original question, it would be better to convert entire string to either uppercase or lowercase, to make less comparaisons. 但是,正如Gavin在对原始问题的评论中所说的那样,最好将整个字符串转换为大写或小写,以减少比较。 Or, as per Pshemo 's comment, use equalsIgnoreCase() . 或者,按照Pshemo的评论,使用equalsIgnoreCase()

You also need to change: 您还需要更改:

if (discount = true)

which is assigning the value of true to the variable discount , to 这将true的值分配给变量discount ,以

if (discount == true)

or, as per Pshemo 's comment, 或者,根据Pshemo的评论,

if (discount)

I've checked your code with my eclipse. 我已经用日食检查了您的代码。 Using 使用

if(firstName == "mike" || firstName == "diana" || firstName == "Mike" || firstName == "Diana" ||firstName == "MIKE" || firstName == "DIANA")

is the problem, because of problems when comparing strings like this. 是问题,因为比较这样的字符串时遇到问题。 You must have to change it to 您必须将其更改为

if (firstName.equals("mike")){
    discount = true;
}

Then keep in mind that discount is a boolean variable. 然后请记住,折扣是一个布尔变量。 So you dont have to compare as whether it's true or not. 因此,您不必比较它是否正确。

if (discount) //if will check whether it's true or not if provided with a condition,discount is boolean, so need of comparison

will be very simple for boolean operator. 对于布尔运算符将非常简单。

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

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