Java 如何使用一个二维阵列创建4个阵列以及如何扫描它们
我得做一个关于商店和销售的节目。我必须从客户那里读取值,并将它们放入不同的数组和数量中。一个阵列中的商店、另一个阵列中销售的产品以及所有产品的数量和价格必须在2d阵列中。 我在2D阵列中遇到了困难 我试图读取所有数组的值,但当我打印2D数组时,它总是给我一个错误。我认为我的问题在于“for”,但我不确定,因为我是java新手Java 如何使用一个二维阵列创建4个阵列以及如何扫描它们,java,arrays,Java,Arrays,我得做一个关于商店和销售的节目。我必须从客户那里读取值,并将它们放入不同的数组和数量中。一个阵列中的商店、另一个阵列中销售的产品以及所有产品的数量和价格必须在2d阵列中。 我在2D阵列中遇到了困难 我试图读取所有数组的值,但当我打印2D数组时,它总是给我一个错误。我认为我的问题在于“for”,但我不确定,因为我是java新手 public static void main(String[] args) { Scanner read = new Scanner(System.in);
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int sells = 0;
String store = "";
System.out.print("How many sells did you make: ");
sells = read.nextInt();
String[] arrStores = new String[sells];
String[] arrProduct = new String[sells];
double[][] arrAmountPrice = new double[sells][2];
for (int i = 0; i < sells; i++) {
System.out.print("Store: ");
arrStores[i] = ler.next();
System.out.print("Product sold: ");
arrProduct[i] = ler.next();
System.out.print("Amount of product sold: ");
arrAmountPrice[i][0] = ler.nextDouble();
System.out.print("Price of all the product: ");
arrAmountPrice[i][1] = ler.nextDouble();
}
publicstaticvoidmain(字符串[]args){
扫描仪读取=新扫描仪(System.in);
int=0;
字符串存储=”;
System.out.print(“你卖出了多少?”;
sells=read.nextInt();
字符串[]arrStores=新字符串[销售];
字符串[]arrProduct=新字符串[销售];
double[]arrAmountPrice=新的double[销售][2];
for(int i=0;i
首先需要导入'java.util.Scanner'或'java.util.*'类才能访问Scanner对象,然后只需将'ler'对象替换为'read'
import java.util.*;
public class Test{
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int sells = 0;
String store = "";
System.out.print("How many sells did you make: ");
sells = read.nextInt();
String[] arrStores = new String[sells];
String[] arrProduct = new String[sells];
double[][] arrAmountPrice = new double[sells][2];
for (int i = 0; i < sells; i++) {
System.out.print("Store: ");
arrStores[i] = read.next();
System.out.print("Product sold: ");
arrProduct[i] = read.next();
System.out.print("Amount of product sold: ");
arrAmountPrice[i][0] = read.nextDouble();
System.out.print("Price of all the product: ");
arrAmountPrice[i][1] = read.nextDouble();
}
}
}
import java.util.*;
公开课考试{
公共静态void main(字符串[]args){
扫描仪读取=新扫描仪(System.in);
int=0;
字符串存储=”;
System.out.print(“你卖出了多少?”;
sells=read.nextInt();
字符串[]arrStores=新字符串[销售];
字符串[]arrProduct=新字符串[销售];
double[]arrAmountPrice=新的double[销售][2];
for(int i=0;i
欢迎使用堆栈溢出!简单地说“它总是给我一个错误”并不是一个有用的说法。请将您的问题包括错误的确切文本。此外,我建议使用OOP设计并创建一个对象来保存信息,而不是三个并行的数组