0

我很新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 "()"
        }
    }
}
4

1 回答 1

2

字符串不可分配给整数

使用nextInt() since itemnum is int, where as next() returns String,因此不兼容的类型。

itemnum = kbd.nextInt();

不是

itemnum = kbd.next();

并看到不同的类型

于 2013-10-01T07:36:26.730 回答