简体   繁体   English

如何在 Java 类中更改变量

[英]How do I change a Variable, in a Java Class

I have to be able to convert some variables in my class.我必须能够在我的班级中转换一些变量。 I have a boolean variable, WaGa (Stands for Workstation/Gaming computer), and if it's true, I want to convert String WorGam我有一个布尔变量 WaGa(代表工作站/游戏计算机),如果它是真的,我想转换 String WorGam

I have to do this through service and support methods, and I keep trying, but I constenly fail.我必须通过服务和支持方法来做到这一点,我一直在努力,但我经常失败。 It just prints out what's in the driver.它只是打印出驱动程序中的内容。 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);

You need to correct your worGam() function:您需要更正您的worGam()函数:

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

And the main() function: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));
}

You may need to reread you code to make sure there aren't any other mistakes in your code, but this is the root of your problem.您可能需要重新阅读代码以确保代码中没有任何其他错误,但这是问题的根源。

In order to access the WarGam getter, you need to call:为了访问WarGam getter,您需要调用:

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

When you do System.out.println(unique) , you are trying to print out the entire Graphics object instead of just the WarGam string.当您执行System.out.println(unique) ,您试图打印出整个Graphics对象,而不仅仅是WarGam字符串。

You then should change your WarGam() method to look like the following:然后,您应该将WarGam()方法更改为如下所示:

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

   return "Gaming";
}

Here is a more in depth explanation of the changes:以下是对这些变化的更深入的解释:

  1. WaGa is a private variable of your Graphics class. WaGaGraphics类的私有变量。 Since the WarGam() method is in the same Graphics class, it already had access to the WaGa variable, so you do not need to pass it in.由于WarGam()方法在同一个Graphics类中,它已经可以访问WaGa变量,因此您不需要传入它。
  2. if(WaGa == true) is just a wordier way of writing if(WaGa) . if(WaGa == true)只是if(WaGa)一种写法。
  3. Instead of creating a String WorGam variable, you can just return the string you want directly.您可以直接返回所需的字符串,而不是创建String WorGam变量。
  4. The else surrounding the second return is unnessary since that code will only be hit if the first return is skipped.围绕第二个returnelse是不必要的,因为只有在跳过第一个return才会命中该代码。

After these changes, the private String WarGam variable is really not necessary either.在这些更改之后, private String WarGam变量也确实没有必要了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM