問題描述
X市正在進行歌唱比賽,請你寫一個程序計算得分。
每名選手從1到N編號,每名選手的綜合成績由以下幾個部分組成:
1.歌唱得分佔70%
2.才藝展示得分佔20%
3.觀衆投票得分佔10%
4.不超過五分的特殊加分(如果總分超過100分則記爲100)
輸入格式
第一行一個整數N,表示選手人數
接下來N行,每行四個整數,分別表示歌唱得分,才藝得分,投票得分和加分。
輸出格式
輸出N行,每行一個數,表示得分(保留一位小數)
樣例輸入
6
62 50 60 1
77 71 85 3
98 79 98 5
75 62 60 0
99 72 68 4
82 88 89 2
樣例輸出
60.4
79.6
99.2
70.9
94.5
85.9
思路:這題的難點就在於最後輸出的那個部分,其他的地方仔細讀題就可。
補充知識點:DecimalFormat("#.0").format(); 保留一位小數輸出。
詳情:https://www.cnblogs.com/yelongsan/p/5482774.html
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
double ans = 0;
for (int i = 0; i < n; i++) {
int sing = scanner.nextInt(); // 歌曲成績
int skill = scanner.nextInt(); // 才藝成績
int vote = scanner.nextInt(); // 投票成績
int bonus = scanner.nextInt(); // 加分成績
ans = sing * 0.7 + skill * 0.2 + vote * 0.1 + bonus; // 題目算式
if (ans > 100) { // 成績大於100
ans = 100; // 直接給100
}
System.out.println(new DecimalFormat("#.0").format(ans)); // 保留一位小數輸出
}
}
}
思路:直接利用三目運算符返回是否是大於100的成績即可~~
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int sing = scanner.nextInt();
int skill = scanner.nextInt();
int vote = scanner.nextInt();
int bonus = scanner.nextInt();
double sum = sing * 0.7 + skill * 0.2 + vote * 0.1;
double ans = sum + bonus;
System.out.printf("%.1f\n", ans > 100.0 ? 100.0 : ans); // 三目判斷輸出
}
}
}