我是Java
的新手,我正在使用BlueJ
。我一直收到错误:
incompatible types
现在这听起来很自我解释,但我似乎无法弄清楚如何解决问题。希望可以有人帮帮我。提前谢谢。
以下是class Program2
的代码:
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;
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.
while (!item.equals("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.next(); //Error on "()"
}
}
}
答案 0 :(得分:2)
字符串不能分配给整数
使用nextInt()
,因为itemnum
为int
,其中next()
返回String
,因此不兼容的类型。
itemnum = kbd.nextInt();
不是
itemnum = kbd.next();