我需要兩個字符串和雙陣列去從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
}
我知道這是錯的,但我不知道該做些什麼來解決它。
通過不創建並行數組,但使用Java的面向對象功能,並創建一個帶有兩個字段的對象的* single *數組,即以正確的方式進行。 – Andreas
@GDaniels而不是nextLIne你應該使用scan.next() –