如何在不同方法中访问相同的变量?

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

How to access same variables in different methods?

问题

I'm a beginner and I'm facing the same issues on different projects.
我是个初学者,我在不同的项目中遇到了相同的问题。

I want to print the variable I got from my method
我想要打印我从我的方法中获取的变量

I mean, for example: randomNumber method will create random number and in other method I will print that randomNumber variable on print method.
我的意思是,例如:randomNumber方法将创建一个随机数,在另一个方法中,我将在print方法中打印该randomNumber变量。

Here is my code please help me.
这是我的代码,请帮助我。

I wasn't going to ask here but I'm stuck at this and I can't understand some other things like constructors so I feel overwhelmed I though it would be nice to get some help.
我本来不打算在这里问,但我卡在这个问题上,而且我不懂一些其他的东西,比如构造函数,所以我感到不知所措,我觉得得到一些帮助会很好。

英文:

I'm a beginner and I'm facing the same issues on different projects.
I want to print the variable I got from my method
I mean, for example: randomNumber method will create random number and in other method I will print that randomNumber variable on print method.
Here is my code please help me.
I wasn't going to ask here but I'm stuck at this and I can't understand some other things like constructors so I feel overwhelmed I though it would be nice to get some help.

package aviator;

import java.util.Random;

public class Main {
	private static int ucakrenk;
	public static void main(String[] args) {
		
		Random random = new Random();
		ucak mainucak = new ucak(7, 8, 9);
		
		ucakrenk();
		kusurat();
		test();

		}
	}

	private static void test() {
		// TODO Auto-generated method stub
		System.out.println("Siradaki ucak:");
		System.out.println("+-renk: " + ucakrenk);
	}

	private static void kusurat() {
		// TODO Auto-generated method stub
		Random random = new Random();
		int kusurat = random.nextInt(100);
		
	}

	public static void ucakrenk() {
		// TODO Auto-generated method stub
		Random random = new Random();
		int ucakrenk = random.nextInt(100);
		
	}
}

答案1

得分: 0

包 aviator;

import java.util.Random;

public class Main {
    private static int ucakrenk;
    public static void main(String[] args) {
        
        Random random = new Random();
        ucak mainucak = new ucak(7, 8, 9);
        
        int uca = ucakrenk();
        int kusa = kusurat();
        test(uca);
        test(kusa);

    }
}

private static void test(int ucakrenk) {
    // TODO 生成方法存根
    System.out.println("下一个飞机:");
    System.out.println("+-颜色: " + ucakrenk);
}

private static int kusurat() {
    // TODO 生成方法存根
    Random random = new Random();
    int kusurat = random.nextInt(100);
    return kusurat;
}

public static int ucakrenk() {
    // TODO 生成方法存根
    Random random = new Random();
    int ucakrenk = random.nextInt(100);
    return ucakrenk;
}
英文:
package aviator;

import java.util.Random;

public class Main {
    private static int ucakrenk;
    public static void main(String[] args) {
        
        Random random = new Random();
        ucak mainucak = new ucak(7, 8, 9);
        
        int uca = ucakrenk();
        int kusa kusurat();
        test(uca);
        test(kusa)

        }
    }

    private static void test(int ucakrenk) {
        // TODO Auto-generated method stub
        System.out.println("Siradaki ucak:");
        System.out.println("+-renk: " + ucakrenk);
    }

    private static int kusurat() {
        // TODO Auto-generated method stub
        Random random = new Random();
        int kusurat = random.nextInt(100);
        return kusurat
        
    }

    public static int ucakrenk() {
        // TODO Auto-generated method stub
        Random random = new Random();
        int ucakrenk = random.nextInt(100);
        return ucakrenk
        
    }
}

Code sample to "solve" your problem

I encourage you to check online what is variable scope. When you create a variable in a function in java, it's scope is the function itself, hence if you don't return it it gets "deleted" (to make it simple) after the function ends

What you need to do is "return" it (with the return keyword) and then assign it to a variable when you call the function

( int uca = ucakrenk(); for example)

Then pass it as an argument to your next function to reuse it with

 private static void test(int ucakrenk) { …

huangapple
  • 本文由 发表于 2023年6月6日 13:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411778.html
匿名

发表评论

匿名网友

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

确定