我在如何以及在何处放置数组方面遇到问题。 我弄清楚了如何以及在哪里放置循环,以便它将继续收集将存储在数组内的多个用户数据,但我不知道在哪里放置数组。将有一个产品编号数组、一个产品名称数组和一个价格数组。
import java.io.*;
public class DataInsideArrays
{
public static DataInputStream a = new DataInputStream(System.in)
public static void main(String args[])throws Exception
{
int y = 0;
for(int x=1;x<=100;x++)
{
System.out.println("1 - Maintenance");
System.out.println("2 - Transaction");
System.out.println("");
System.out.print("Enter code: ");
int code =
Integer.parseInt(a.readLine());
System.out.println("");
if(code==l)
{
System.out.print("") ;
System.out.print("Product number: ");
System.out.print("Product name: ");
String prodname = a.readLine();
System.out.print("Price: ");
int price = Integer.parseInt(a.readLine());
System.out.println("");
}
else if(code==2)
{
System.out.println("Display List of Products-Prices")
System.out.print("Enter product number:
") ;
int prodnum
Integer.parseInt(a.readLine());
System.out.println("")
}
if(code==3)
{
System.out.println("Invalid");
System.out.println("Restart");
System.out.println("");
}}}}
最佳答案
首先,您在 for
循环中从哪里获得这个 l(小写 L)
值?这不应该是1(第一)
吗?此外,如果它是 0(零)
那就更好了,这样您就可以将它用作数组的索引。
第二件事,int y = 0' 的意义是什么?
您没有在程序中的任何地方使用y
。
int y = 0;
for(int x = l; x <= 100; x++)
将其更改为:
for(int x = 0; x < 100; x++)
由于您想要拥有 3 个独立的数组,因此您应该拥有 3 个独立的索引来跟踪它们。
现在让我们看看如何初始化一个数组来存储数据。您应该在类的顶部(或在本地 main()
中)将大小声明为变量,以便以后在程序中更轻松地更改它。您可以对阵列执行相同的操作。在您的类中或在 main()
中本地声明它。我将向您展示如何在类外声明两者,但您也应该尝试其他方式进行练习。
现在请记住,使用数组时,您必须在创建数组后立即提前知道数组的大小,并且之后无法更改该大小。这仅意味着数组中可以容纳许多元素。如果您想要可变大小的结构,请查看 ArrayList 的一些答案。
public class DataInsideArrays
{
public static DataInputStream a = new DataInputStream(System.in)
public static int size = 100; // now you only change this number and everything works
public static int[] productNumbers = new int[size];
public static String[] productNames = new String[size];
public static int[] productPrices = new int[size];
public static void main(String args[]) throws Exception
{
int prodnumIndex = 0; // index for tracking product numbers
int prodnameIndex = 0; // index for tracking product names
int prodpriceIndex = 0; // index for tracking product prices
for(int x = 0; x < size; x++)
{
// ... your code...
// ...
int prodNum = Integer.parseInt(a.readLine());
// check if we didn't reach our maximum size
if(prodnumIndex < productNumbers.length) {
productNumbers [prodnumIndex] = prodnum;
prodnumIndex++; // increment
} else {
System.out.println("Cannot add product number. Reached maximum amount.");
}
// ...
String prodName = a.readLine();
// check if we didn't reach our maximum size
if(prodnameIndex < productNames.length) {
productNames [prodnameIndex] = prodName ;
prodnameIndex++; // increment
} else {
System.out.println("Cannot add product name. Reached maximum amount.");
}
// ...
int prodPrice = Integer.parseInt(a.readLine());
// check if we didn't reach our maximum size
if(prodpriceIndex < productPrices.length) {
productPrices [prodpriceIndex] = prodPrice ;
prodpriceIndex++; // increment
} else {
System.out.println("Cannot add product number. Reached maximum amount.");
}
// ...
}
实现此目的的另一种方法是创建一个名为 Product
的对象,该对象将包含您需要的属性,然后拥有该对象的数组。
class Product {
int num;
String name;
int price;
Product(int num, String name, int price) {
this.num = num;
this.name = name;
this.price = price;
}
}
// ...
// declare your array of `Product` objects...
Product[] products = new Product[size];
// ...
// then you can do something like
System.out.println("Enter product number:");
int prodNum = Integer.parseInt(a.readLine());
System.out.println("Enter product name:");
String prodName = a.readLine();
System.out.println("Enter product price:");
int prodPrice = Integer.parseInt(a.readLine());
products[INDEX] = new Product(prodNum, prodName, prodPrice);
// ...
此外,还可以考虑使用 float
的 double
作为产品价格,因为它更能真实地代表实际产品。
关于java - 存储/显示数组的数据输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914142/