【问题标题】:Method Needs to return a value from a string array方法需要从字符串数组中返回一个值
【发布时间】:2013-11-12 03:09:32
【问题描述】:

我需要为我的电视类创建一个方法,该方法将接收一个整数,但会从一个数组中返回一个字符串值(频道名称)。我在 toString() 中遇到问题,我在应该显示频道名称的部分收到错误“error-'.class' expected'。谢谢,这是我的代码。

public class Television

{

private int channel;
private int currentChannel;
private int volume;
private boolean power;
private String[] channelName = {"CBS", "FOX", "DISCOVERY", "PBS", "HBO", "CNN", "DISNEY", "CNN", "TBS", "USA"};



//No argument constructor
public Television()
{
    currentChannel = 1;
    volume = 0;
    power = false;

}


public void powerChange()
{
    this.power = !this.power;
}

public void setVolume(int vol)
{
    if (vol>10)
    {
    volume = 10;
    }else
    {
        volume = vol;
    }
    if (vol<0)
    {
    volume = 0;
    }

}

public void increaseVolume()
{
    volume++;
}

public void decreaseVolume()
{
    volume--;
}

public int getVolume()
{
    return volume;
}

public void setChannel(int ch)
{
    if(ch>10)
    {
    channel = 10;
    }else
    {
        channel = ch;
    }
    if(ch<1)
    {
    channel = 1;
    }
}

public void increaseChannel()
{
    channel++;
}

public void decreaseChannel()
{
    channel--;
}

public int getChannel()
{
    return channel;
}

public String getChannelName(int channel)
{

    if (channel==1)
    {
        return channelName[0];
    }
    else if (channel == 2)
    {
        return channelName[1];
    }   
    else if (channel == 3)
    {
        return channelName[2];
    }   
    else if (channel == 4)
    {
        return channelName[3];
    }   
    else if (channel == 5)
    {
        return channelName[4];
    }   
    else if (channel == 6)
    {
        return channelName[5];
    }   
    else if (channel == 7)
    {
        return channelName[6];
    }
    else if (channel == 8)
    {
        return channelName[7];
    }   
    else if (channel == 9)
    {
        return channelName[8];
    }   
    else if (channel == 10)
    {
        return channelName[9];
    }   


}


public String toString()
{
    if(!power)
    {
        return String.format("%s :%s\n%s :%d\n %s :%s\n%s :%d", "TV State", "OFF", "Channel No", channel, "Channel Name", getChannelName(channel), "Volume", volume);
    }
    else if(power)
    {
        return String.format("%s :%s\n%s :%d\n %s :%s\n%s :%d", "TV State", "ON", "Channel No", channel, "Channel Name", getChannelName(channel), "Volume", volume);
    }


}

}

【问题讨论】:

  • 你认为这段代码有什么作用:getchannelName[]?
  • 我的原始代码中没有 getchannelName[],但我尝试使用 toString() 只是为了看看是否有任何帮助。我试图调用我之前创建的方法,但我意识到这不起作用。

标签: java arrays string methods


【解决方案1】:
TVChannel[] channelName = {"CBS", "FOX", "DISCOVERY", "PBS", "HBO", 
                           "CNN", "DISNEY", "CNN", "TBS", "USA"};

应该是

String[] channelName = {"CBS", "FOX", "DISCOVERY", "PBS", "HBO", 
                        "CNN", "DISNEY", "CNN", "TBS", "USA"};

您的数组是TVChannel type 并且应该是String 类型。 TVChannel 是一个根本不存在的类。

编辑:也考虑

channelName = new TVChannel[MaxChannel];  // delete this from your constructor.
                                          // it is not needed. Your instance of
                                          // the class already give you array

还要考虑您的getChannelName() 方法。也许你想要一个int 参数?

public String getChannelName(int channel){ // instead of using the instance var
                                           // the logic really doesn't make sense
                                           // for your situation

}

【讨论】:

  • 或者它需要列出 OP 实际需要的 TVChannel 的实例;)
  • 谢谢。我已更改此设置,但仍收到相同的错误消息。
  • @user2981579,你的构造函数中不需要这段代码channelName = new TVChannel[MaxChannel];
  • @peeskillet 谢谢,这确实更有意义。关于如何在 toString 方法中引用它的任何提示?我仍然从中得到错误。
  • @user2981579 getchannelName[]。你想要getChannel(channel) 在你的toString() 中。你遇到了什么错误?只是在toString() 中吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 2014-08-17
  • 2011-08-28
  • 2018-12-16
  • 2017-06-23
  • 1970-01-01
相关资源
最近更新 更多