Java 无法理解循环。希望得到帮助
我对Java 无法理解循环。希望得到帮助,java,exception,loops,bluej,Java,Exception,Loops,Bluej,我对Java相当陌生,我正在使用BlueJ。要使我的程序正常工作,我需要: // Insert code here to perform a sequence of // interactive transactions with the user. // The user enters an item number and the program // either displays the item or prints an error message
Java
相当陌生,我正在使用BlueJ
。要使我的程序正常工作,我需要:
// Insert code here to perform a sequence of
// interactive transactions with the user.
// The user enters an item number and the program
// either displays the item or prints an error message
// if the item is not found. The program terminates
// when the user enters zero as the item number.
不幸的是,我似乎无法让程序正常运行。希望有人能帮助我
这是需要循环的类程序2
:
import java.util.*;
public class Program2 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
Catalog store = new Catalog(3);
int itemnum = 0;
Item item;
try {
store.insert
(new Music(1111, "Gold", 12.00, "Abba"));
store.insert
(new Movie(2222, "Mamma Mia", 16.00, "Meryl Streep"));
store.insert
(new Book(3333, "DaVinci Code", 8.00, "Dan Brown"));
store.insert
(new Music(4444, "Legend", 15.00, "Bob Marley"));
} catch (CatalogFull exc) {
System.out.println(exc);
}
// Insert code here to perform a sequence of
// interactive transactions with the user.
// The user enters an item number and the program
// either displays the item or prints an error message
// if the item is not found. The program terminates
// when the user enters zero as the item number.
itemnum = kbd.nextInt();
while (itemnum == 0) {
item = store.find(itemnum);
if (item != null) {
System.out.print(itemnum);
} else {
System.out.printf("%s was not found.%n", item);
}
System.out.println();
System.out.print("Player (0 to exit)? ");
itemnum = kbd.nextInt();
}
}
}
当前错误在“(itemnum)”的循环中,表示:
尽管如此,我怀疑这不是唯一的问题,我没有正确地完成程序的这一部分。希望有人能帮我。先谢谢你
以下是一些可能有用或不有用的其他类:
这是类ItemNotFound
:
// This exception is thrown when searching for an item
// that is not in the catalog.
public class ItemNotFound extends Exception {
public ItemNotFound(int id) {
super(String.format("Item %d was not found.", id));
}
}
这是class CatalogFull
// This exception is thrown when trying to insert an item
// when the catalog is full.
public class CatalogFull extends Exception {
public CatalogFull() {
super("The catalog is full.");
}
}
这是类目录
:
public class Catalog {
private Item[] list;
private int size;
// Construct an empty catalog with the specified capacity.
public Catalog(int max) {
list = new Item[max];
size = 0;
}
// Insert a new item into the catalog.
// Throw a CatalogFull exception if the catalog is full.
public void insert(Item obj) throws CatalogFull {
if (list.length == size) {
throw new CatalogFull();
}
list[size] = obj;
++size;
}
// Search the catalog for the item whose item number
// is the parameter id. Return the matching object
// if the search succeeds. Throw an ItemNotFound
// exception if the search fails.
public Item find(int id) throws ItemNotFound {
for (int pos = 0; pos < size; ++pos){
if (id == list[pos].getItemNumber()){
return list[pos];
}
}
throw new ItemNotFound(id);
}
}
因为您定义了方法
find()
like,所以当找不到项时,它会抛出ItemNotFound
异常
public Item find(int id) throws ItemNotFound {
编译器告知方法find()
可能抛出异常
,请采取必要步骤
所以你需要把这一行item=store.find(itemnum)在中尝试catch block
,然后处理它
所以
find(int-id)方法显示ItemNotFound异常。我们需要在这里处理ItemNotFound异常
itemnum = kbd.nextInt();
while (itemnum == 0) {
try{
item = store.find(itemnum);
if (item != null) {
System.out.print(itemnum);
}
} catch (ItemNotFound e){
System.out.println(" Item was not found with id :"+ itemnum);
}
System.out.println();
System.out.print("Player (0 to exit)? ");
itemnum = kbd.nextInt();
}
public Item find(int id) throws ItemNotFound {
try{
item = store.find(itemnum);
} catch (ItemNotFound e){
//handle it
}
item = store.find(itemnum); //here you are finding the item
itemnum = kbd.nextInt();
while (itemnum == 0) {
try{
item = store.find(itemnum);
if (item != null) {
System.out.print(itemnum);
}
} catch (ItemNotFound e){
System.out.println(" Item was not found with id :"+ itemnum);
}
System.out.println();
System.out.print("Player (0 to exit)? ");
itemnum = kbd.nextInt();
}