名称/变量输入和列表

时间:2013-10-03 18:57:27

标签: java

我正在尝试创建一个程序,允许用户说,输入(橙色/苹果/香蕉/等),然后是他们想要购买的数量,程序将计算总数。但是,在尝试使用Strings(无法将它们相乘)和其他一些选项之后,我就陷入了困境。我和无数的指南一起浏览了这个论坛,但没有用。 我插入的IF语句只是最后一次随意尝试使其工作,当然,它崩溃和烧毁。这是我确定的所有基本内容,但我对此很陌生。 我还想显示一个可供选择的列表,也许是类似的东西 橘子:Qnty :(盒子在这里) 苹果:Qnty :(盒子在这里) 香蕉:Qnty :(盒子在这里) 等等 但我真的很乐意帮助用户输入一个单词,橙色,然后分配我预设的值,这样我就可以将它乘以数量。 当然,所有的帮助都受到赞赏,对你来说,合理的程度...... 这是我的代码。

/* 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 );
}
}

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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);

    }

}

答案 2 :(得分: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之一。

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