用我目前的代码,用户可以一次输入一个墨西哥玉米卷,但当我运行它时,我将输入第一个玉米饼的名称,然后价格自动列出“输入塔科价格”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的名称 酥脆薄饼卷 输入玉米饼的价格 1.19 输入taco 2的名称 至尊酥脆薄饼卷 输入玉米饼的价格 1.59 输入taco 3的名称 酥软墨西哥薄饼卷 输入玉米饼的价格 1.19
发布于 2015-09-23 17:25:48
我对玉米饼不太了解(开玩笑的,我真的知道),但我注意到有一些事情做错了。
首先,您的for-循环应该是:
for (int i = 0; i < NUMBER_OF_TACOS; i++) 最初您有i < NUMBER_OF_TACOS + 1,这将导致索引超出界限异常。
其次,您没有正确地终止第一个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
}
}发布于 2015-09-23 17:24:50
您的缩进和大括号不同步。
您可能希望在行tacos[i] = keyboard.nextLine();之后有一个尾大括号。
您可能希望在行for (int j = 0; j < NUMBER_OF_PRICES; j++)之后有一个开始支撑。
但那还是不能按你要求的顺序问问题。
发布于 2015-09-23 17:25:16
这是因为你的循环的顺序。此外,您还有许多异常和语法错误。
现在,您需要一个单独的循环,因为数组的长度相同。你会要求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();
}https://stackoverflow.com/questions/32745667
复制相似问题