-2

我有一个只有 99 个频道和 20 个作为最高音量的电视课程,但是当我将音量和频道设置器添加到音量而不是添加 1 到 10(这是我的默认音量)时,我的音量和频道设置器也无法正常工作,它只添加一个到给定的音量数字我的电视课

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

    public TV(String onOffSwitch, int channel, int volume)
    {
        flag = onOffSwitch;
        ch = channel;
        vol = volume;
    }

    public void setFlag(String onOffSwitch)
    {
        onOffSwitch = "Off";
    }// End of setFlag

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

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

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

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

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

        if(newVolume < 0)
        {
            vol=0;
        }
    }// End of SetVolume

    public void incrementVolume()
    {
        vol++;
    }

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

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

}// End of TV class

我的试驾课

import java.util.Scanner;

public class TvTest
{
    public static void main (String[] args)
    {
        String tvStatus;
        int chan;
        int volu;

        Scanner input = new Scanner(System.in);
        TV tv2 = new TV("off",105,10);

        System.out.print(tv2);
        System.out.println();

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

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

        System.out.print("Volume : ");
        volu = input.nextInt();

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

        System.out.println(tv1);
    }
}

和测试输出

TV is switched :off
TV channel:105
TV volume :10
Turn TV On or Off ?on
Channel : 105
Volume : 1
TV is switched :on
TV channel:105
TV volume :2

为什么我的二传手不工作???

4

5 回答 5

2

您的构造函数应该使用您的设置器:

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

setFlag 应该设置flag为传入的值。

public void setFlag(String onOffSwitch)
{
    this.flag = onOffSwitch;
}// End of setFlag
于 2013-03-18T03:29:35.223 回答
1
public void setFlag(String onOffSwitch)
{
    onOffSwitch = "Off";
}// End of setFlag

onOffSwitch变量不是字段。那是参数列表中的名称,应该是flag

于 2013-03-18T03:24:13.717 回答
1
 public void setVol(int newVolume)
    {
        if(newVolume >= 20)
        {
            vol = 20;
        }

        if(newVolume < 0)
        {
            vol=0;
        }
    }// End of SetVolume

除非新值超出范围,否则该 setter 不会执行任何操作。

于 2013-03-18T03:36:51.867 回答
0

问题不在于您的 setter/getter(尽管它们读起来有点奇怪),问题是您在构造函数中忽略了它们...

public TV(String onOffSwitch, int channel, int volume)
{
    flag = onOffSwitch;
    ch = channel;
    vol = volume;
}

尝试使用类似的东西

public TV(String onOffSwitch, int channel, int volume)
{
    setFlag(flag);
    setCh(channel);
    setVol(volume);
}

反而...

该变量onOffSwitch也未定义,因此您的示例将无法编译;)。它应该读到类似...

public void setFlag(String onOffSwitch)
{
    flag = onOffSwitch;
}// End of setFlag

请注意,我可能会使用boolean值、拥有turnOnturnOff方法,但这只是我......

于 2013-03-18T03:30:14.290 回答
0

我一整天都在喝酒,所以请记住这一点,但我猜你也是,因为你无法完成这项基本任务。 p

首先,永远,永远,我的意思是永远使用字符串来表示两个状态值。布尔值很好,所以使用它们。

class Televsion {
    power = false; # off
    power = true;  # on
}

其次,你的变量名很烂。并不是说,它们之间存在相当多的匹配错误,这不必要地使程序复杂化。也不要使用缩写或简写变量名。如果这可能是 20 年或 10 年前,我可以理解,但现代编辑器往往具有自动完成功能,可以帮助一两次击键填写代码。

您似乎还没有了解关键字“this”。当您在类中使用“this”时,它会特异性地从类中调用特定的变量或方法。主要的收获是它可以让你避免任何来自参数和局部变量的名称冲突。

class Televsion {
    private boolean power;
    private int channel;
    private int volume;

    public Televsion(  boolean power, int channel, int volume ) {
        this.power = power;
        this.channel= channel;
        this.volume = volume;
    }
}

这样对读者来说更理智一点;P

在 main 方法中测试代码适用于新手和失败者。JUnit 现在几乎已经内置在 Java 中了,为什么不使用它,或者你可以使用 TestNG ^.^

构建测试套件可以更轻松地设计和返工代码。

您可能还想看看 Hamcrest。

public TelevsionTest {
    private Televsion televsion;

    @Before
    public void boilerplateSetUpCode() {
        // The before annonation has it's roots in apsectJ, I beleve.
        // In this context it makes this method run before every test.

        televsion = new Televsion();
    }

    @Test
    public void testSwitchOn() {
        televsion.switchOn();
        assertTrue( televsion.getPowerState() == true ); 
        # JUnits assertions are weak, that's why I like hamcrest.
    }
}

尝试编写可重用性和可维护性的代码。从长远来看,这可以让生活更轻松,但在短期内却是一个挑战。如果你得到有线电视或卫星电视,这会影响频道号码吗?

这是关于我如何重新解决问题的五分钟技巧。

公共类 Televsion { 私有 Logger4j 记录器;

private ChannelSource channelSource; // Naming is a little weak
private int volume;
private boolean power;

public Tv() {
    channelSource= channelSource.LOCAL;
    volume = 0;
    power = false;
}

public turnOn(){ power = true; }
public void turnOff() { power = false; }
public void muteVolume() { volume = 0; }
public void setVolume( volume ) {
    if ( volume > -1 && volume < 100 ) {
        this.volume = volume;
    } else {
        logger.dubug( String.format("Arg: %d", volume) );
        // This is anti-pattern of how not to word a debugging statement.
        // Plus, I'm lazy...
    }
}
public void changeChannelSource( ChannelSource channelSource ) {
    this.channelSource = channelSource;
}
public void changeChannelNumber( int channelNumber ) {
    channelSource.changeChannelNumber( channelNumber );
}

// I like singletons 
public enum ChannelSource {
    LOCAL(0), ALT(0), CABLE(0);

    private int channelNumber;

    private ChannelSource( int channelNumber ) {
        this.channelNumber = channelNumber ;
    }        


    public int getChannelNumber() { return channelNumber; }
    public void setChannelNumber( channelNumber ) {
        // Lazy hack
        if ( this.getName() == "LOCAL" ) {
            if ( channelNumber > -1 && channelNumber < 100 ) {
                this.channelNumber = channelNumber;
            } else {
                logger.debug( 
                    String.format("Unable to change %s, channel to %d", this.getName(), channelNumber) 
                );
            }
        }
    }
}

}

希望,如果您开始提高您的编码标准并开始学习良好的编程技术,那么您可能会喜欢足够的编码来创建一个实际的 Stack Exchange 帐户:)

这是一个简单的列表,你应该学习让你的 Java 老师开心的事情。

- Unit Testing
   + Might as well let a computer grade the code then a human, free A's

- Data Structures
   + Just using collections good enough.  
   + Side rant, If use this style of matrices, Object[][] I will haunt you down.
      - It's inefficient, plus hard for people maintaining this shitty code
      - Jagged arrays can work just as well List< List<Object> > or you can even just use a single array, [] and sprinkle in some magic.

- Annotations
   + Helps takes the Java out of Java

- Read Effective Java
   + It's not a difficult book, will make life easier.

- Know a little about concurrency
   + If you use the synchronized keyword, then you know something is wrong.
   + Take a look into concurrent utils API
   + Read Java Concurrency in Practice

- Spring
   + If you want to get paid for knowing Java

我确信还有很多其他的东西我会遗漏,但这对于任何开始学习 Java 的人来说已经足够了。

于 2013-03-18T05:32:36.010 回答