import java.io.*;
import java.util.*;
public class AP_ExamArray
{
//User Input AP Exam Scores Using For Loops
public static void main(String args[])
{
int [] scores = new int[5];
int j;
int sum_exam=0;
for (j=0; j<scores.length; j++)
{
Scanner kbReader = new Scanner(System.in);
System.out.println("Please enter AP Exam Score (1-5): ");
scores[j]=kbReader.nextInt();
sum_exam+=scores[j];
}
//Initializer List Final Percentage
double [] final_percent = {97.3, 89.8, 96.2, 70.1, 93.0};
double sum_percent=0;
int k;
for(k=0; k<final_percent.length;k++)
{
sum_percent+=final_percent[k];
}
// String array containing last name (while loop)
String [] last_name = new String[5];
int i=0;
while (i<last_name.length)
{
Scanner kbReader = new Scanner(System.in);
System.out.println("Please enter last name: ");
last_name[i]=kbReader.next();
i++;
}
//Average Exam Scores
double avg_exam = (double) sum_exam/scores.length;
//Average Final Percentage
double avg_percent = (double) sum_percent/final_percent.length;
}
}
方向是:
“对于输出,请使用循环来打印学生姓名、百分比和测试分数,并确保在每个列标题下方包含适当的数据:
学生姓名 学生百分比 学生测试成绩。”
我不知道该怎么做!
最佳答案
怎么样:
for(i=0; i<scores.length; i++)
System.out.println(last_names[i] +"\t"+ final_percent[i] +"\t"+ scores[i]);
使用
for
循环是因为您想要循环遍历整个 已知大小的数组。相反,如果您只想在满足特定条件时循环 您可以使用
while
循环:(例如while(condition) 循环;
)。System.out.println()
将在每次调用后打印一个新行,\t
将添加一个制表符。
为了更好地格式化,请查看 this关于使用 printf
和 format
关于java - 使用循环打印数组的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19968954/