先在类方法中定义数组的方法:
public int[] method6(int[] arr){
for(int i = 0; i<arr.length;i++){
arr[i] = (int)(Math.random()*10+1);
}
return arr;
}
在类方法中返回类名就可以了
在测试类中提取出来:
int[] arr = new int[10];//数组的接收需要考虑要遍历接收
for(int q = 0;q<arr.length;q++){
int[] arr1 = com.method6(arr);
System.out.print(arr1[q]+"\t");
//int[q] arr = ;
}
//定义一个电脑类,里面不含有属性但是有方法;
package demo;
public class Computer {
public boolean method(int a){
boolean flag;
//int a = 0;
if(a%2==0){
flag = true;
}else{
flag = false;
}
return flag;
}
public int method2(int year){
if(year%400==0| year%4==0&&year%100!=0){
return 1;
}else{
return 0;
}
}
public int method3(int A,int B){
int sum = A+B;
return sum;
}
public int method4(int i,int j){
int Mul = i*j;
return Mul;
}
public int method5(int c,int d){
int Max;
if(c>d){
Max = c;
}else{
Max = d;
}
return Max;
}
public int[] method6(int[] arr){
for(int i = 0; i<arr.length;i++){
arr[i] = (int)(Math.random()*10+1);
}
return arr;
}
}
//测试类含有main操作的程序入口标识
package demo;
import java.util.*;
public class Test4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Computer com = new Computer();
System.out.println("请输入一个数字:");
int a= sc.nextInt();
boolean flag = com.method(a);
System.out.println(flag);
System.out.print("请输入年份:");
int year = sc.nextInt();
int b = com.method2(year);
System.out.println(b);
int A = 5;
int B = 6;
int sum = com.method3(A, B);
System.out.println(sum);
System.out.println("请输入两个数:");
int i = sc.nextInt();
int j = sc.nextInt();
int Mul = com.method4(i, j);
System.out.println(Mul);
int Max = com.method5(8, 6);
System.out.println(Max);
int[] arr = new int[10];//数组的接收需要考虑要遍历接收
for(int q = 0;q<arr.length;q++){
int[] arr1 = com.method6(arr);
System.out.print(arr1[q]+"\t");
//int[q] arr = ;
}
}
}