나는 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.
나는 불행하게도, 일할 수있는 프로그램을 얻을 수없는 것 : 내 프로그램 작업을하려면 내가 할 필요가있다. 잘만되면 누군가 나를 도울 수 있습니다.
이
루프 필요class Program2
입니다 : 현재 오류에 루프에있는 순간 "(itemNum이)"에서
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();
}
}
}
말 :
unreported exception ItemNotFound; must be caught or declared to be thrown
내가 있음을 의미하며, 그게 유일한 문제는 아닌 것 같아요, 나는 프로그램의이 부분을 제대로하지 않을 것입니다. 잘만되면 누군가 나를 도울 수 있습니다. 미리 감사드립니다.
이이 class 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 Catalog
입니다 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);
}
}
여기
은 또는 유용하지 않을 수 있습니다 다른 클래스입니다
이 class Item
입니다 :
public class Item {
private int itemnum;
private String title;
private double price;
// Construct a new item object.
public Item(int id, String t, double p) {
itemnum = id;
title = t;
price = p;
}
// Return the item number of this item.
public int getItemNumber() {
return itemnum;
}
// Return the item type. This is overridden in subclasses.
public String getItemType() {
return "Item";
}
// Return a printable String represenation of this item.
public String toString() {
String line1, line2, line3, line4, out;
String itemtype = this.getItemType();
line1 = String.format("Item number: %d%n", itemnum);
line2 = String.format("Item type: %s%n", itemtype);
line3 = String.format("Item title: %s%n", title);
line4 = String.format("Item price: %.2f%n", price);
out = line1 + line2 + line3 + line4;
return out;
}
}