简体   繁体   English

错误:不兼容的类型:int 无法转换为 int[] 和其他错误

[英]error: incompatible types: int cannot be converted to int[] & other errors

import java.util.Scanner;

public class BasketBallChart {
    public static void main (String[] args) {
        Scanner reader = new Scanner(System.in);

        System.out.print("Enter the first names of the 5 players: ");
        String nameString = reader.nextLine();
        String[] name = nameString.split(" ");

        for (int i = 0; i < 5; i++) {
            System.out.print("Enter points earned by " +name[i]+ ": ");
            int pL = reader.nextInt();
            int p[i] = pL;
        }

        for (int j = 0; j < 5; j++) {
            System.out.println(p[j]);
        }
    }
}

So basically the first part asks the user to enter the names of 5 players (with spaces) which then I split into a string array.所以基本上第一部分要求用户输入 5 个玩家的名字(带空格),然后我将其拆分为一个字符串数组。

The for loop I was trying to make it so I wouldn't have to put 5 separate printlns and inputs, so it would be less lines and look cleaner.我试图制作的 for 循环,这样我就不必放置 5 个单独的 println 和输入,所以它的行数会更少,看起来更干净。 So I was trying to make it so each point would be associated with the number of the times the for loop has been run for example, say if "i" was 0 and I had entered Oliver for the first name in the first input, name[0] would be Oliver, which then I wanted it to be like the number of points you enter in the first for loop would then be p[0] therefore associated with Oliver, in such that they're both 0.所以我试图让每个点都与 for 循环的运行次数相关联,例如,如果“i”为 0,并且我在第一个输入中输入 Oliver 作为名字,name [0] 将是 Oliver,然后我希望它就像您在第一个 for 循环中输入的点数将是 p[0] 因此与 Oliver 相关联,因此它们都是 0。

I used the second for loop for testing purposes.我使用第二个 for 循环进行测试。

I keep getting errors and I am not sure why.我不断收到错误,我不知道为什么。

Change your code to将您的代码更改为

String[] name = nameString.split(" ");
int p[] = new int[5];

for (int i = 0; i < 5 && i < name.length; i++) {  // also check name array length
        System.out.print("Enter points earned by " +name[i]+ ": ");
        int pL = reader.nextInt();
        p[i] = pL;
}

This will move the declaration of the p array to before (and outside) of the for loop.这会将p数组的声明移动到for循环的之前(和外部)。 I have also added code to check that the looping does not exceed the length of the name array.我还添加了代码来检查循环不超过name数组的长度。

As suggested below by @RolandIllig, the array p would be better of being named as points and the name arrays would be better off named as names - giving variables names which reflect their usage makes for easier to understand code.正如@RolandIllig 下面所建议的那样,数组p最好命名为points ,而name arrays 最好命名为names - 提供反映其用法的变量名称有助于更容易理解代码。

Here you have two issues with your code:在这里,您的代码有两个问题:

1) You need to declare array of p before the loop like int[] p = new int[5]; 1)您需要在循环之前声明 p 数组,例如int[] p = new int[5];

2) Then inside loop assign value to each array element like p[i] = pL; 2) 然后在循环内部为每个数组元素赋值,如p[i] = pL;

3) For being safe please check for length of name array like if (name.length == 5) 3) 为了安全起见,请检查name数组的长度,例如if (name.length == 5)

    public static void main(final String[] args) {

    final Scanner reader = new Scanner(System.in);

    System.out.print("Enter the first names of the 5 players: ");
    final String nameString = reader.nextLine();
    final String[] name = nameString.split(" ");
    if (name.length == 5) {
      final int[] p = new int[5];
      for (int i = 0; i < 5; i++) {
        System.out.print("Enter points earned by " + name[i] + ": ");
        final int pL = reader.nextInt();
        p[i] = pL;
      }

      for (int j = 0; j < 5; j++) {
        System.out.println(p[j]);
      }
    }

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM