只使用三种方法(包括main)和一个包含10个整数的数组,我需要创建一个执行这些操作的程序:
这就是我现在所拥有的。我唯一的问题是我不知道如何返回多个值。
public class Array10PassFail
public static int countPass (int [] num)
{
int count = 0;
int counter = 0;
for (int i = 0; i < num.length; i++)
{
if (num [i] >= 50)
{
count++;
}
else if (num [i] < 50)
{
counter++;
}
}
return count;
}
public static int printStats (int [] num)
{
int aTotal = 0, bTotal = 0,cTotal = 0, dTotal = 0, eTotal = 0;
for (int i = 0; i < num.length; i++)
{
if (num[i] >= 80)
{
aTotal++;
}
else if (num[i] >= 70)
{
bTotal++;
}
else if (num[i] >= 60)
{
cTotal++;
}
else if (num[i] >= 50)
{
dTotal++;
}
else if (num[i] < 50)
{
eTotal++;
}
}
return aTotal;
}
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
int [] num = new int [10];
for (int i = 0; i < num.length; i++)
{
System.out.print("Enter score: ");
num[i] = sc.nextInt();
}
int passf = countPass(num);
System.out.println("There are " + passf + " people who passed and " + ??? + " who failed. ");
}
答案 0 :(得分:0)
如果要返回多个值,通常需要返回封装值的单个对象。 使用列表&gt;或者一个ArrayList,将您的值添加到List中并返回它。在调用函数时,获取List中的返回值并根据需要提取信息
答案 1 :(得分:0)
创建一个数组。例如,
public static int[] countPass(int[] num){
int count = 0, counter=0;
for (int i = 0; i < num.length; i++){
if (num [i] >= 50) count++;
else counter++;
}
return new int[]{count,counter};
}
和做
之类的事情int array[] = countPass(num);
System.out.println("Failed -> " + array[1] + "Passed->" + array[0]);
同样,为成绩做。