2015-11-10 116 views
-3

我需要一種方法來打印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

['使用等於()比較string'](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in -java) – sam

+0

你的第一個問題是你定義的數組太小。數組基於零(即從零開始)。這意味着大小爲15的數組包含元素0,1,2 ...,13,14 – Jamie

+0

您有2個獨立的程序嗎?通過將數組打印到控制檯,然後嘗試從控制檯讀取它們,將數組從一個功能傳遞到另一個功能效率不高。這隻有在命令行上有一個程序(viewCatalog)傳送到另一個程序(排序)時纔有效。 – Jamie

回答

0

從你的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]); 
    } 
} 
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實例的數組和簡單地調用排序各一次。

一個很好的例子在此示出 http://examples.javacodegeeks.com/java-basics/java-comparable-example/

相關問題