0
所以我可能在這裏錯過了一個明顯的錯誤,但我似乎無法弄清楚。我有一個菜單,用戶可以使用它來顯示商店的全部庫存。但是,當我調用遍歷清單列表的方法時,它將無限顯示整個列表。這裏是我的代碼:每個無限循環的java - 無限顯示整個列表
import java.util.Scanner;
import java.util.ArrayList;
public class MyStore
{
private Scanner kbd;
private int inventorySize;
private ArrayList<InventoryItem> list;
public MyStore()
{
kbd = new Scanner(System.in);
System.out.print("What is the size of the inventory you are creating?");
inventorySize = kbd.nextInt();
list = new ArrayList<InventoryItem>(inventorySize);
initInventory();
}
public void initInventory()
{
for (int i = 0; i < this.inventorySize; i++)
{
System.out.println("Please select one of the following options: G)uitar D)rums");
String input = kbd.next();
String n;
int nis;
double p;
if (input.equals("G"))
{
System.out.print("What is the name of this item?");
n = kbd.next();
System.out.print("How many items are you adding?");
nis = kbd.nextInt();
System.out.print("What is the price of this item?");
p = kbd.nextDouble();
Guitar guitar = new Guitar(n, nis, p);
list.add(guitar);
}
else if(input.equals("D"))
{
System.out.print("What is the name of this item?");
n = kbd.next();
System.out.print("How many items are you adding?");
nis = kbd.nextInt();
System.out.print("What is the price of this item?");
p = kbd.nextDouble();
Drums drums = new Drums(n, nis, p);
list.add(drums);
}
}
}
public void start()
{
String nm;
boolean done = false;
while (!done)
{
System.out.print("Please select from the following menu:\n 1: Search for an item by name. \n 2: Display Inventory. \n 3: Quit \n ->");
int menuNum = kbd.nextInt();
int count = 0;
while(count != 3)
{
if (menuNum == 1)
{
System.out.print("Please enter the name of the item: ");
nm = kbd.next();
searchAndDisplay(nm);
count = 0;
}
else if (menuNum == 2)
{
displayWholeInventory();
}
}
}
}
public void searchAndDisplay(String name)
{
search(name);
boolean found = false;
if (name.equals(search(name)))
{
found = true;
}
else
{
System.out.print("Sorry, we currently don't have that in stock.");
}
}
public InventoryItem search(String name)
{
for (InventoryItem item : list.)
{
if (item.name().contains(name))
{
return item;
}
}
return null;
}
public void displayWholeInventory()
{
for (InventoryItem item : list) //loops through array and calls each item's print method
{
System.out.println();
item.display();
}
}
public void buyItem(String name) //extra credit
{
//find item, reduce numberInStock, print message for user
//or if numberInStock is already 0, print suitable message
}
public static void main(String[] args)
{
MyStore store = new MyStore();
store.start();
}
}
然後這裏是我的InventoryItem類,它有原來的顯示方法:
public class InventoryItem
{
private String name;
private int numberInStock;
//Constructor
public InventoryItem(String n, int nis)
{
name = n;
numberInStock = nis;
}
//display the item
public void display()
{
System.out.print("Item Name: " + name + " Stock Number: " + numberInStock);
}
//return name
public String name()
{
return name;
}
}
的displayWholeInventory()方法是在迭代時,對於其餘遺憾代碼,這仍然是一項正在進行中的工作。
任何幫助,將不勝感激,即使它只是在正確的方向推動。
謝謝
我只看到兩處'count'被修改的地方。在這兩個地方它被設置爲零。 – Sam
'while(count!= 3)' - 什麼時候'count'會變成3? – user2357112