我正在做作业。需要并行数组...我需要帮助,至少需要三件事。
在此感谢您的帮助!
import java.util.Scanner;
public class Cafeteria
{
public static void main (String [] args)
{
String [] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday ", "Saturday ", "Sunday "};
String [] drinks = {"Soda", "Sweet Tea", "Lemonade", "Frozen Lemonade", "Coffee-Hot", "Coffee-Iced", "Latte"};
double [] price; = {1.25, 1.50, 1.75, 2.00, 2.25, 2.50, 3.75};
for ( int i = 0; i < days.length; i++)
{
}
Scanner scan = new Scanner(System.in);
System.out.println("What is the price of a Soda? ");
price [0] = scan.nextDouble();
System.out.println("What is the price of a Sweet Tea? ");
price [1] = scan.nextDouble();
System.out.println("What is the price of a Lemonade? ");
price [2] = scan.nextDouble();
System.out.println("What is the price of a Frozen Lemonade? ");
price [3] = scan.nextDouble();
System.out.println("What is the price of a Coffee-Hot? ");
price [4] = scan.nextDouble();
System.out.println("What is the price of a Coffee-Iced? ");
price [5] = scan.nextDouble();
System.out.println("What is the price of a Latte? ");
price [6] = scan.nextDouble();
System.out.println();
scan.nextLine();
System.out.println("Which day of the week do you want the discounted drink price for?");
String day = scan.nextLine();
System.out.println();
System.out.println("Weekday Drink Original-Price Discount-Price");
System.out.println("----------------------------------------------------------");
System.out.println(days[0] + drinks[0] + price[0]); //Print out the list of the desire array when you enter a day in
System.out.println("The highest price drink is latte at $3.75");
}
}
答案 0 :(得分:1)
好,我们走...
如何在打印语句中添加空格?
如下所示添加空格
System.out.println(days[0] + " " + drinks[0] + " " + price[0]);
另一个问题是价格[0]的“ 1.0”显然是“ 1.25”,但是 为什么打印出“ 1.0”?
不确定您的意思,但是如果输入1,则输出1.0,依此类推
您想要星期几?...”它仍然打印出 星期一的信息。如果您在星期二输入,我该如何编码
之所以会这样,是因为您将输入存储在day
中,并且
尝试使用数组days
的索引。只需打印出day
变量,则不需要数组days
。
System.out.println(day + " " + drinks[0] + " " + price[0]);
答案 1 :(得分:1)
首先,在 price 变量之后有一个多余的分号,这将导致编译错误:
double [] price; = {1.25, 1.50, 1.75, 2.00, 2.25, 2.50, 3.75};
第二,您永远不要使用实例化数组的值,而是在运行程序时分配新的值。因此,如果您回答第一个问题-“苏打水的价格是多少?” -使用 1 ,则最终结果将为 1.0 。
第三,要添加所需的空间,只需在打印出结果时将其添加:
System.out.println(days[0] + " " + drinks[0] + " " + price[0]);
答案 2 :(得分:0)
并行数组不是问题,但是您当然可以创建一个新类,例如Drink。
public class Drink {
String drink;
double price;
public Drink(String drink) {
this.drink = drink;
}
public void setPrice(double price) {
this.price = price;
}
public String getDrink() {
return drink;
}
public double getPrice() {
return price;
}
}
这样,您可以在收到用户输入后,使用构造函数创建饮料,并使用设置器设置价格。您还可以在折扣(或当日折扣)或其他内容中添加字段。
在您的情况下,不确定日子在哪里...