[英]Array errors I'm facing using Scanner
I'm working on an assignment. 我正在做作业。 Parallel arrays are required... I need help with a couple of things, well, at least three things. 需要并行数组...我需要帮助,至少需要三件事。
I'll appreciate any help at this point! 目前,我们将不胜感激!
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");
}
}
Ok here we go... 好了,我们去...
How do I add spaces in the print statement? 如何在打印语句中添加空格?
Add spaces as shown below 如下所示添加空格
System.out.println(days[0] + " " + drinks[0] + " " + price[0]);
Another problem is the "1.0" I clearly have "1.25" for price [0] but why is it printing out "1.0"? 另一个问题是“ 1.0”,我显然将价格[0]设置为“ 1.25”,但是为什么打印出“ 1.0”呢?
Not sure what you mean, but if you input 1 it outputs 1.0 so on and so forth 不确定您的意思,但是如果输入1,则输出1.0,依此类推
Which day of the week do you want...." It stills prints out the information for Monday. How do I code it where if you type in Tuesday 您想在星期几?...“它仍然打印出星期一的信息。如果您在星期二键入,如何编码?
This is happening because you are storing the input in day
and trying to use the index of array days
. 发生这种情况是因为您将输入存储在day
并尝试使用数组days
的索引。 Just print out the day
variable, you dont need the array days
. 只需打印出day
变量,就不需要数组days
。
System.out.println(day + " " + drinks[0] + " " + price[0]);
First of all you have an extra semicolon after the price variable that should result in a compilation error: 首先,在price变量之后有一个多余的分号,这将导致编译错误:
double [] price; = {1.25, 1.50, 1.75, 2.00, 2.25, 2.50, 3.75};
Secondly you never use the values you instantiate the array with, you assign new values as you run the program. 其次,您永远不要使用实例化数组的值,而是在运行程序时分配新值。 So if you answer the first question - "What is the price of a Soda?" 因此,如果您回答第一个问题-“苏打水的价格是多少?” - with 1 , then the end result will be 1.0 . -带有1 ,则最终结果将是1.0 。
Thirdly, to add the required space, just add it when you print out the result: 第三,要添加所需的空间,只需在打印出结果时将其添加:
System.out.println(days[0] + " " + drinks[0] + " " + price[0]);
Parallel arrays is not a thing, but you could of course make a new class, called eg, Drink. 并行数组不是问题,但是您当然可以创建一个新类,例如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;
}
}
This way, you can use the constructor to create the drink and the setter to set the price, after you got the user input. 这样,您可以在获得用户输入后,使用构造函数创建饮料,并使用设置程序设置价格。 You could also include a field for discount (or discount of the day) or something. 您还可以包括折扣(或当日折扣)或其他内容的字段。
In your scenario, not sure where the days are for... 在您的情况下,不确定日子在哪里...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.