我正在尝试编写一个轮询程序,该程序处理五个问题,并将问题放在2d数组的行上。此外,我如何让程序计算一个人对该问题的评分。例如,如果五个人的评分为五,我该如何编写程序来计算评分并将其放在2d数组中。
以下是说明:
编写一个简单的轮询程序:
-
允许用户对五个社会意识问题进行评分,从1(最不重要)到10(最重要);
-
选择五个对你来说很重要的原因(例如,政治问题、全球环境问题)。使用一个-维度数组主题(String类型),用于存储五个原因;要总结调查响应,请使用5行10列二维阵列响应(int类型):
- 每一行对应于主题数组中的一个元素。当程序运行时,它应该要求用户对每个问题进行评分。(5,13)范围内的人对调查做出了回应。然后让程序显示结果摘要,包括:
a) 一份表格报告,左侧有五个主题,顶部有10个评分,每列列出每个主题的评分数。
b) 在每一行的右侧,显示该问题的平均评分。
c) 哪一期的总分最高?显示问题和积分总数。d) 哪一期的总分最低?显示问题和积分总数。
这是我的代码:
import java.util.Arrays;
import java.util.*;
public class Polling {
/**
* @param args the command line arguments
*/
public static String[] issues=new String[20];
public static void main(String[] args) {
Scanner console=new Scanner(System.in);
issues[0]="Global Warming";
issues[1]="Earth Quakes";
issues[2]="Stopping war";
issues [3]="Equal Rights";
issues[4]="Curing Cancer";
int[][] polling =new int[5][10];
Random rand=new Random();
int random=rand.nextInt(9)+5;
int poll=0;
String polling2=Arrays.toString(polling);
for(int i=1;i<random;i++){
System.out.println("Person"+i);
System.out.println("Rate these issues from 1-10");
System.out.println(issues[0]);
int zero=console.nextInt();
System.out.println(issues[1]);
int one=console.nextInt();
System.out.println(issues[2]);
int two=console.nextInt();
System.out.println(issues[3]);
int three=console.nextInt();
System.out.println(issues[4]);
int four=console.nextInt();
}
System.out.println();
类似的东西?:
public static final String[] ISSUES = {
"Global Warming",
"Earth Quakes",
"Stopping war",
"Equal Rights",
"Curing Cancer",};
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Random rand = new Random();
int pollings = rand.nextInt(9) + 5;
int [][] rates= new int[pollings][ISSUES.length];
for (int i = 0; i < pollings; i++) {
System.out.println("Person" + i);
System.out.println("Rate these issues from 1-10");
for (int j = 0; j < ISSUES.length; j++) {
System.out.println(ISSUES[j]);
rates[i][j] = console.nextInt();
}
}
// ADDED
int minRating = Integer.MAX_VALUE;
int maxRating = Integer.MIN_VALUE;
int minRatingIndex = -1;
int maxRatingIndex = -1;
for (int i = 0; i < ISSUES.length; i++) {
System.out.print(ISSUES[i]+":");
int rating = 0;
for (int j = 0; j < pollings; j++) {
System.out.print("t"+rates[j][i]);
rating += rates[j][i];
}
double average = ((double)rating)/pollings;
System.out.println("tavr: "+average);
if (rating < minRating ){
minRating = rating;
minRatingIndex = i;
}
if (rating > maxRating ){
maxRating = rating;
maxRatingIndex = i;
}
}
System.out.println("Max points:t"+ISSUES[maxRatingIndex]+":t"+maxRating+" points");
System.out.println("Min points:t"+ISSUES[minRatingIndex]+":t"+minRating+" points");
System.out.println();
}