在简单的java程序中使用IF语句来应用折扣

时间:2014-10-13 22:25:30

标签: java if-statement boolean

我一直在使用搜索工具寻找答案,但我现在因为能够解决以前的几个问题而陷入困境。我能够比较和调整,但现在我不明白我的程序有什么问题。

我是第一年学习从Java开始的计算机编程。我读过很多人说要使用equalsIgnoreCase但是我们还没有学习它,这就是我在这一刻被教导的方式。我还看了一篇帖子说if (discount)可能比if (discount == true)好,但我从来没有使用if语句而没有在它旁边显示变量。

首先,它为我输入的每个名称提供折扣,包括那些不在指定给予折扣的4个名称中的名称。现在它没有给4个名字中的任何一个打折(Mike,mike,Diane,diane)。

我觉得这是一个非常小的错误,但我似乎无法弄明白。

import java.util.Scanner;
import java.text.DecimalFormat;

public class PizzaOrder
{
public static void main (String [] args)
{
    DecimalFormat formatter = new DecimalFormat("#0.00");

    Scanner keyboard = new Scanner (System.in);

    String firstName;                   
    boolean discount = false;       
    int inches;                     
    char crustType;             
    String crust = "Hand-tossed"; 
    double cost = 12.99;            
    final double TAX_RATE = .08;    
    double tax;                     
    char choice;                        
    String input;                   
    String toppings = "Cheese ";    
    int numberOfToppings = 0;       

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

    if (firstName == "Mike" || firstName == "mike")
    {
        discount = true;
    }
    if (firstName =="Diane" || firstName == "diane")
    {
        discount = true;
    }

    System.out.println("Pizza Size (inches)   Cost");
    System.out.println("        10            $10.99");
    System.out.println("        12            $12.99");
    System.out.println("        14            $14.99");
    System.out.println("        16            $16.99");
    System.out.println("What size pizza would you like?"); 
    System.out.print("10, 12, 14, or 16 (enter the number only): ");
    inches = keyboard.nextInt();

    if (inches == 10)
    {
        cost = 10.99;
    }
    if (inches == 12)
    {
        cost = 12.99;
    }
    if (inches == 14)
    {
        cost = 14.99;
    }   
    if (inches == 16)
    {
        cost = 16.99;
    }
    else
        System.out.println("You have selected incorrect pizza size.");
        System.out.println("You have been assigned 12 inch pizza by default.");

    keyboard.nextLine();    

    System.out.println("What type of crust do you want? ");
    System.out.print("(H)Hand-tossed, (T) Thin-crust, or " +
        "(D) Deep-dish (enter H, T, or D): ");
    input = keyboard.nextLine();
    crustType = input.charAt(0);

    switch (crustType)
    {
        case 'H':
        case 'h':
            System.out.println("You have selected Hand-tossed crust.");
            break;
        case 'T':
        case 't':
            System.out.println("You have selected Thin-crust.");
            break;
        case 'D':
        case 'd':
            System.out.println("You have selected Deep-dish crust.");
            break;
        default:
            System.out.println("You have selected invalid type of crust.");
            System.out.println("You have been assigned Hand-tossed crust by default.");
            break;
    }

    System.out.println("All pizzas come with cheese."); 
    System.out.println("Additional toppings are $1.25 each,"
            +" choose from");
    System.out.println("Pepperoni, Sausage, Onion, Mushroom");

    System.out.print("Do you want Pepperoni?  (Y/N):  ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
        numberOfToppings += 1;
        toppings = toppings + "Pepperoni ";
    }
    System.out.print("Do you want Sausage?  (Y/N):  ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
        numberOfToppings += 1;
        toppings = toppings + "Sausage ";
    }
    System.out.print("Do you want Onion?  (Y/N):  ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
        numberOfToppings += 1;
        toppings = toppings + "Onion ";
    }
    System.out.print("Do you want Mushroom?  (Y/N):  ");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'Y' || choice == 'y')
    {
        numberOfToppings += 1;
        toppings = toppings + "Mushroom ";
    }

    cost = cost + (1.25*numberOfToppings);

    System.out.println();
    System.out.println("Your order is as follows: ");
    System.out.println(inches + " inch pizza");
    System.out.println(crust + " crust");
    System.out.println(toppings);       

    if (discount)
    {
        System.out.println("You are eligible for $2.00 discount.");
        cost -= 2.00;
    }

    System.out.println("The cost of your order is: $" + formatter.format(cost));

    tax = cost * TAX_RATE;
    System.out.println("The tax is:  $" + formatter.format(tax));
    System.out.println("The total due is:  $" + formatter.format((tax+cost)));

    System.out.println("Your order will be ready for pickup in 30 minutes.");
}   
}

3 个答案:

答案 0 :(得分:1)

要比较您需要使用的字符串.equals()

firstName.equals("Mike") || firstName.equals("mike")

甚至更好

firstName.equalsIgnoreCase("mike")

答案 1 :(得分:1)

你错过了一个括号

else
    System.out.println("You have selected incorrect pizza size.");
    System.out.println("You have been assigned 12 inch pizza by default.");

应该是

else {
    System.out.println("You have selected incorrect pizza size.");
    System.out.println("You have been assigned 12 inch pizza by default.");
}

您也忘了设置默认的披萨大小。但是,您应该使用switch语句作为大小,并在默认情况下设置默认值。像这样:

switch(inches) {
    case 10: 
        cost = 10.99;
    break;
    case 12:
        cost = 12.99;
    break;
    case 14:
        cost = 14.99;
    break;
    case 16:
        cost = 16.99;
    break;
    default:
        System.out.println("You have selected incorrect pizza size.");
        System.out.println("You have been assigned 12 inch pizza by default.");
        inches = 12;
        cost = 12.99;
}

此处建议使用String.equalsString.equalsIgnoreCase的其他答案也是正确的。见How do I compare strings in java?。我非常怀疑你的教授会以正确的方式处理问题。我不确定==在这种情况下是否会起作用,但这是错误的。

答案 2 :(得分:0)

您需要使用equals()来比较字符串而不是==

这是因为Java中的字符串是对象,而==检查对象是否相同,而equals()则设计为查看字符串的内容。

例如:

String s1 = new String("abc");
String s2 = new String("abc");

System.out.println(s1 == s2);      //prints false
System.out.println(s1.equals(s2)); //prints true