gpt4 book ai didi

java - 我使用扫描仪遇到的数组错误

转载 作者:行者123 更新时间:2023-12-02 01:51:12 25 4
gpt4 key购买 nike

我正在做一项作业。需要并行数组...我需要几件事的帮助,嗯,至少三件事。

  • 我一路运行程序时遇到的第一个问题。我如何在打印语句中添加空格?出来是这样的“MondaySoda1.0”
  • 另一个问题是“1.0”,我的价格显然是“1.25”[0],但是为什么它打印出“1.0”?
  • 最后,如果我在 Tuesday 中输入,它会询问“一周中的哪一天做你想要......”它仍然打印出周一的信息。如何做我对它进行了编码,如果您输入 Tuesday,它不会在以下位置打印任何内容全部。

此时我将不胜感激任何帮助!

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");


}
}

最佳答案

好的,我们开始...

  • How do I add spaces in the print statement?

    添加空格如下所示

    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,它会输出 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

    发生这种情况是因为您将输入存储在 day 中并且尝试使用数组days的索引。只需打印出日期变量,您不需要数组 days

     System.out.println(day + "  " + drinks[0] + "  " + price[0]);

关于java - 我使用扫描仪遇到的数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52971523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com