2016-09-26 37 views
-2

我必須能夠在一定變量轉換在我的課。我有一個布爾變量,WaGa(代表工作站/遊戲電腦),如果它是真的,我想轉換字符串WorGam如何改變一個變量,在一個Java類

我必須通過服務和支持方法來做到這一點,我一直試圖,但我堅持失敗。它只是打印出驅動程序中的內容。幫幫我。

public class Graphics 
//instance data 
{ 
    private int Ram; 
    private String Brand; 
    private int Res; 
    private int BiWi; 
    private int BaCl; 
    private boolean K4; 
    private boolean WaGa; 
    private String WorGam; 

    //boolean WaGa, boolean K4, int BaCl, int BiWi, int Res, String Brand, int Ram 


    public Graphics (int R, String B, int Re, int Bi, int Ba, boolean K4, boolean Wa, String Wor)  // constructor 
    { 
     Ram = R; 
     Brand = B; 
     Res = Re; 
     BiWi = Bi; 
     BaCl = Ba; 
     K4 = K4; 
     WaGa = Wa; 
     Wor = WorGam; 
    } 

    public int get_Ram() //Accessor Method - there are 3 of them 
    { 
     return Ram; 
    } 

    public String get_Brand() //Accessor Method - there are 3 of them 
    { 
     return Brand; 
    } 

    public int get_Res() //Accessor Method - there are 3 of them 
    { 
     return Res; 
    } 

    public int get_BiWi() //Accessor Method - there are 3 of them 
    { 
     return BiWi; 
    } 

    public int get_BaCl() 
    { 
     return BaCl; 
    } 

    public boolean get_K4() 
    { 
     return K4; 
    } 

    public String WorGam(boolean WaGa) 
    { 
     String WorGam; 
     if (WaGa == true) { 
      return WorGam = "Workstation"; 
     } else { 
      return WorGam = "True"; 
     } 
    } 

    public String toString() 
    { 
     return ("Ram" + " " + Ram + ". " + "Brand:" + " " + Brand + ". " + "Resolution" + " " + Res + ". " + "Processer" + " " + BiWi + "." + " " + "Base Clock" + " " + BaCl+ " " + "K4?" + " " + K4+ " " +WorGam); 
    } 
} 

public class Graphicse_Driver 
{ 
    public static void main(String [] args) 
{ 

Graphics unique=new Graphics(4, "Nvinda", 6, 7, 9, false, false, "sdf" ); 

System.out.println(unique); 
+0

老實說,我不知道你在問什麼。 「改變變量」是什麼意思? 「轉換某些變量」是什麼意思?什麼是「服務和支持方法」?描述「失敗」意味着什麼 - 你期望看到什麼,而你看到了什麼? – VGR

+0

我想打印WorGam,如果賀是真的,那麼WorGam打印「工作站」,如果假的,它打印「遊戲」 –

+0

聽起來就像if語句。你真的應該使用適當的變量和方法命名約定。 –

回答

0

您可能需要重讀你的代碼,以確保沒有任何其他代碼中的錯誤,但這是你問題的根源。

爲了訪問WarGam消氣,你需要調用:

System.out.println(unique.WarGam()); 

當你做System.out.println(unique),你要打印出整個Graphics對象,而不是僅僅是WarGam字符串。

然後你應該改變你的WarGam()方法如下所示:

public String WorGam() 
{ 
    if (WaGa) { 
     return "Workstation"; 
    } 

    return "Gaming"; 
} 

這裏的變化有更深入的解釋:

  1. WaGa是你Graphics類的私有變量。由於WarGam()方法是在同一個Graphics類,它已經有了進入WaGa變量,所以你不需要它傳遞。
  2. if(WaGa == true)只是寫if(WaGa)的wordier方式。
  3. 而是創建一個String WorGam可變的,你可以回到你直接要的字符串。
  4. 圍繞第二returnelse是unnessary因爲如果第一return被跳過該代碼將只擊中。

經過這些更改後,private String WarGam變量實際上也不是必需的。

0

您需要更正您的worGam()功能:

public String worGam(boolean waGa) { 
    if (waGa == true) 
     return "Workstation"; 
    else 
     return "True"; 
} 

而且main()功能:

public static void main(String [] args) { 

    Graphics unique = new Graphics(4, "Nnn", 6, 7, 9, false, false, "xxx"); 

    System.out.println(unique.WorGam(false)); 
} 
+0

仍然打印相同的東西,不成問題。我需要它來打印以太網工作站,或者是真的,但它會一直打印我的佔位符,無論我是否使用了你的答案。 –

+0

@ZachArmstrong所以你需要修改main()函數。檢查我編輯的答案。 –

0
public String worGam(boolean waGa) { 
    if (waGa) 
     return "Workstation"; 
    else 
     return "Gaming"; 
}