java - 方法需要从字符串数组返回一个值

标签 java arrays string methods

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

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


}

}

最佳答案

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 类型,并且应该是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

}

关于java - 方法需要从字符串数组返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19920162/

相关文章:

java - 将启用工作和测试的 Selenium 键盘操作

java - Android中的字节数据类型内存大小

java - java类在内存中驻留的时间是多少

java - ArrayIndexOutOfBoundsExecption :Android

c# - Stringbuilder 从多个数组追加表单

java - 如何将字符串数组分组到 map ?

java - 在 JSON 数组中查找 JSON 对象

arrays - 如何在 typescript 中使用具有对象属性的数组

c - 在 C 中删除标点符号和大写

c++ - 字符串 "companion types"