简体   繁体   English

问题与字符串作为参数

[英]Issue with String as parameter

First of, sorry for my bad English. 首先,对不起我的英语不好。 I'm pretty new to java and we are learning it in School at the moment. 我对Java很陌生,目前我们正在School中学习它。 In my Program I want to use String as a parameter but when I try to build an object I can't type anything in for the Parameter. 在我的程序中,我想使用String作为参数,但是当我尝试构建对象时,无法在Parameter中输入任何内容。 Here's my bytecode: 这是我的字节码:

public String gender;
public Waage(String pGender)
{
    gender = pGender;
}

As you can see I want the user to type in their gender, the variable gender will then be set to be the same as the parameter pGender. 如您所见,我希望用户输入性别,然后将变量性别设置为与参数pGender相同。 If I want to create an object with Bluej now, I just get the error that it cannot find symbol - variable x (for example female). 如果我现在想用Bluej创建一个对象,我会得到一个错误,它找不到符号-变量x(例如female)。 Could someone please explain to me what I'm doing wrong or how to fix it? 有人可以向我解释我在做什么错或如何解决吗? Thank you! 谢谢! Also this is my first question here so if I did any big mistakes just tell me. 这也是我的第一个问题,因此,如果我犯了重大错误,请告诉我。

Edit: My full code: 编辑:我的完整代码:

public class Waage
{ 
   public String gender;

   public Waage(String pGender)
   {
       gender = pGender;
   }

   public void changeGender(String pGender)
   {
       gender = pGender;
   }

   public String giveGender()
   {
       return gender;
   }
}

And the Error Message is If I would type in female : Error: cannot find symbol - variable female 错误消息是:如果我要输入female:错误:找不到符号-变量female

In the constructor, 在构造函数中

public Waage(pGender)
{
    gender = pGender;
}

pGender is local variable of the constructor, that is taking String argument, and it is missing datatype. pGender是构造函数的局部变量,采用String参数,并且缺少数据类型。 Edit that as below and it'll compile. 如下编辑,它将编译。

 public Waage(String pGender)
 {
     gender = pGender;
 }

One of the cause is not passing string in double quotes in constructor call. 原因之一是未在构造函数调用中用双引号传递字符串。

You must be creating your class's object like this: 您必须像这样创建类的对象:

Waage waage = new Waage(female);

Either use : 可以使用:

Waage waage = new Waage("female");

or: 要么:

String gender = "female";
Waage waage = new Waage(gender);

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

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