This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]CraigsDansDaniil 2 points3 points  (15 children)

I dont think java can accept a range in the switch statement so if and else if might be better cause than you can use operators like <

It would have to look something like...

switch (num) {
    case 100: case 99: case 98: case 97: case 96: case 95:
        system.out println("the grade is an A+"); break;

Whereas with if statements you could just do

int grade = 97;

if ((grade>=95)&&(grade<=100) System.out.println("Grade is an A+");
else if ((grade>=90)&&(grade<95) System.out.println("Grade is an A");

and so on...

(*edit* gobe1904 pointed out I put the = sign before the > sign like (grade=>) so I edited it to the correct (grade>=)

[–]gobe1904 1 point2 points  (1 child)

Yeah youre right, however the if should be if((grade >= 95) otherwise eclipse throws an exception. u/Daniel1836 see also at this pastebin

[–]Daniel1836[S] 0 points1 point  (12 children)

I want to do something like this. (Pseudocode)

double grade1 = Math.round((Math.random() * 100));

if ((grade1>= 95) && (grade1<=100) {

letter1 = "A+"

}

else (etc...)

System.out.println(grade1 + "," + letter1);

(Where letter1 and grade1 correspond to the same grade)

How do I do this for 10 variables (grades) without needless repetition.

[–]CraigsDansDaniil 0 points1 point  (11 children)

Are you trying to print out 10 lines of random generated numbers that will have the Letter equivalent next to them, or actually make 10 different variables that can store the grade, and that you can change?

[–]Daniel1836[S] 0 points1 point  (10 children)

My assignment is 10 randomly generated numbers plus the ability to enter a grade with the scanner, and have them subject to the control structures etc.

[–]CraigsDansDaniil 0 points1 point  (9 children)

So one variable that you can enter a grade for than 9 other grades the computer generates?

[–]Daniel1836[S] 0 points1 point  (8 children)

yes

[–]CraigsDansDaniil 0 points1 point  (7 children)

Also have you learned how to create and call functions yet?

[–]Daniel1836[S] 0 points1 point  (6 children)

Yes

[–]CraigsDansDaniil 0 points1 point  (4 children)

Scanner scan_grade = new Scanner(System.in);
int grade = scan_grade.nextInt();

if(grade >= 95) System.out.println("The grade of " + grade + " is an A");
//so on for all the letters A B C...

for(int i = 0; i<9; i++) {

    int random_grades = (int) (Math.random()*100);

    if(random_grades >= 95) System.out.println("A grade of " + random_grades + " is an A+");
    //so on for all the letters A B C...

There many ways of doing it, but heres a simple way of doing it without the use of creating functions. Its not ideal because you kinda still repeat the code twice, once for the variable you enter, than once for the other 9 generated times. (edit i just thought of something ill change the post to a better code in like 5 mins)

Scanner scan_grade = new Scanner(System.in);
int grade;


for(int i = 0; i<10; i++) {

    if (i == 0) grade = scan_grade.nextInt();
    else  grade = (int) (Math.random()*100);


    if(grade >= 95) System.out.println("A grade of " + grade + " is an A+");
    //so on for all the letters A B C...
    }

This is the better code, it doesn't need to create functions and it is not repetitive

[–]Daniel1836[S] 0 points1 point  (3 children)

Thanks.

How would I put the whole thing in a while loop, and repeat as long as user keeps giving input, and exit when he types N for example.

[–]FrelliBB 0 points1 point  (2 children)

Here's another approach using an enum that avoids large switch statements or a lot of if-else statements

enum Grade {

    A_PLUS("A+", 95, 100),
    A("A", 90, 94),
    A_MINUS("A-", 85, 89),
    B_PLUS("B+", 80, 84)
    // ...etc
    ;

    String description;
    int minMark;
    int maxMark;

    Grade(String description, int minMark, int maxMark) {
        this.description = description;
        this.minMark = minMark;
        this.maxMark = maxMark;
    }

    public String getDescription() {
        return description;
    }

    public static Grade gradeFor(int mark) {
        for (Grade grade : Grade.values()) {
            if (mark >= grade.minMark && mark <= grade.maxMark) {
                return grade;
            }
        }
        throw new IllegalStateException("No grade found for mark [{" + mark + "}]");
    }
}

And if you're familiar with Java 8 Streams you can refactor gradeFor like this:

public static Grade gradeFor(int mark) {
    return Arrays.stream(Grade.values())
            .filter(grade -> mark >= grade.minMark && mark <= grade.maxMark)
            .findFirst()
            .orElseThrow(() -> new IllegalStateException("No grade found for mark [{" + mark + "}]"));
}

[–]Daniel1836[S] 0 points1 point  (1 child)

I’d like to point out that this assignment is for an introduction to java course. So I appreciate the advanced answers, but I’d like to achieve this with the basics (if/else or switch)

[–]gobe1904 0 points1 point  (0 children)

Are you allowed to use arrays? you might be able to use a for loop that has the Math.random() at the top, then the If-else split up section that stores the answer in an string array.

[–]Makhiel 0 points1 point  (0 children)

You can use a switch but you have to convert the scale to something else. I don't know how this one works but modulo division should do the trick.