表达式的类型必须是数组类型,但它解析为float [英] The type of the expression must be an array type but it resolved to float

查看:439
本文介绍了表达式的类型必须是数组类型,但它解析为float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我正在编写Java代码时,我遇到了麻烦。我觉得我不知何故搞砸了这个概念,比如我不确定:

I hit a bump when I'm doing my Java code. 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;
}

错误消息对应于sco [ID] =得分;

The error message corresponds to "sco[ID] = score; "

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

我很困惑我应该放在括号内,书要求我把float []得分而不是float [] sco,但是它不起作用,所以我经过几次试验后编辑了一下。这部分编码通常描述了存储5个主题的分数的重载方法。

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. This part of coding generally describes the method of overloading that stores the score for 5 subjects.

这是我的整个编码:

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!

推荐答案

您正在分配类变量的值得分到参数 sco 时应该执行反向。换句话说:

You are assigning a value of the class variable score to the parameter sco when you should do the opposite. In other words:

您的代码说:

sco = score;

但你应该做的是:

score = sco;

在这两个功能中,您需要切换<$ strong $的订单 c>得分和 sco ,以便得分获得 sco 。

In both functions you need to switch the order of score and sco so that score gets the value of sco.

至于你的错误,在 setScore(float sco,int id)您将参数 sco 定义为 float ,但您尝试将其作为数组访问(通过说 sco [Id] =得分)。这就是您收到错误消息的原因。

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). 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;

进入:

score[Id] = sco;

编辑:

至于这一部分:


我很困惑我应该放在括号中,书要求我把 float []得分>而不是float [] sco,但它不起作用,所以我在几次试验后编辑了一下。这部分编码一般描述了存储5>主题分数的重载方法。

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. This > part of coding generally describes the method of overloading that stores the score for 5 > subjects.

因为你想知道如何对参数和类变量使用相同的名称,如@Smutje所提到的,你应该使用关键字 this

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.

这样,您使用的得分没有歧义:

That way, there is no ambiguity with which score you are using:


  • this.score 属于你的班级名为得分并且可以在类中的任何函数中使用。它对课堂内的所有内容都是可见的。

  • this.score belongs to your class called Score and can be used in any function inside the class. It is visible to everything inside the class.

得分是函数本地的函数参数 setScore()并且只能在 setScore()中使用。它仅在函数内可见。

score is a function parameter local to a function setScore() and can only be used inside setScore(). It is only visible inside the function.

因此,包括所提及的所有内容,您应进行以下更改:

Therefore, including everything mentioned, you should make the following changes:

更改:

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

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

收件人:

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
}

这篇关于表达式的类型必须是数组类型,但它解析为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆