-5

如何使该程序停止并返回以打开和关闭电视?

如果选择关闭,我希望它显示电视的当前状态。

import java.util.Scanner;

public class TvTest
{
    public static void main (String[] args)
    {
        String x;
        String y;
        boolean tvStatus = false;
        int chan;
        int volu;

        Scanner input = new Scanner(System.in);
        TV tv2 = new TV(false,2,10);

        // Print out the current status of our TV
        System.out.print(tv2);
        System.out.println();

             System.out.print("Turn TV On or Off ?");
             x = input.nextLine();

             if(x.equalsIgnoreCase("on"))
               {
                   tvStatus = true;

               }else if(x.equalsIgnoreCase("off"))
               {
                   tvStatus =false;
               }

            System.out.print("Change the Channel to : ");
            chan = input.nextInt();

            System.out.print("Increase the volume by 1 or Decrease by -1 : ");
            volu = input.nextInt();

            TV tv1 = new TV(tvStatus,chan,volu);

            if(volu == 1)
            {
                tv1.incrementVolume();

            }else if (volu == -1)
            {
                tv1.decrementVolume();
            }

            System.out.println(tv1);


    }
}// ENd of TvTest

这是我的电视课,我认为应该有办法改进 toString 方法甚至我的 setter

public class TV
{
    private boolean flag = false;
    private int ch;
    private int vol = 10;

    public TV(boolean onOffSwitch, int channel, int volume)
    {
        this.setFlag(onOffSwitch);
        this.setCh(channel);
        this.setVol(volume);
    }

    public void setFlag(boolean onOffSwitch)
    {
        if(onOffSwitch == true)
        {
            flag = true;
        }else
        {
            flag = false;
        }

    }// End of setFlag

    public boolean getFlag()
    {
        return flag;
    }// End of getFlag

    public void setCh (int newChannel)
    {
        if (newChannel >= 99)
        {
            ch = 99;
        }else
        {
            ch = newChannel;
        }

        if(newChannel < 0)
        {
            ch = 1;
        }
    }//end of setCh

    public int getCh ()
    {
        return ch;
    }// End of getCh

    public void setVol(int newVolume)
    {
        if(newVolume >= 20)
        {
            vol = 20;
        }

    }// End of SetVolume

    public void incrementVolume()
    {
        vol++;
    }

    public void decrementVolume()
    {
        vol--;
    }

    public int getVol()
    {
        return vol;
    }// ENd of getVolume

    public String toString()
    {
        if(flag == false)
        {
            return String.format("%s :%s\n%s:%d\n%s :%d","TV is switched","OFF","TV channel",ch,"TV volume",vol);
        }else
        {
            return String.format("%s :%s\n%s:%d\n%s :%d","TV is switched","ON","TV channel",ch,"TV volume",vol);
        }

    }

}// End of TV class
4

4 回答 4

3

首先,这个:

public void setFlag(boolean onOffSwitch)
    {
        if(onOffSwitch == true)
        {
            flag = true;
        }else
        {
            flag = false;
        }

    }

应该 :

public void setFlag(boolean onOffSwitch)
    {
        flag = onOffSwitch;
    }

然后 :

public void setCh (int newChannel)
    {
        if (newChannel >= 99)
        {
            ch = 99;
        }else
        {
            ch = newChannel;
        }

        if(newChannel < 0)
        {
            ch = 1;
        }
    }//end of setCh

应该 :

ch = (newChannel>=99) ? 99 : ((newChannel<0) ? 1 : newChannel);
于 2013-03-20T11:23:25.337 回答
2
 public void setFlag(boolean onOffSwitch)
    {
        if(onOffSwitch == true)
        {
            flag = true;
        }else
        {
            flag = false;
        }

    }// End of setFlag

可能...

 public void setFlag(final boolean onOffSwitch)
        {
          flag = onOffSwitch;

        }// End of setFlag

其次,你有同样的问题......

if(x.equalsIgnoreCase("on"))
               {
                   tvStatus = true;

               }else if(x.equalsIgnoreCase("off"))
               {
                   tvStatus =false;
               }

可能...

tvStatus = x.equalsIgnoreCase("on");

将您的方法参数final作为 Bob Martin 在干净代码中引用的一般实践。

if (newChannel >= 99)
        {
            ch = 99;
        }else
        {
            ch = newChannel;
        }

可能...

ch = (newChannel>=99) ? 99 : newChannel;

最后...

if(flag == false)

这样做会更干净...

if(!flag)
于 2013-03-20T11:25:38.213 回答
1

toString() 方法的小修改。请找到以下代码片段:

 if(flag == false)
    {
        return String.format("%s :%s\n%s:%d\n%s :%d","TV is switched","OFF","TV channel",ch,"TV volume",vol);
    }else
    {
        return String.format("%s :%s\n%s:%d\n%s :%d","TV is switched","ON","TV channel",ch,"TV volume",vol);
    }

您可以直接检查标志而不是flag==false.

 if(!flag)
    {
        return String.format("%s :%s\n%s:%d\n%s :%d","TV is switched","OFF","TV channel",ch,"TV volume",vol);
    }else if(flag)
    {
        return String.format("%s :%s\n%s:%d\n%s :%d","TV is switched","ON","TV channel",ch,"TV volume",vol);
    }
于 2013-03-20T11:52:43.217 回答
0

首先,您应该消除重复返回:

public String toString()
{
    String onOrOff = flag ? "ON" : "OFF";
    return String.format("%s :%s\n%s:%d\n%s :%d","TV is switched",onOrOff,"TV channel",ch,"TV volume",vol);
}

其次,我不喜欢 C 风格的字符串形式:

public String toString()
{
    String onOrOff = flag ? "ON" : "OFF";
    String result = "TV is switched :" + onOrOff + '\n'
        + "TV channel:" + ch + '\n'
        + "TV volume :" + vol;
    return result;
}

第三,您可以定义一个枚举:

enum Power { ON, OFF };
Power onOrOff = Power.ON;

public String toString()
{
    String result = "TV is switched :" + onOrOff + '\n'
        + "TV channel:" + ch + '\n'
        + "TV volume :" + vol;
    return result;
}
于 2013-03-20T18:24:55.330 回答