我试图对每个炸玉米饼(1-10)的价格进行冒泡,同时让炸玉米饼名称遵循其原始价格(炸玉米饼名称不需要分类)。但是,我在我的排序的if语句中收到错误消息。
Exception in thread "main" java.lang.Error:
Unresolved compilation problems: The type of the expression must be an array type but it resolved to int
Type mismatch: cannot convert from double to int
Type mismatch: cannot convert from double to int
Type mismatch: cannot convert from double to int
Type mismatch: cannot convert from double to int
Type mismatch: cannot convert from double to int
at TacoSort.main(TacoSort.java:36)
我是否可能不准确地对双打进行冒泡分类,或者我没有恰当地合并字符串?
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)
{
// 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; 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");
prices[i] = keyboard.nextDouble();
}
for(double i = 0; i < NUMBER_OF_PRICES; i++)
{
for(double j = i + 1; j < tacos.length; j++)
{
if(NUMBER_OF_PRICES[i] > tacos[(int) j])
{
String temp = tacos[i];
tacos[i] = tacos[j];
tacos[i] = temp;
}
}
}
for(int i = 0; i < tacos.length; i++)
{
System.out.print(tacos[i] + " ");
}
}
}
答案 0 :(得分:0)
请参阅我的内联评论我认为他们会帮助你。 您的解决方案存在一些不同的问题,这些问题已在我的评论中注明。最大的问题是你没有跟踪与炸玉米饼名称交换的价格,因为你有两个不同的阵列。因此,如果您考虑一下,如果您更换taco名称,它将与不同的炸玉米饼价格相关联,这不是我们想要的。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class TacoSort {
//Create a constant amount of temperatures
public static int NUMBER_OF_TACOS = 5; //<-- you should consider only using one constant value since they will both always be the same
public static int NUMBER_OF_PRICES = 5;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); //<-- notice i changed replace the scanner with this line to allow spaces in between the name of the tacos
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]; //<-- it would be a better practice to create a taco object which would have two values name and price.
//Populates array of 10 prices
//Prompts user to enter price of each taco
double[] prices = new double[NUMBER_OF_PRICES]; //<-- error should initialize this array outside the loop or you will create a new one on every iteration
for (int i = 0; i < NUMBER_OF_TACOS; i++) {
System.out.print("Enter the name of taco " + (i + 1) + "\n");
tacos[i] = keyboard.readLine(); //<-- you want to read in next line so you can have spaces in your taco names
System.out.print("Enter taco's price " + (i + 1) + "\n");
prices[i] = Double.parseDouble(keyboard.readLine());
}
for (int i = 0; i < NUMBER_OF_PRICES; i++) {
for (int j = 1; j < NUMBER_OF_PRICES; j++) {
if (prices[j] < prices[j - 1]) //<-- error here you need to compare prices not what you were doing before by comparing the max size of your array
{
String temp = tacos[j];
tacos[j] = tacos[j - 1];
tacos[j - 1] = temp; //<-- error here should be swapping element j here
/* NOTE YOU NEED TO KEEP TRACK OF THE PRICES SWAP AS WELL */
double tempDouble = prices[j];
prices[j] = prices[j - 1];
prices[j - 1] = tempDouble;
}
}
}
System.out.println("Sorted Tacos are");
for (int i = 0; i < tacos.length; i++) {
System.out.println("Taco Prices " + tacos[i] + " " + prices[i] + " ");
}
}
}