我目前正在用 Java 创建一个小型应用程序。但是,我现在卡住了。我无法从一种方法捕获返回值到另一种方法。
这是我何时返回值以及我想在何处捕获它的示例:
public int[] highest(int y[]){
int largest = 500;
int largeArr[] = new int[y.length];
for(int count = 0; count < y.length; count++)
{
if(y[count] >= largest){
largeArr[count] = y[count];
System.out.print(largeArr[count] + " ");
}
}
return largeArr;
}
public void highText()
{
RandomNumbers high = new RandomNumbers();
System.out.println("\nDessa tal är " + high.highest() + " intervallet 500 - 999: ");
}
我很清楚我应该利用参数和参数。但是当我想稍后打印文本时,这对我来说是个问题
完整代码:
RandomNumbers rnd = new RandomNumbers();
rnd.highText();
rnd.highest(nummer);
import java.util.Scanner;
public class RandomNumbers {
public void rnd(int y[]){
for(int count = 0; count < y.length; count++)
{
y[count] = (int) ( Math.random()*1000);
System.out.print(y[count] + " ");
}
}
public int[] lowest(int y[]){
int largest = 499;
int smallArr[] = new int[y.length];
for(int count = 0; count < y.length; count++)
{
if(y[count] <= largest){
smallArr[count] = y[count];
System.out.print(smallArr[count] + " ");
}
}
return smallArr;
}
public int[] highest(int y[]){
int largest = 500;
int largeArr[] = new int[y.length];
for(int count = 0; count < y.length; count++)
{
if(y[count] >= largest){
largeArr[count] = y[count];
System.out.print(largeArr[count] + " ");
}
}
return largeArr;
}
public void highText()
{
RandomNumbers high = new RandomNumbers();
System.out.println("\nDessa tal är " + high.highest() + " intervallet 500 - 999: ");
}
public void lowText()
{
System.out.println("\nDessa " + smallArr + " tal är i intervallet 0 - 499: ");
}
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
System.out.print("Hur många slumptal i intervallet 0-999 önskas? ");
int x = user_input.nextInt();
int nummer[] = new int[x];
RandomNumbers rnd = new RandomNumbers();
System.out.println("\nHär är de slumpade talen: ");
rnd.rnd(nummer);
rnd.lowText();
rnd.lowest(nummer);
rnd.highText();
rnd.highest(nummer);
}
}
当我打印文本时,我使用方法 rnd.highText();。如果我以前在参数中使用 largeArr,那么程序请求参数 - 当我真的只想打印方法时:(
请帮忙=)