-1

for loop should print .8 if studentScore is 63, .9 if studentScore is 64... 3.9 if studentScore is 94

        System.out.println("Enter student's score: ");
        int studentScore = console.nextInt();
        
        double score = 0;
        if(studentScore >= 63 && studentScore < 95){
            for(score = .8; score <= studentScore; score++){
                score += .1;
            }
            System.out.printf("Your grade is: %.1f", score-studentScore);
        }
        
        else if (studentScore >= 95){
            System.out.println("Your score is 4.0");
        }
        
        else if(studentScore >= 60 && studentScore <=62){
            System.out.println("Your score is 0.7");
        }
        
        else{
            System.out.println("Your score is 0.0");
        }
Troyal
  • 11
  • 3
  • it would be easier to understand if you also added an actual question. you do realize there is no need for the ' && studentScore < 95' in your else if, right? – Stultuske Aug 11 '21 at 06:01
  • Thanks, that's a start but I changed double score = 0 and score to .8 and still can't get it to work – Troyal Aug 11 '21 at 06:05
  • I have to finish the code for other conditions, just trying to figure out why my for loop rolls back to 0 after 1.0 – Troyal Aug 11 '21 at 06:06
  • Here's the rest of the conditions. Sorry just posted the for loop because I'm on a time crunch. – Troyal Aug 11 '21 at 06:08
  • What do you think the `for` loop is doing ? For a start it uses `score` and the variable but also changes it in the body of the loop (always a "code smell"). – John3136 Aug 11 '21 at 06:19
  • I'm guessing that it is supposed to iterate from 0.8 to 3.9 depending on studentScore – Troyal Aug 11 '21 at 06:22
  • 1
    Why .8 to 3.9 ? your for condition is `score <= studentScore` – John3136 Aug 11 '21 at 06:31
  • And just saying: using floating point numbers within loops can be tricky business. Make sure you understand https://stackoverflow.com/questions/588004/is-floating-point-math-broken for example. – GhostCat Aug 11 '21 at 06:42

4 Answers4

0

IMHO using a for loop to calculate the score is overly complicated and error prone.

Just use

score = 0.8 + (studentScore - 63) * 0.1;
System.out.printf("Your grade is: %.1f", score);

Why does the current for loop not work? That might be easier to see if you replace it with the equivalent while loop:

score = 0.8;
while (score <= studentScore) {
    score += 0.1;
    score++;
}
System.out.printf("Your grade is: %.1f", score);

This loop generates the values 0.8, 1.9, 3.0, 4.1, ..., 63.5, 64.6, 65.7, ... and stops as soon as the generated value is greater than studentScore.

That means that for a studentScore of 63 it stops when score is 63.5. And because the print statement calculates score-studentScore it will print out 0.5.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
0

Maybe you don’t need to use loops

        double score = 0.8;
        if(studentScore >= 63 && studentScore < 95){
            System.out.printf("Your grade is: %.1f", (studentScore - 63) / 10 + score);
        }
xuanzjie
  • 51
  • 7
0

Using for loop to calculate a score is not the best. Use simple arithmetic instead.

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.print("Enter student's score: ");
    int studentScore = console.nextInt();

    if (studentScore >= 95)
        System.out.println("Your score is 4.0");
    else if (studentScore >= 63) {
        double score = 0.8 + (studentScore - 63) * 0.1;
        System.out.format(Locale.ENGLISH, "Your grade is: %.1f\n", score);
    } else if (studentScore >= 60)
        System.out.println("Your score is 0.7");
    else
        System.out.println("Your score is 0.0");
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
-1

you should change this:

else if(studentScore >= 63 && studentScore< 95){
            for(score = 1; score <= studentScore; score++){
                score += .1;
            }

to this:

double score = .8;
    if (studentScore >= 95){
        System.out.println("4.0");
    }
    else if(studentScore >= 63 && studentScore< 95){
        for(int i = 64; i <= studentScore; i++){
            score += .1;
    
        }
        System.out.printf("Your grade is: %.1f", score);
    }