如何将数组从一种方法打印到另一种方法



我需要一种方法来打印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,需要尽我所能的帮助。

viewCatalog()方法中取出数组的声明(在修复了Jaime在评论中提到的数组长度问题之后)。在这种方式下继续增长,只需去掉你申报的地方。然后,您可以在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]);
    }
}

一个可能的解决方案是创建一个类并将数组元素存储为字段

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/

相关内容

最新更新