• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

[for statement] I can't assign a value correctly

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I'm new to Java and English.
This program is for calculating a student's GPA with given grade (e.g., A, B, C, D, and F) entered by user.
But according to my code, I wonder why student's GPA is 0.0 regardless of any inputs.
I tried many times and found there's a problem when assigning a varible 'sum'.
I think it's value is always zero.
The problem is, I can't find the way to modify it correctly.
Could somebody help me find what is the problem and how can I mend it?

Here's my code. Variable 'sum' appears in the for statement.

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's 2 problems I see. First you are using == to check a string. You should uses equals() or equalsIgnoreCase(). If you change this, your sum will no longer be 0 at least.

The 2nd problem is the avg, when you input "A" then "B" you should get (4+3)/2=3.5 but actually got 3.0? And yes you defined it as double. Can you figure out how to get the proper result?
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:There's 2 problems I see. First you are using == to check a string. You should uses equals() or equalsIgnoreCase(). If you change this, your sum will no longer be 0 at least.

The 2nd problem is the avg, when you input "A" then "B" you should get (4+3)/2=3.5 but actually got 3.0? And yes you defined it as double. Can you figure out how to get the proper result?



Wonderful! I solved 2 problems you mentioned above.

About typecasting, I absoulutely forgot! Thank you for reminding me.
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic