变量的值在不同方法之间不会被存储(初学者 Java 项目)

huangapple go评论58阅读模式
英文:

value is not stored in variable between differente methods (beginner java project)

问题

我这里有两个方法,第一个方法生成一个学生 ID 并将其存储在一个变量中,这个方法能够正常工作并且值被存储了。但是在第二个方法中,当我从第一个方法中取出那个变量时,显示出来的值是 0。这里有什么问题?

附注:我之前使用了一个返回字符串的方法。

// 生成学生 ID
public int studentid(int grade) {
    System.out.println("现在我们将生成一个 ID 号码");

    int id = grade * 10000;
    int t = 1000;
    
    for (int i = 0; i < 4; i++) {
        int random = (int)(Math.random() * 9);
        random = random * t;
        id = id + random;
        t = t / 10;
    }
    System.out.println("您的 ID 号码是:" + id);
    return id;
}

// 显示姓名、ID、年级和已注册课程
public void getInfo() {
    System.out.println("学生的名字:" + FirstName);
    System.out.println("学生的姓氏:" + LastName);
    System.out.println("学生的年级:" + grade);
    System.out.println("学生的 ID 号码:" + id); 
    System.out.println("学生的课程:" + v); 
}
现在我们将生成一个 ID 号码
您的 ID 号码是:88222
学生的名字:luke
学生的姓氏:goob
学生的年级:8
学生的 ID 号码:0
学生的课程:null
英文:

I have here two methods, the first one generate an student Id and store it an a value, this method works perfectly and the value is stored, but in the second mehtod when I take that variable from the first method on show that it is 0, what is the problem here ?

p.s. I worked with another method that return a string

// generate a student id 

		public int studentid(int grade) {
			System.out.println(&quot;now we gonna gerate an id number &quot;);

			int id = grade * 10000;
			int t = 1000;
			 
			for(int i = 0; i&lt;4 ; i++) {
				int random = (int)(Math.random() * 9);
				random =  random * t;
				id = id + random;
				t = t / 10;
			}
			System.out.println(&quot;your id number is : &quot; + id);
			return id;

			
			
		}
	
	 
	 
			
	// show name , id , grade and course enrolled 
	public void getInfo() {
		System.out.println(&quot;Student&#39;s First name: &quot; + FirstName);
		System.out.println(&quot;Student&#39;s Last name: &quot; +  LastName);
		System.out.println(&quot;Student&#39;s grade year: &quot; + grade);
		System.out.println(&quot;Student&#39;s id number: &quot; + id); 
		System.out.println(&quot;Student&#39;s courses: &quot; + v); 

 	}

now we gonna gerate an id number 
your id number is : 88222
Student&#39;s First name: luke
Student&#39;s Last name: goob
Student&#39;s grade year: 8
Student&#39;s id number: 0
Student&#39;s courses: null

答案1

得分: 1

在方法studentid中,您定义了一个局部变量id。这会隐藏类实例变量id。该方法返回它计算的值,但并未将其赋值给实例变量。

您可以修改此方法,将计算得出的值分配给实例变量

public int studentid(int grade) {
  int id =
  ... 计算发生在这里...
  this.id = id;
  return id;
}

或者,调用此方法的人可以在实例变量上设置值。

public void someMethod(int grade) {
  this.id = studentid(grade);
}
英文:

In the method studentid you define a local variable id. This hides the class instance variable id. The method returns the value that it calculates but it does not assign it to the instance variable.

You could alter this method to assign the calculated value into the instance variable

public int studentid(int grade) {
  int id =
  ... calcuation happens...
  this.id = id;
  return id;
}

Or, whomever is invoking this method can then set the value on the instance variable.

public void someMethod(int grade) {
  this.id = studentid(grade);
}

huangapple
  • 本文由 发表于 2020年9月17日 18:58:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63936577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定