我想創建一個購物清單程序,在輸入產品的名稱和價格後,將它們輸入到數組中,然後打印出這個代碼有哪些錯誤的整個列表?從掃描儀到陣列的寫入數據
import java.util.Scanner;
import java.util.Arrays;
public class List {
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
String [] name = new String[4];
double [] price = new double[4];
for (int i =0; i<name.length; i++) {
System.out.println("name");
name[i] = sc.nextLine();
System.out.println("price");
price[i] = sc.nextDouble();
}
System.out.println("your product: " + Arrays.toString(name) + Arrays.toString(price));
}
}
這是當你對混合'nextLine()'和'nextDouble()' – Rishav
而不是'sc.nextDouble電話會發生什麼()','sc.nextLine()'並解析爲'Double'。 – Rishav