2013-03-20 42 views
-5

如何讓此程序停止並返回打開和關閉電視?布爾到Java中的字符串

如果選擇OFF,我希望它顯示電視機的當前狀態。

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方法,甚至我的二傳手

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 
+2

爲什麼你不開心toStri ng ?? – PermGenError 2013-03-20 11:22:54

+0

更正了你的setFlag方法...你的toString方法有什麼問題?它太慢了嗎?它不寫你所期望的? – LaGrandMere 2013-03-20 11:25:00

+1

我不明白你的問題到底是什麼。 – 2013-03-20 11:25:12

回答

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); 
+2

避免使用這樣的三元條件。這是糟糕的代碼,因爲對於測試/調試來說很難理解並且很糟糕。也沒有可能登錄。更好的方法是返回新的通道'public int getFilteredChannel(int channel)'並使用if(channel> = 99)返回99;',if(channel <0)返回1;'和'return channel'。與嵌套的三元條件方法不同,閱讀和理解更容易。代碼大小很重要,但並不符合可讀性。 – Eich 2013-03-20 12:12:48

+0

@da_re它確實是一個更可讀的選項。 – LaGrandMere 2013-03-20 14:59:38

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由鮑勃·馬丁在乾淨的代碼refrenced通用做法。

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

可能是......

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

最後...

if(flag == false) 

將是清潔只是做...

if(!flag) 
+0

事實上,對於tvStatus,它不是'if ... else',而是'if ... else if' ...所以在原始代碼中,如果它不是'on'或'off',那就是書面,沒有任何反應......所以我不確定你提出的是正確的。 – LaGrandMere 2013-03-20 11:27:52

+0

根據他發佈的代碼,它可以打開或關閉,所以你可以使用其他的。至少就他所發佈的代碼而言,可以重構爲如此,除非它具有「開」或「關」以外的其他值。 – david99world 2013-03-20 11:31:46

+0

這取決於他想做什麼......他掃描System.in。目前錯誤的順序,他決定什麼都不做,也不打開或關閉他的電視。它可以是一個相關的選擇。 – LaGrandMere 2013-03-20 11:35:04

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

我回答了原來的問題,與實際的問題有很大的不同。 : - / – Ollbert 2013-03-20 18:38:14