您好,我不明白为什么我的代码中会出现此错误。
线程“main”java.lang.Error中出现异常: Unresolved 编译问题: 类型不匹配:无法从 String 转换为 int 这是我的代码:
import java.util.*;
public class matchScore{
public static void main(String[] args){
String opt;
String home_team_name;
String away_team_name;
int home_team_score;
int away_team_score;
String[] name = new String[10];
int[] score = new int[10];
System.out.println("Retype an option:\n");
System.out.println("Home teams");
System.out.println("Away teams");
System.out.println("Outputs");
Scanner scan = new Scanner(System.in);
opt = scan.nextLine();
if(opt == "Home teams"){
System.out.println("Entre first home team name: ");
name[0] = scan.nextLine();
System.out.println("Entre first home team score: ");
score[0] = scan.nextLine();
System.out.println("Entre second home team name: ");
name[1] = scan.nextLine();
System.out.println("Entre second home team score: ");
score[1] = scan.nextLine();
System.out.println("Entre third home team name: ");
name[2] = scan.nextLine();
System.out.println("Entre third home team score: ");
score[2] = scan.nextLine();
System.out.println("Entre forth home team name: ");
name[3] = scan.nextLine();
System.out.println("Entre forth home team score: ");
score[3] = scan.nextLine();
System.out.println("Entre fifth home team name: ");
name[4] = scan.nextLine();
System.out.println("Entre fifth home team score: ");
score[4] = scan.nextLine();
System.out.println("Entre sixth home team name: ");
name[5] = scan.nextLine();
System.out.println("Entre sixth home team score: ");
score[5] = scan.nextLine();
System.out.println("Entre seventh home team name: ");
name[6] = scan.nextLine();
System.out.println("Entre seventh home team score: ");
score[6] = scan.nextLine();
}
}
}
我不明白为什么要这样做。我可以想象这是一些简单的事情,只是我没有看到 fs。
最佳答案
使用
score[0] = scan.nextInt();
而不是
score[0] = scan.nextLine();
因为score[]是一个int[]数组。
还可以使用 .equals
来比较字符串。改变
opt == "Home teams"
至opt.equals("Home teams")
关于java - 类型不匹配 : cannot convert from String to int - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52746809/