我收到此错误:不兼容的类型
k0 =(i == 0 * 2);
......... ^
import java.util.Scanner; 公共课pers5 {
public static void main(String[] args) {
int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;
int produkt;
System.out.print("ange ett personnummer: ");
String indata = new Scanner(System.in).nextLine();
for (int i=0; i<10; i++){
int nummer=indata.charAt(i) -48;
System.out.print(indata.charAt(i)+ " ");
System.out.println(indata.charAt(i));
//this code has lots of stuff between these two but its irrelevant.
k0= (i==0 * 2);
k1= (i==1 * 1);
k2= (i==2 * 2);
k3= (i==3 * 1);
k4= (i==4 * 2);
k5= (i==5 * 1);
k6= (i==6 * 2);
k7= (i==7 * 1);
k8= (i==8 * 2);
k9= (i==9 * 1);
produkt= k0+k1+k2+k3+k4+k5+k6+k7+k8+k9;
答案 0 :(得分:1)
i==0
以及后续调用返回一个布尔值,因此它不能分配给k0,因为它的类型为int
答案 1 :(得分:0)
你错过了这一点
k0 = (i == 0 * 2); // i == 0 * 2 resulting a boolean
因此k0
必须是boolean
。您可以使用boolean
代替int