将void方法的值获取到另一个方法中 [英] Get values of a void method into another method

查看:107
本文介绍了将void方法的值获取到另一个方法中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Demo {

	public static String EvenOrOdd(int n) {
		String s = "";

		if (n % 2 == 0) {
			s = n + " is an Even number";
		} else {
			s = n + " is an Odd number";
		}
		return s;
	}

	public static String isPerfect(int n) {
		int sum = 0;
		String s = "";

		for (int i = 1; i < n; i++) {
			sum += i;

		}
		if (sum == n) {
			s = n + " is a perfect number";
		} else {
			s = n + " is not a perfect number";
		}

		return s;

	}

	public static int gcd(int a, int b) {
		int gcd = 1;
		int k = 2;

		while (k <= a && k <= b) {
			if (a % k == 0 && b % k == 0)
				gcd = k;
			k++;
		}

		return gcd;
	}

	public static int lcm(int a, int b) {
		int x;
		x = (a > b) ? a : b;
		while (true) {
			if (x % a == 0 && x % b == 0)
				return x;
			++x;
		}

	}

	public static String Palindrome(int n) {
		int palindrome = n;
		int reverse = 0;
		String s = "";

		while (palindrome != 0) {
			int remainder = palindrome % 10;
			reverse = reverse * 10 + remainder;
			palindrome = palindrome / 10;
		}

		if (n == reverse) {
			s = n + " is a Palindrome";
		} else {
			s = n + " is not a Palindrome";
		}
		return s;
	}

	public static int Reverse(int n) {
		int reverse = 0;

		while (n != 0) {
			reverse = reverse * 10;
			reverse = reverse + n % 10;
			n = n / 10;
		}

		return reverse;

	}

	public static int SumDigits(int n) {
		int sum = 0;

		while (n != 0) {
			sum += n % 10;
			n /= 10;
		}
		return sum;

	}

	public static String Prime(int n) {
		int j = 2;
		int result = 0;
		String s = "";

		while (j <= n / 2) {
			if (n % j == 0) {
				result = 1;
			}
			j++;
		}
		if (result == 1) {
			return s = n + " is not Prime";
		} else {
			return s = n + " is Prime";
		}

	}

	public static void CallRectangleMethod() {
		int width = (int) (Math.random() * 11);
		int height = (int) (Math.random() * 11);
	}

	// public static int RectangleArea(int width, int height) {
	// return width * height;
	// }

	// public static int RectanglePerimeter(int width, int height) {
	// return 2 * (width * height);
	// }

	public static void main(String[] args) {

	}
}



这是我的代码。

正如你所看到的那样,有一种名为CallRectangleMethod的方法,它不带任何参数。正如你可以看到方法体包含2个随机生成的整数。

所以我想要做的是我想将随机生成的两个整数分配给下面的方法 RectangleArea和RectanglePerimeter。



我尝试过:



我尝试调用方法name.get,但不幸的是,在Java中没有类似的内置方法。所以我需要一些帮助


This is my code.
As you can see there is a method called "CallRectangleMethod" which do not take any parameters. And as you can see the method body in contains 2 randomly generated integers.
So what i am trying to do is i want to assign those two integers that were randomnly generated as parameters into the methods below called "RectangleArea" and "RectanglePerimeter".

What I have tried:

I tried calling the method name.get but unfortunately there exists no built-in methods like that in Java. So i need a little help

推荐答案

你不能。

CallRectangleMethod实际上什么都不做:它生成两个随机值,分配它们到局部变量然后退出,抛弃它刚刚使用的局部变量。它什么都不返回,没有参数,也没有办法向外界提供信息(除了通过你没有的类级变量,在这种情况下哪个是个坏主意)。

可能最好的方法是让它返回 Dimension(Java Platform SE 7) [ ^ ]实例,并使用它。
You can't.
The CallRectangleMethod effectively does nothing: it generates two random values, assigns them to local variables and then exits, throwing away the local variables it just used. It returns nothing, takes no parameters and has no way of supplying info to the outside world (other than via class level variables which you don't have, and which would be a poor idea in this case).
Probably the best way would be to make it return a Dimension (Java Platform SE 7 )[^] instance, and use that.


尝试使用全局变量。



声明全局变量并使用随机生成的整数为它们赋值。然后在所需方法中使用此全局变量值。
Try to use global variables.

Declare global variable and assign value to them with randomly generated integers. Then use this global variable values in required method.


这篇关于将void方法的值获取到另一个方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆