0

整数を受け取り、配列から文字列値 (チャンネル名) を返す Television クラスのメソッドを作成する必要があります。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);
    }


}

}

4

1 に答える 1

2
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であり、型でなければなりませんStringTVChannel存在すらしないクラスです。

編集:これも考慮してください

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

}
于 2013-11-12T03:11:53.667 に答える