我需要一种方法来打印3个这些数组...
public static void viewCatalog(){
String[] description = new String[15];
description[0]= "Alumni Drink ware";
description[1]= "Binders";
description[2]= "Bookbag";
description[3]= "Fabulous Desserts";
description[4]="Folders";
description[5]="Gift Cards";
description[6]="Highlighters";
description[7]="Jackets";
description[8]="JAVA Programming";
description[9]="Network Solutions";
description[10]="Pencils";
description[11]="Pens";
description[12]="Shorts";
description[13]="Sweatshirts";
description[14]="Tshirts";
description[15]="Web Design Ideas";
String[] category = new String[15];
category[0]= "Gifts";
category[1]= "School Supplies";
category[2]= "School Supplies";
category[3]= "Textbooks";
category[4]="School Supplies";
category[5]="Gifts";
category[6]="School Supplies";
category[7]="Campus Gear";
category[8]="Textbooks";
category[9]="Textbooks";
category[10]="School Supplies";
category[11]="School Supplies";
category[12]="Campus Gear";
category[13]="Campus Gear";
category[14]="Campus Gear";
category[15]="Textbooks";
double[] price = new double[15];
price[0]= 25.00;
price[1]= 3.00;
price[2]= 20.00;
price[3]= 25.00;
price[4]=1.00;
price[5]=25.00;
price[6]=2.00;
price[7]=65.00;
price[8]=150.00;
price[9]=75.00;
price[10]=1.00;
price[11]=2.00;
price[12]=10.00;
price[13]=40.00;
price[14]=15.00;
price[15]=55.00;
System.out.println(Arrays.toString(description));
System.out.println(Arrays.toString(category));
System.out.println(Arrays.toString(price));
}
在这种方法中......
public static void sort(){
System.out.println("How would you like to sort?");
System.out.println("a) Increasing Price \n"
+ "b) Decreasing Price \n"
+ "c) Description \n"
+ "d) Category \n"
+ "Option: ");
Scanner input = new Scanner(System.in);
String option=input.next();
if (option=="a") {
}
}
我需要能够以不同的顺序打印它们,但是现在我根本无法打印它们。我不断收到错误,说“找不到课程”等。我试图通过价值传递,但我不认为我做得对。请帮忙!我无论如何都不擅长Java,需要我能得到的所有帮助。
答案 0 :(得分:0)
从viewCatalog()
方法中取出数组的声明(在修复注释中提到的数组长度问题Jaime之后)。继续在那个mehtod中弹出它们,然后取出你声明它们的地方。然后,您可以使用sort
方法访问公共静态数组。像这样的东西。
public class Main{
public static String[] description;
public static String[] category;
public static double[] price;
public static void viewCatalog(){
//Populate Arrays
description[0] = "foo";
}
public static void sort(){
//Sort and print
System.out.println(description[0]);
}
}
答案 1 :(得分:0)
一种可能的解决方案是创建一个类并将数组元素存储为字段
clas Item extends Comparable{
private double price;
private String desc;
private String category;
int sortSelection = (your input here);
public int compareTo(item i){
switch(sortSelection){
case 1: // sort by price
case 2: // sort by desc
case 3: // sort by category
}
}
}
然后创建Item
的实例数组,并且每次只调用sort。
这里显示了一个很好的例子 http://examples.javacodegeeks.com/java-basics/java-comparable-example/