使用我目前拥有的代码,用户可以一次输入一个炸玉米饼,但是当我运行它时,我将输入第一个炸玉米饼的名称,然后价格会自动列出& #34;输入Taco的价格" 1到10.如何解决这个问题。我确信在循环方面我做错了什么。
代码:
import java.util.Scanner;
public class TacoSort {
//Create a constant amount of temperatures
public static int NUMBER_OF_TACOS = 10;
public static int NUMBER_OF_PRICES = 10;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");
//Populates array of 10 tacos
//Prompts user to enter name of each taco
String[] tacos = new String[NUMBER_OF_TACOS];
for (int i = 0; i < NUMBER_OF_TACOS+1; i++)
{
System.out.print("Enter the name of taco " + (i+1) + "\n");
tacos[i] = keyboard.nextLine();
//Populates array of 10 prices
//Prompts user to enter price of each taco
double[] prices = new double[NUMBER_OF_PRICES];
for (int j = 0; j < NUMBER_OF_PRICES; j++)
System.out.print("Enter taco's price" + (j+1) + "\n");
System.out.println("\n");
prices[i] = keyboard.nextDouble();
}
}
}
用户输出应类似于:
输入taco 1的名称 脆脆的炸玉米饼 输入taco的价格
1.19
输入taco 2的名称 Crunchy Taco Supreme
输入taco的价格
1.59
输入taco 3的名称 软玉米饼 输入taco的价格
1.19
答案 0 :(得分:1)
我不太了解炸玉米饼(开玩笑,我真的这么做),但是我注意到这里做了一些错误。
首先,你的for循环应该是:
for (int i = 0; i < NUMBER_OF_TACOS; i++)
您最初拥有i < NUMBER_OF_TACOS + 1
,这会导致索引超出范围异常。
其次,您没有正确终止第一个for循环的括号;你错过了一个大括号。在你的第二个for循环条件之后,你也错过了一个起始的大括号。
第三,您应该通过循环检查错误的用户输入,直到用户输入适当的值,并使用类型转换而不是Scanner#nextDouble()
。
最后,您要完成整个第一个循环,然后是整个第二个循环。如果您希望您的程序询问Taco#1的名称和价格,然后是Taco#2的名称和价格,那么您应该在同一循环中询问每个炸玉米饼的名称和价格,等等。
正确的代码如下所示:
import java.util.Scanner;
public class TacoSort {
//Create a constant amount of tacos
public static int NUMBER_OF_TACOS = 10;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");
//Populates array of 10 tacos
//Prompts user to enter name of each taco
String[] tacos = new String[NUMBER_OF_TACOS];
double[] prices = new double[NUMBER_OF_TACOS]; // NOTE Moved this up from below
for (int i = 0; i < NUMBER_OF_TACOS; i++) // NOTE Fixed off-by-one error
{
System.out.print("Enter the name of taco " + (i+1) + "\n");
tacos[i] = keyboard.nextLine();
System.out.print("Enter taco's price " + (i+1) + "\n");
prices[i] = keyboard.nextDouble(); // TODO Fix this so it checks for non-double input?
} // NOTE Added end-bracket here!
// Consolidated prices loop into taco names loop
// Do something with the list of tacos and prices
}
}
答案 1 :(得分:0)
您的缩进和大括号不同步。
您可能需要在tacos[i] = keyboard.nextLine();
你可能想要在for (int j = 0; j < NUMBER_OF_PRICES; j++)
行之后的一个开始大括号。
但是仍然没有按照你要求的顺序提出问题。
答案 2 :(得分:0)
这是因为你的循环的顺序。此外,您还有许多异常和语法错误。
就目前而言,您需要一个循环,因为数组的长度相同。你们会要求Taco 1,价格1-10,然后是Taco 2,价格1-10等等。
抛弃嵌套循环,你会没事的。
for (int i = 0; i < NUMBER_OF_TACOS; i++) //tacos +1 will go out of bounds!
{
System.out.print("Enter the name of taco " + (i+1) + "\n");
tacos[i] = keyboard.nextLine();
//Populates array of 10 prices
//Prompts user to enter price of each taco
System.out.print("Enter taco's price" + (i+1) + "\n");
System.out.println("\n");
prices[i] = keyboard.nextDouble();
}
答案 3 :(得分:0)
上述代码的逻辑存在一些问题。请仔细研究一下,它运作得很好
import java.util.Scanner;
class TacoSort
{
//Create a constant amount of temperatures
public static int NUMBER_OF_TACOS = 10;
public static int NUMBER_OF_PRICES = 10;
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");
//Populates array of 10 tacos
//Prompts user to enter name of each taco
String[] tacos = new String[NUMBER_OF_TACOS];
for (int i = 0; i < NUMBER_OF_TACOS; i++)
{
System.out.print("Enter the name of taco " + (i+1) + "\n");
tacos[i] = keyboard.next();
//Populates array of 10 prices
//Prompts user to enter price of each taco
double[] prices = new double[NUMBER_OF_PRICES];
//for (int j = 0; j < NUMBER_OF_PRICES; j++) //
System.out.print("Enter taco's price" + (i+1) + "\n");
System.out.println("\n");
prices[i] = keyboard.nextDouble();
}
}
}