0

我正在尝试创建一个程序,允许用户说,输入(橙/苹果/香蕉/等),然后他们想要购买的数量,程序将计算总数。但是,在尝试了字符串(不能将它们相乘)和其他一些选项之后,我被卡住了。我和无数指南一起仔细浏览了这个论坛,但无济于事。我插入的 IF 语句只是让它工作的最后一次随机尝试,当然,它崩溃并烧毁了。我敢肯定,这都是基本的东西,但我对此很陌生。我还想显示一个可供选择的列表,可能类似于橘子:数量:(此处为框)苹果:数量:(此处为框)香蕉:数量:(此处为框)等但我真的很乐意寻求帮助允许用户输入一个单词,橙色,它被分配了我预设的值,所以我可以将它乘以数量。感谢所有帮助,当然也批评,你知道,在合理的范围内......这是我的代码。

/* Name 1, x0000
* Name 2, x0001
* Name 3, x0003
*/
import java.util.Scanner;

public class SD_CA_W3_TEST1
{
public static void main(String args[]) 
{
Scanner in = new Scanner(System.in);
double nameOfItem1, nameOfItem2, nameofItem3;
double quantityItem1, quantityItem2, quantityItem3;
final double apple = 0.30;
final double orange = 0.45;
final double strawberry = 2.30;
final double potato = 3.25;
final double turnip = 0.70;
final double carrot =  1.25;
double totalCost;
String strNameOfItem1;


System.out.println(" \t \t What would you like to buy today?");
System.out.print("Please choose from our fine selection of: oranges, strawberries, potatoes, turnips, and carrots. \n" );

System.out.print("Enter name of product ");
nameOfItem1 = in.nextDouble();
nameOfItem1 = If = nameOfItem1 (apple, orange, strawberry, potato, turnip, carrot);
System.out.print("Please enter a quantity to purchase");
quantityItem1 = in.nextDouble();

totalCost = quantityItem1 * strNameOfItem1;

System.out.print("The total cost of your purchase is: " +totalCost );
}
}
4

3 回答 3

0

我会使用HashMap。这是一个很好的教程: http ://www.tutorialspoint.com/java/util/hashmap_get.htm

HashMap food = new HashMap();
food.put("Apple", 0.30);
food.put("Orange", 0.45);
...

然后使用

food.get("Apple");

给你价格。

总计将是这样的:

double quantity = 4.0;    
double total = food.get("apple") * quantity;
于 2013-10-03T19:15:44.057 回答
0

尝试使用枚举,

class Test{

    public enum Fruits{
        apple(0.30), orange(0.45), strawberry(2.30), potato(3.25);

        private final double value;

        Fruits(double value1){
            value = value1;    
        }

        public double getValue(){
            return value;    
        }
    }

     public static void main(String[] args) {

        int quantity = 0;            
        // Read your value here and assign it to quantity

        System.out.println(Fruits.apple.getValue()*quantity);

    }

}
于 2013-10-03T19:21:54.953 回答
0

Enum在这里似乎是一个不错的选择。它将帮助您轻松地将项目名称映射到价格,而不是创建多个double变量。

private enum Items {
    APPLE(0.30), ORANGE(0.45), STRAWBERRY(2.30),
    POTATO(3.25), TURNIP(0.70), CARROT(1.25);

    double price;

    Items(double price) {
        this.price = price;
    }

    double getPrice() {
        return price;
    }
}

用于Scanner#next()读取String并用于Enum.valueOf()验证用户输入并将其转换为您Item的 s 之一。

Scanner in = new Scanner(System.in);

System.out.println("What would you like to buy today?");
System.out.println("Please choose from our fine selection of: " +
        "Orange, Strawberry, Potato, Turnip, and Carrot.");
System.out.print("Enter name of product: ");

String nameOfItem = in.next();
Items item;
try {
    // Validate Item
    item = Items.valueOf(nameOfItem.toUpperCase());
} catch (Exception e) {
    System.err.println("No such item exists in catalog. Exiting..");
    return;
}

System.out.print("Please enter a quantity to purchase: ");
int quantity;
try {
    quantity = in.nextInt();
    if (!(quantity > 0)) { // Validate quantity
        throw new Exception();
    }
} catch (Exception e) {
    System.err.println("Invalid quantity specified. Exiting..");
    return;
}

double totalCost = quantity * item.getPrice();
System.out.printf("The total cost of your purchase is: %.2f", totalCost);

输出

What would you like to buy today?
Please choose from our fine selection of: Orange, Strawberry, Potato, Turnip, and Carrot.
Enter name of product: Strawberry
Please enter a quantity to purchase:3
The total cost of your purchase is: 6.90
于 2013-10-03T19:34:03.790 回答