gpt4 book ai didi

java - 对整数输入进行排序时如何在输出中显示 "subject"

转载 作者:行者123 更新时间:2023-12-01 16:35:58 25 4
gpt4 key购买 nike

我需要答案来显示最低分和最高分,同时给出正确的主题

import java.util.Scanner; //import package
class Task11 {
public static void main(String[] args) {

Scanner in = new Scanner(System.in); //import scanner
System.out.print("Enter SAC score for Financial: "); //enter for 3 subject sac scores
int financial = in.nextInt();
System.out.print("Enter SAC score for Statistics: ");
int stats = in.nextInt();
System.out.print("Enter SAC score for Networks: ");
int network = in.nextInt();
double average = (financial+stats+network)/3; //calculate average, use double incase of decimals

if (financial > stats) { //determine if number1 is bigger than 2 then equal itself
int answer = financial;
financial = stats;
stats = answer;
}

else if (stats > network) { //same for 2 and 3
int answer = stats;
stats = network;
network = answer;
}

else if (financial > stats) { //let it repeat itself
int answer = financial;
financial = stats;
stats = answer;
}
int highest = network;
int lowest = financial;
System.out.println("The average is: " + average);
System.out.println("The highest score was: " + highest);
System.out.println("The lowest score was: " + lowest); //show user results of input


}
}

最佳答案

这不是好的代码,但它可以在分数相同的情况下显示主题:

public static void main(String[] args) {

Scanner in = new Scanner(System.in); // import scanner
Map<Integer, List<String>> subjectScores = new HashMap<Integer, List<String>>();
List<String> subjects = new ArrayList<String>();
System.out.print("Enter SAC score for Financial: "); // enter for 3
// subject sac
// scores
List<Integer> scores = new ArrayList<Integer>();
int financial = in.nextInt();
subjects = new ArrayList<String>();
subjects.add("Financial");
subjectScores.put(financial, subjects);
scores.add(financial);

System.out.print("Enter SAC score for Statistics: ");
int stats = in.nextInt();
if (subjectScores.containsKey(stats)) { // check if score of Statistics same with score of Financial
subjects = subjectScores.get(stats);
subjects.add("Statistics");
} else {
subjectScores.put(stats, Arrays.asList("Statistics"));
}
scores.add(stats);

System.out.print("Enter SAC score for Networks: ");
int network = in.nextInt();
if (subjectScores.containsKey(network)) {
subjectScores.get(network).add("Networks");
} else {
subjects = new ArrayList<String>();
subjects.add("Networks");
subjectScores.put(network, subjects);
}
scores.add(network);

System.out.println(
"The average score was: " + (financial + stats + network) / 3);

int highestScore = Collections.max(scores);
int lowestScore = Collections.min(scores);
System.out.print("The highest subject was ");
for (String subject : subjectScores.get(highestScore)) {
System.out.print(subject + ", score: " + highestScore + ". ");
}
System.out.println();

System.out.print("The lowest subject was ");
for (String subject : subjectScores.get(lowestScore)) {
System.out.print(subject + ", score: " + lowestScore + ". ");
}
System.out.println();

in.close();
}

关于java - 对整数输入进行排序时如何在输出中显示 "subject",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61946547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com