我正在尝试创建一个程序,允许用户说,输入(橙/苹果/香蕉/等),然后他们想要购买的数量,程序将计算总数。但是,在尝试了字符串(不能将它们相乘)和其他一些选项之后,我被卡住了。我和无数指南一起仔细浏览了这个论坛,但无济于事。我插入的 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 );
}
}