JBTALKS.CC

标题: 请帮忙我!!! [打印本页]

作者: lmy5360    时间: 2009-8-8 04:09 PM
标题: 请帮忙我!!!
public class TV{
  private int channel;
  private int volume;
  private boolean flag;

//no-arg constructor
  public TV(){}

//default constructor
  public TV(int channel,int volume,boolean flag){
     this.channel=channel;
     this.volume=volume;
     this.flag=flag;
  }

  public int getChannel(){
     return channel;
  }

  public int getVolume(){
     return volume;
  }

  public boolean isOn(){
     return flag;
  }

  public void setChannel(int channel){
     this.channel=channel;
  }

  public void setVolume(int volume){
     this.volume=volume;
  }

  public void volumeUp(){
     this.volume+=1;
  }

  public void volumeDown(){
     this.volume-=1;
  }

  public void setFlag(boolean flag){
     this.flag=flag;
  }

  public String toString(){
     return ((flag) ? "TV is on" : "TV is off") +
             "\nChannel is " + channel + "\nVolume is "
             + volume;

  }
}





import java.util.Scanner;
public class testTV{
  public static void main(String[] args){


   //create object
   TV tv = new TV();
   Scanner input = new Scanner(System.in);

   System.out.print("do you want to switch the power ? On = 1 or Off = 0: ");

         //powerOnOff = input.nextBoolean();
         int power = input.nextInt();

      if (power == 1)
                           tv.setFlag(true);
                   else if(power == 0){
                  System.out.println("The tv is off ");
                           //tv.setPowerOnOff(false);
                   //else
                        System.exit(0);
                }


   System.out.print("Enter the TV channel between 0 to 99: ");

        int channel = input.nextInt();
        if (channel >=0 && channel <=99)
                        tv.setChannel(channel);
                else
                        System.exit(0);



   System.out.print("Enter the TV volume between 0 to 20: ");
         int volume = input.nextInt();
         if (volume >=0 && volume <=99)
                        tv.setVolume(volume);
                else
                        System.exit(0);


  // System.out.print("Pls increased the volume by unit: ");
  //        volume = input.nextInt();
  //       tv.setVolumeUp(volume);


  // System.out.print("Pls decreased the volume by unit: ");
  //        volume = input.nextInt();
  //       tv.setVolumeDown(volume);


   //tv.setPowerOnOff(powerOnOff);
   tv.setChannel(channel);
   tv.setVolume(volume);
   tv.volumeUp();
   tv.volumeDown();

   System.out.println(tv);
   System.out.println("\nThe channel's TV is: " + tv.getChannel());
   System.out.println("\nThe volume is : " + tv.getVolume());
   System.out.println("\nWhen the volume increased by 1 \n" + tv);
   System.out.println("\nWhen the volume decreased by 1 \n" + tv);
  }
}


为么 VOLUME dint increase & decrease!!!哪出了问题
作者: Super-Tomato    时间: 2009-8-8 09:44 PM
原帖由 lmy5360 于 2009-8-8 04:09 PM 发表
public class TV{
  private int channel;
  private int volume;
  private boolean flag;

//no-arg constructor
  public TV(){}

//default constructor
  public TV(int channel,int volume,bool ...



   tv.setVolume(volume); //這裡設定 volume
   tv.volumeUp(); //這裡 increase
   tv.volumeDown(); // 這裡 decrease

我想會數學的人 50 + 1 - 1 也知道最終答案




欢迎光临 JBTALKS.CC (https://www.jbtalks.cc/) Powered by Discuz! X2.5