简体   繁体   English

Java错误,必需:char []找到char

[英]Java Error, required: char[] found char

I have made a character array in Java, 我用Java制作了一个字符数组,

char[] letterGrade = { 'A','B','C','D','F'};

and am trying to reference the elements like this, 并试图引用这样的元素,

letterGrade[0]

I am getting an error that says incompatible types 我收到一条错误消息,指出类型不兼容

required: char[] 必填:char []

found: char 找到:char

I've done this elsewhere in the code with an array of doubles and it worked fine. 我已经在代码中的其他地方使用双精度数组完成了此操作,并且效果很好。 Why doesn't it work with a character array? 为什么它不能与字符数组一起使用?

Thanks! 谢谢!

update from your comment 根据您的评论更新

public static void method()
{
    double totalEarnedPoints = 1;
    double totalPossiblePoints = 1;

    double gradePer = 0.0;
    gradePer = (totalEarnedPoints / totalPossiblePoints);
    char[] letterGrade = { 'A', 'B', 'C', 'D','F'};
    if (gradePer >= gradeScale[0])
    {
        letterGrade = char letterGrade(0);
    }
    else if (calcPercent >= gradeCutoffs[1] && calcPercent < gradeCutoffs[0])
    {
        letterGrade= char letterGrade(1);
    }
}

"I'm trying to implement it with an if statement, “我正在尝试使用if语句来实现它,

char[] letterGrade = { 'A', 'B', 'C', 'D','F'}; 
if (calcPercent >= gradeCutoffs[0]) { 
      letterGrade = letterGrade[0];
}

If you are doing so. 如果这样做的话。 Since letterGrade is of type char[] , you can't assign it with only char. 由于letterGrade的类型为char[] ,因此不能仅使用char进行分配。 LHS value must be a char. LHS值必须为char。

The posted sample works if your target variable is of type char, so I assume that you are doing something wrong in the assignment. 如果您的目标变量的类型为char,那么发布的示例将起作用,因此我认为您在分配中做错了什么。 Maybe you are calling a function which requires a char[] in wich case you should use funct(letterGrade) without the [0] . 也许您正在调用一个需要char[]的函数,在这种情况下,您应该使用funct(letterGrade)而不使用[0]

If your problem persists you should post a bit more code. 如果问题仍然存在,则应发布更多代码。

Update 更新

I fixed your code to make it compile. 我修复了您的代码以使其可编译。 of course I don't know what it is supposed to be so I had to make some assumptions, but you should see how the syntax is supposed to be. 当然我不知道应该是什么样,所以我必须做一些假设,但是您应该了解语法应该是什么样。

public static void method()
{
    double totalEarnedPoints = 1;
    double totalPossiblePoints = 1;
    double gradeScale[] = {1, 2, 3};
    double gradeCutoffs[] = {1, 2, 3};
    double gradePer = 0.0;
    char letter;
    double calcPercent = 1;

    gradePer = (totalEarnedPoints / totalPossiblePoints);
    char[] letterGrade = { 'A', 'B', 'C', 'D','F'};
    if (gradePer >= gradeScale[0])
    {
        letter = letterGrade[0];
    }
    else if (calcPercent >= gradeCutoffs[1] && calcPercent < gradeCutoffs[0])
    {
        letter = letterGrade[1];
    }
    else
        letter = 'F';

    System.out.println("Grade:"+letter);
}

This works fine for me: char[] letterGrade = { 'A','B','C','D','F'}; 这对我来说很好用:char [] letterGrade = {'A','B','C','D','F'}; System.out.println(letterGrade[0]); System.out.println(letterGrade [0]); It outputs A 输出A

So I suspect it is what you are assigning it to. 所以我怀疑这就是您要分配给它的东西。 Perhaps somewhere you have: letterGrade = letterGrade[A] 也许您在某个地方:letterGrade = letterGrade [A]

required: char[]

found: char 

this type of error occurs , if you are assigning a char in char[] datatype. 如果您要在char []数据类型中分配char,则会发生此类错误。

ex: 例如:

char[] ch = letterGrade[0];

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

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