我必须能够在我的班级中转换一些变量。我有一个布尔变量,WaGa(Stands for Workstation / Gaming computer),如果是真的,我想转换String WorGam
我必须通过服务和支持方法来做到这一点,并且我一直在努力,但我总是失败。它只是打印出驱动程序中的内容。 HELP。
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 :(得分: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));
}
答案 1 :(得分:0)
您可能需要重新阅读代码以确保代码中没有任何其他错误,但这是您问题的根源。
要访问WarGam
获取者,您需要致电:
System.out.println(unique.WarGam());
当您执行System.out.println(unique)
时,您正在尝试打印整个Graphics
对象,而不仅仅是WarGam
字符串。
然后,您应该将WarGam()
方法更改为如下所示:
public String WorGam()
{
if (WaGa) {
return "Workstation";
}
return "Gaming";
}
以下是对更改的更深入解释:
WaGa
是Graphics
类的私有变量。由于WarGam()
方法属于同一个Graphics
类,因此它已经可以访问WaGa
变量,因此您无需将其传入。if(WaGa == true)
只是写作if(WaGa)
的一种更为冗长的方式。String WorGam
变量。else
周围的return
是不确定的,因为只有在跳过第一个return
时才会触及该代码。在这些更改之后,private String WarGam
变量实际上也不是必需的。
答案 2 :(得分:0)
public String worGam(boolean waGa) {
if (waGa)
return "Workstation";
else
return "Gaming";
}