gpt4 book ai didi

java - 将用户输入存储在不是从 0 开始的数组中

转载 作者:行者123 更新时间:2023-11-29 04:11:56 25 4
gpt4 key购买 nike

我的问题是,在我下面的代码中,在选项 2 中,如果用户 Store = 1 并且项目详细信息会用新的详细信息覆盖项目 [0]。我希望它存储在项目 [7] 中而不是覆盖当前信息。该项目在另一个类的构造函数中找到。请查看我如何解决此问题并改进我的代码。

//构造类

public class Item {

private String itemName;
private int itemNumber;
private int itemQuantity;
private double itemPrice;

public Item() {
itemName = " ";
itemNumber = 0;
itemQuantity = 0;
itemPrice = 0.0;
}

public Item(String name, int itemNum, int itemQty, double itemP) {
this.itemName = name;
this.itemNumber = itemNum;
this.itemQuantity = itemQty;
this.itemPrice = itemP;
}

public void setItemName(String name) {
this.itemName = name;
}

public void setItemNumber(int itemNum) {
this.itemNumber = itemNum;
}

public void setItemQuantity(int itemQty) {
this.itemQuantity = itemQty;
}

public void setItemPrice(double itemP) {
this.itemPrice = itemP;
}

public String getName() {
return this.itemName;
}

public int getItemNumber() {
return this.itemNumber;
}

public int getItemQuantity() {
return this.itemQuantity;
}

public double getItemPrice() {
return this.itemPrice;
}

public void displayData() {
for (int i = 0; i < 1; i++) {
System.out.println("Name: " + this.itemName + "\t\tItem number: " + this.itemNumber + "\t\tPrice: $" + this.itemPrice
+ "\t\tQuantity: " + this.itemQuantity);
}
}

public boolean is_the_same(Item itemNum) {
boolean same = (itemName.equals(itemNum.itemName) && itemNumber == itemNum.itemNumber);
return same;
}
} //end of constructor

import java.util.Scanner;

public class test {

public static void main(String[] args) {
Item ItemArray[] = new Item[6];
Scanner input = new Scanner(System.in);

String[] itemName = {"Eggs", "Chicken", "Coca-Cola", "Apple", "Water", "Chocolate"};
int[] itemNumber = {1, 5, 11, 15, 12, 20};
Double[] itemPrice = {0.2, 5.8, 1.4, 0.6, 1.0, 0.7};
int[] itemQuantity = {60, 20, 12, 35, 80, 58};

for (int x = 0; x < ItemArray.length; x++) {
ItemArray[x] = new Item(itemName[x], itemNumber[x], itemQuantity[x], itemPrice[x]);
}

for (int i = 0; i < 1; i++) {
System.out.println("Choose one of the options[] below: \n[1] Buy item. \n[2] Add an item to store. \n[3] List all items and their prices. \n[4] Search for an item.");
System.out.print("Enter Option: ");
int Option = input.nextInt();

if (Option < 1 || Option > 4) {
System.out.println("\nInvalid Option. Please enter option again.");
System.out.println("Choose one of the options[] below: \n[1] Buy item. \n[2] Add an item to store. \n[3] List all items and their prices. \n[4] Search for an item.");
System.out.print("Enter Option: ");
Option = input.nextInt();
}

if (Option == 2) {
System.out.print("\n--------------OPTION 2--------------");
System.out.print("\nHow many items to add to store: ");
int Store = input.nextInt();

for (int c = 0; c < Store; c++) {
System.out.print("Name: ");
ItemArray[c].setItemName(input.next());
System.out.print("Number: ");
ItemArray[c].setItemNumber(Integer.parseInt(input.next()));
System.out.print("Price: $");
ItemArray[c].setItemPrice(Double.parseDouble(input.next()));
System.out.print("Quantity: ");
ItemArray[c].setItemQuantity(Integer.parseInt(input.next()));
}

input.close();

System.out.print("\n\n");
for (int c = 0; c < ItemArray.length; c++) {
ItemArray[c].displayData();
}
System.exit(0);
}
}
}
}

最佳答案

如果您要在特定索引处插入新元素,使用 List 对象或其他集合会比使用数组基元更简单。

List.add函数可让您在特定位置插入项目。

关于java - 将用户输入存储在不是从 0 开始的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54615172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com