我一直收到这个错误:
类型不匹配:无法从java.lang.String转换为int
每当我去编译我的程序。我只是把我的主要包括在内,因为我不认为这与课程有任何关系。有人可以帮忙。
import java.util.Scanner;
public class publicationMain {
public static void main(String[] args) {
System.out
.println("Hi, welcome to the Publication Program. You will be required to answer several questions about the ");
System.out.print("publication(s):");
System.out.println("How many publications would you like?");
Scanner keyboard = new Scanner(System.in);
int x = keyboard.nextInt();
int test1;
test1 = "b"; // this is where I'm having my problem!
int i;
int b, d, e, f;
double c;
publicationAbstract[] a = new publicationAbstract[x];
for (i = 0; i < a.length; i++) {
System.out.println("What is the the title of the publication?");
b = keyboard.nextInt();
System.out.println("What is price of the publication?");
c = keyboard.nextDouble();
System.out
.println("What is the publication year for this publication?");
d = keyboard.nextInt();
System.out.println("Is this publication a Book(b) or a CD(c)?");
e = keyboard.nextInt();
if (test1 == e) {
System.out.println("How many pages is the book?");
f = keyboard.nextInt();
a[i] = new publicationSub1(b, c, d, e, f);
a[i].trans(c);
a[i].getpublicationinfo();
a[i].useMethod();
} else {
System.out.println("How many minutes are there in the book?");
f = keyboard.nextInt();
a[i] = new publicationSub2(b, c, d, e, f);
a[i].trans(c);
a[i].getpublicationinfo();
a[i].useMethod();
}
}
}
}
答案 0 :(得分:3)
当你说,
int test1;
test1="b";
你说test1是整数原始类型,然后你尝试为它分配一个String。这显然是非法的。如果你想要的话,我不清楚
int test1 = (int) 'b';
或
String test1 = "b";
甚至
int test1 = Integer.parseInt("b", 16); // <-- from hex.
答案 1 :(得分:0)
test1是int
,但您将其设置为等于字符串。 “b”不是整数。