2016-09-26 79 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"; 
} 
相关问题