简体   繁体   English

表达式的类型必须是数组类型,但已解析为浮点型

[英]The type of the expression must be an array type but it resolved to float

I hit a bump when I'm doing my Java code. 在执行Java代码时遇到了麻烦。 I feel like I somehow got the concept messed up, like I not sure for this: 我觉得我某种程度上弄乱了这个概念,就像我不确定这一点:

void setScore(float[] sco)
{
    sco = score;
}

public void setScore(float sco, int id)
{
    sco[id] = score;
}

The error message corresponds to "sco[ID] = score; " 错误消息对应于“ sco [ID] = score;”

The type of the expression must be an array type but it resolved to float

I'm confused what I should put in the bracket, the book asks me to put "float[] score" instead of "float[] sco", but it doesn't work, so I edited a bit after several trials. 我对应该放在括号中的内容感到困惑,这本书要求我放入“ float []分数”而不是“ float [] sco”,但是它不起作用,因此在几次试验后我进行了一些编辑。 This part of coding generally describes the method of overloading that stores the score for 5 subjects. 编码的这一部分通常描述了过载的方法,该方法存储5个主题的分数。

And this is my whole coding: 这是我的整个编码:

public class Score {
float math, english, physics, chemistry, biology;
float sum, average;
float[] score;
int id;

void setMath(float Math) {
    math = Math;
}

void setEnglish(float English) {
    english = English;
}

void setPhysics(float Physics) {
    physics = Physics;
}

void setChemistry(float Chemistry) {
    chemistry = Chemistry;
}

void setBiology(float Biology) {
    biology = Biology;
}

void setSum(float Sum) {
    sum = math + english + physics + chemistry + biology;
}

void setAverage(float Average) {
    average = sum / 5;
}

float getMath() {
    return math;
}

float getEnglish() {
    return english;
}

float getPhysics() {
    return physics;
}

float getChemistry() {
    return chemistry;
}

float getBiology() {
    return biology;
}

float getSum() {
    return sum;
}

float getAverage() {
    return average;
}

public void setScore(float[] sco)
{
    sco = score;
}

public void setScore(float sco, int id)
{
    sco[id] = score;
}
}

Now my problem solved! 现在我的问题解决了! Since I just changed like this: 由于我只是这样更改:

    public void score()
{

}
public void setScore(float[] score)
{
    sco = score;
}

Can anyone tell me why now the problem resolved? 谁能告诉我为什么现在问题解决了? Will really appreciate a lot! 真的会非常感激!

You are assigning a value of the class variable score to the parameter sco when you should do the opposite . 当您应该执行相反操作时,您正在将类变量score的值分配给参数sco In other words: 换一种说法:

Your code says: 您的代码说:

sco = score;

But what you should do is: 但是您应该做的是:

score = sco;

In both functions you need to switch the order of score and sco so that score gets the value of sco . 在这两个函数中,您都需要切换 scoresco 的顺序 ,以便score获得sco的值。

As far as your error, in setScore(float sco, int id) you defined parameter sco as a float but you are trying to access it as an array (by saying sco[Id] = score ). 就您的错误而言,在setScore(float sco, int id)您将参数sco定义为float但您尝试将其作为数组访问(通过说sco[Id] = score )。 That is why you are getting your error message. 这就是为什么您收到错误消息的原因。

The type of the expression must be an array type but it resolved to float

Like I said, you can fix this by switching the order again: 就像我说的,您可以通过再次切换顺序来解决此问题:

sco[Id] = score;

Into: 成:

score[Id] = sco;

EDIT: 编辑:

As far as this part: 就这一部分而言:

I'm confused what I should put in the bracket, the book asks me to put "float[] score" > instead of "float[] sco", but it doesn't work, so I edited a bit after several trials. 我对应该放在括号中的内容感到困惑,这本书要求我放入“ float []分数”>而不是“ float [] sco”,但是它不起作用,因此在进行了几次试验后我进行了一些编辑。 This > part of coding generally describes the method of overloading that stores the score for 5 > subjects. 这部分编码通常描述了重载方法,该方法存储了5个主题的分数。

Since you wanted to know how to use the same names for parameters and class variables, as @Smutje mentioned, you should use the keyword this . 由于您想知道如何对参数和类变量使用相同的名称(如@Smutje所述),因此应使用关键字this

That way, there is no ambiguity with which score you are using: 这样,您使用的score就不会有任何歧义:

  • this.score belongs to your class called Score and can be used in any function inside the class. this.score属于您的名为Score的类,可以在该类内的任何函数中使用。 It is visible to everything inside the class. 它对于班级中的所有内容都是可见的。

  • score is a function parameter local to a function setScore() and can only be used inside setScore() . score是函数setScore()局部函数参数,只能在setScore()内部使用。 It is only visible inside the function. 它仅在函数内部可见。

Therefore, including everything mentioned, you should make the following changes: 因此,包括所有提及的内容,您应该进行以下更改:

Change: 更改:

public void setScore(float sco, int id)
{
    sco[id] = score;
}

void setScore(float[] sco)
{
    sco = score;
}

To: 至:

public void setScore(float score, int id) // changed sco to score
{
    this.score[id] = score;   // switched order, added keyword this
}

void setScore(float[] score) // changed sco to score
{
    this.score = score;  // switched order, added keyword this
}

I guess you mixed up the variable names: 我猜你把变量名混在一起了:

void setScore(float[] sco)
{
    sco = score;
}

public void setScore(float sco, int id)
{
    sco[id] = score;
}

because score is the field which you want to populate and sco is the parameter which you want to populate score with. 因为score是您要填充的字段,而sco是您要用来填充score的参数。 The code above does not change any contents of Score , so try to swap it to 上面的代码不会更改Score任何内容,因此请尝试将其交换为

void setScore(float[] sco)
{
    this.score = sco;
}

public void setScore(float sco, int id)
{
    this.score[id] = sco;
}

it also would have helped if you have started using this to mark instance fields explicitly. 如果您已开始使用this显式标记实例字段,那么它也会有所帮助。

Check again how you set your score: 再次检查如何设置分数:

void setScore(float[] sco)
{
    sco = score;
}

What is sco ? 什么是sco What you want to assign to what? 您想分配给什么?

You are assigning your local variable to the parameter. 您正在将局部变量分配给参数。 It should be the other way around. 情况应该相反。 (this.score = sco)

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

相关问题 表达式的类型必须是数组类型,但它解析为SoapObject - The type of the expression must be an array type but it resolved to SoapObject 表达式的类型必须是数组类型,但已解析为TileSet - The type of the expression must be an array type but it resolved to TileSet 表达式的类型必须是数组类型,但已解析为JSONArray - The type of the expression must be an array type but it resolved to JSONArray 表达式的类型必须是数组类型,但它解析为 By - The type of the expression must be an array type but it resolved to By 表达式的类型必须是数组类型,但必须解析为Object - The type of an expression must be an array type, but it is resolved to Object 表达式的类型必须是数组类型,但解析为long - The type of the expression must be an array type but it resolved to long 表达式的类型必须是数组类型,但它解析为 Espectador - The type of the expression must be an array type but it resolved to Espectador 表达式的类型必须是数组类型,但解析为Card - The type of the expression must be an array type but it resolved to Card 表达式的类型必须是数组类型,但解析为Imageview - the type of the expression must be an array type but it resolved to Imageview 表达式的类型必须是数组类型,但是可以解析为字符串? - The type of expression must be an array type but it is resolved to a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM