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

标签 java arrays java.util.scanner

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

  • 我一路运行程序时遇到的第一个问题。我如何 在打印语句中添加空格?出来是这样的 “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/

相关文章:

java - 如何使用java.util.Scanner从System.in正确读取用户输入并对其进行操作?

java - 使用 Java Scanner 库解析此内容的最有效方法是什么?

java - 获取 XML 文件的网站 URL JSP-Java

java - 格式化后拆分字符串并拼接的方法

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

c++ - 为什么 C++ 变量是指针时不需要正确定义?

C++ 类似于汇编的指针访问

java - 使用 Google Guice 删除 web.xml 文件

java - 如何禁用 Java 的 SSL session 恢复

javascript - 如何将 JSON 日期转换为日历格式?