2016-11-10 47 views
1

我需要兩個字符串和雙陣列去從inputAccept表:如何通過返回主方法然後輸入第二個方法將兩個平行數組從一種方法移動到另一種方法?

inputAccept(); 

    table(names, costs); 

    public static void inputAccept() { 
      Scanner scan = new Scanner(System.in); 
      String[] names = {"","","","","","","","","",""}; 
      double[] costs = new double[10]; 

      for (int i = 0; i < 10; i++) 
      { 
       System.out.println("Enter the name of item " + (i + 1) + ": "); 
       names[i] = scan.nextLine(); 
       System.out.println("Enter item cost: "); 
       costs[i] = scan.nextDouble(); 
      } 
    } 

    public static void table(String[] names, double[] costs) { 
    //insert code using the arrays 
    } 

我知道這是錯的,但我不知道該做些什麼來解決它。

+0

通過不創建並行數組,但使用Java的面向對象功能,並創建一個帶有兩個字段的對象的* single *數組,即以正確的方式進行。 – Andreas

+0

@GDaniels而不是nextLIne你應該使用scan.next() –

回答

0

這種方法是清潔嬰兒的底部

private class InputResult { 
    //generate constructor with both fields 
    String[] names; //generate getter 
    double[] costs; //generate getter 
} 

然後

public static InputResult inputAccept() 
    ... 
    return new InputResult(names, costs); 

public static void table(InputResult input) 
    String[] names = input.GetNames(); 
    ... 
+0

編譯錯誤。 'obj [0]'與參數'String []'不兼容'。 'obj [1]'不兼容'double []'。不要說轉換它們,因爲這是錯誤的代碼模式來繞過編譯器類型檢查。創建一個班級。 – Andreas

+0

想你之前倒下,謝謝。 – Poody

+0

我確實在想。您在評論之前是否認爲閱讀我的評論?但我想我錯過了這個:編譯器錯誤。 'object'不是一個類。 – Andreas

0
public class Main { 

    static String[] names = {"","","","","","","","","",""}; 
    static double[] costs = new double[10]; 

    public static void main(String[] args) { 
     inputAccept(); 

     table(names, costs); 
    } 



    public static void inputAccept() { 
     Scanner scan = new Scanner(System.in); 

     for (int i = 0; i < 10; i++) 
     { 
      System.out.println("Enter the name of item " + (i + 1) + ": "); 
      names[i] = scan.next(); 
      System.out.println("Enter item cost: "); 
      costs[i] = scan.nextDouble(); 
     } 
    } 

    public static void table(String[] names, double[] costs) { 
     for (String name: names) { 
      System.out.println(name); 
     } 
     for (double value: costs) { 
      System.out.println(value); 
     } 
    } 
} 
+0

這應該解決你的問題,但我希望這只是爲了練習。大聲笑 –

+0

爲什麼要在初始化程序中分配一個數組而另一個是方法?在代碼中一致性是一件好事。 – Andreas

+0

這只是回答OP的問題。進一步的重新分解可以稍後完成.. –

1

您只需創建兩個數組中的主要方法,那麼首先將它們傳遞到inputAccept方法,然後將兩個數組傳入表方法:

public static void main(String[] args) { 
     double[] costs = new double[10]; 
     String[] names = {"","","","","","","","","",""}; 
     inputAccept(names, costs); 
     table(names, costs); 
    } 

    public static void inputAccept(String[] names, double[] costs) { 
     Scanner scan = new Scanner(System.in); 
     for (int i = 0; i < 10; i++) 
     { 
      System.out.println("Enter the name of item " + (i + 1) + ": "); 
      names[i] = scan.nextLine(); 
      System.out.println("Enter item cost: "); 
      costs[i] = scan.nextDouble(); 
     } 
    } 

    public static void table(String[] names, double[] costs) { 
    //insert code using the arrays 
    } 
+0

我有一個主要的方法。我只是發佈了兩個相關的方法。 – GDaniels

+0

複製問題的代碼時,沒有前兩行代表主要方法,這幾乎不是問題的答案。 – Andreas

+0

我編輯了答案,現在包含更好的解決方案。我之前誤解了這個問題。 – cullan

1

首先,它看起來像是從用戶輸入中創建「項目」。我覺得造型Item應該是第一步:

public final class Item { 
    private final String name; 
    private final double cost; 

    public Item(String name, double cost) { 
     this.name = name; 
     this.cost = cost; 
    } 

    public String getName() { 
     return name; 
    } 

    public double getCost() { 
     return cost; 
    } 
} 

然後inputAccept()變爲:

public static Item[] inputAccept() { 
    Item[] items = new Item[10]; 

    try (Scanner scan = new Scanner(System.in)) { 
     for (int i = 0; i < items.length; i++) { 
      System.out.println("Enter the name of item " + (i + 1) + ": "); 
      String name = scan.next(); 
      System.out.println("Enter item cost: "); 
      double cost = scan.nextDouble(); 

      items[i] = new Item(name, cost); 
     } 
    } 

    return items; 
} 

因此,table(...)成爲

public static void table(Item[] items) { 
    // insert code using the arrays 
} 

和最終用途是

public static void main(String[] args) { 
    table(inputAccept()); 
} 
相關問題