-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProblem$01.java
67 lines (58 loc) · 1.92 KB
/
Problem$01.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package chapter_seven;
import java.util.Locale;
import java.util.Scanner;
/**
* *7.1 (Assign grades) Write a program that reads student scores, gets the best score, and
* then assigns grades based on the following scheme:
* Grade is A if score is >= best -5
* Grade is B if score is >= best -10;
* Grade is C if score is >= best -15;
* Grade is D if score is >= best -20;
* Grade is F otherwise.
* The program prompts the user to enter the total number of students, and then
* prompts the user to enter all of the scores, and concludes by displaying the grades.
* Here is a sample run:
*
* Enter the number of students: 4
* Enter 4 scores: 40 55 70 58
* Student 0 score is 40 and grade is F
* Student 1 score is 55 and grade is C
* Student 2 score is 70 and grade is A
* Student 3 score is 58 and grade is C
*
* @author Sharaf Qeshta
* */
public class Problem$01
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
System.out.print("Enter the number of students: ");
int nS = scanner.nextInt();
System.out.print("Enter " + nS + " scores: ");
int[] scores = new int[nS];
int max = Integer.MIN_VALUE;
for (int i = 0; i < nS; i++)
{
int score = scanner.nextInt();
scores[i] = score;
if (score > max)
max = score;
}
for (int i = 0; i < nS; i++)
System.out.println("student " +i+ " score is " + scores[i] + " and grade is " + getChar(scores[i], max));
}
public static char getChar(int score, int max)
{
if (score >= max-5)
return 'A';
else if (score >= max-10)
return 'B';
else if (score >= max-15)
return 'C';
else if (score >= max-20)
return 'D';
else
return 'F';
}
}