这是我的代码作业的一个片段。我似乎无法让布尔标志正常工作。当我试图弄清楚,要么每个名字都得到折扣,要么没有名字得到折扣。澄清麦克或戴安娜的名字应该给予折扣。不知道我在做什么错在这里(布尔标志)
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.");
什么问题? – PeterK 2014-10-10 22:24:16
除了字符串比较,你应该用'if(discount == true)'替换'if(discount = true)'。第一种是给变量'折扣'赋值,第二种是比较。 – AntonH 2014-10-10 22:26:50
@SotiriosDelimanolis字符串比较不是唯一的问题 – 2014-10-10 22:27:01