7 Replies - 296 Views - Last Post: 10 February 2013 - 02:42 PM Rate Topic: -----

#1 apeterson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-February 13

Problem with if and else's - probably really simple?

Posted 10 February 2013 - 01:21 PM

So i've got to make a program for my Basic Computer Science class where, when you input a number between 0-100 it gives you a letter grade based on a grade scale and if you enter a number outside that range it gives you an invalid number message.

I've gotten my code so that it can go through the grade scale and it runs fine, but when I try to put in the code I've written for my invalid number message, it either 1) only runs the invalid message no matter where I place it in the code 2) will run and put both the letter grade and then follow with the invalid number message or 3) tell me that my "letterGrade" variable may have not been initialized.

Here is the code I have for just the grades:

 import java.util.Scanner;

class LetterGrade {
    public static void main(String[] args) {

Scanner scanInput;
	scanInput = new Scanner ( System.in );


		int userNumber;	
		char letterGrade;


		System.out.print("Enter a number: ");
		userNumber = scanInput.nextInt();
		
		
		  if ((userNumber >= 92)&&(userNumber <= 100)) {
          letterGrade = 'A'; 
      } else if ((userNumber >= 80)&&(userNumber <= 91)) {
          letterGrade = 'B';
      } else if ((userNumber >= 70)&&(userNumber <= 79)) {
          letterGrade = 'C';
      } else if ((userNumber >= 60)&&(userNumber <= 69)) {
          letterGrade = 'D';
      } else {
          letterGrade = 'F';
		}
			   	
	
  		 System.out.println("Grade = " + letterGrade);
	}
}



and here is the code I have written for the invalid number message:

if ((userNumber <0)||(userNumber >100)) {
} else {
	System.out.println("The number " +userNumber+ " is not valid. Only integers between 0 and 100 are valid.");
	}


I don't know if I'm just putting them together in the wrong places or what but I can't seem to figure out how to get them to work. Like I said the user should put in a value 0-100 and it should pop out a letter grade, and if the value isn't in the 0-100 range it should give an error message. I can't seem to figure out how to get the parameters set up so that I can get the printed error message if the values are out of range and only the letter grade when it's in range. Any tips?

I originally had the code for the "F" grade as a parameter between 0 and 59, but it kept giving me the error about not having an initialized variable for "letterGrade."

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with if and else's - probably really simple?

#2 baseball435  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 68
  • Joined: 30-November 09

Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 01:32 PM

You could just do

if (userNumber >= 0 && userNumber <= 100) {
    if (userNumber >= 92) 
        letterGrade = 'A';
    else if (userNumber >= 80)
        letterGrade = 'B';
    else if (userNumber >= 70)
        letterGrade = 'C';
    else if (userNumber >= 60)
        letterGrade = 'D';
    else
        letterGrade = 'F';
} else {
    System.out.println("The number " +userNumber+ " is not valid. Only integers between 0 and 100 are valid.");
}


Was This Post Helpful? 0
  • +
  • -

#3 apeterson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-February 13

Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 01:50 PM

View Postbaseball435, on 10 February 2013 - 01:32 PM, said:

You could just do

if (userNumber >= 0 && userNumber <= 100) {
    if (userNumber >= 92) 
        letterGrade = 'A';
    else if (userNumber >= 80)
        letterGrade = 'B';
    else if (userNumber >= 70)
        letterGrade = 'C';
    else if (userNumber >= 60)
        letterGrade = 'D';
    else
        letterGrade = 'F';
} else {
    System.out.println("The number " +userNumber+ " is not valid. Only integers between 0 and 100 are valid.");
}



It gives me an error on the line:
 System.out.println("Grade: " +letterGrade); 

saying that the variable is not initialized. Without that line, the code you supplied does work, but it has to print the grade/message.

Sorry I feel pretty silly dunno why I'm having such an issue.
Was This Post Helpful? 0
  • +
  • -

#4 baseball435  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 68
  • Joined: 30-November 09

Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:02 PM

View Postapeterson, on 10 February 2013 - 01:50 PM, said:

View Postbaseball435, on 10 February 2013 - 01:32 PM, said:

You could just do

if (userNumber >= 0 && userNumber <= 100) {
    if (userNumber >= 92) 
        letterGrade = 'A';
    else if (userNumber >= 80)
        letterGrade = 'B';
    else if (userNumber >= 70)
        letterGrade = 'C';
    else if (userNumber >= 60)
        letterGrade = 'D';
    else
        letterGrade = 'F';
} else {
    System.out.println("The number " +userNumber+ " is not valid. Only integers between 0 and 100 are valid.");
}



It gives me an error on the line:
 System.out.println("Grade: " +letterGrade); 

saying that the variable is not initialized. Without that line, the code you supplied does work, but it has to print the grade/message.

Sorry I feel pretty silly dunno why I'm having such an issue.


when you declare it just set it equal to 0 or something.
Was This Post Helpful? 0
  • +
  • -

#5 apeterson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-February 13

Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:17 PM

letterGrade is a character though, not an int or any type of number? If i set letterGrade = '0' and enter, for example, -1, the program runs and prints: "The number -1 is invalid.
-1 is a

This post has been edited by macosxnerd101: 10 February 2013 - 02:24 PM
Reason for edit:: Removed quote. No need to quote the person above you.

Was This Post Helpful? 0
  • +
  • -

#6 Razakel  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 05-February 10

Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:27 PM

View Postapeterson, on 10 February 2013 - 02:17 PM, said:

letterGrade is a character though, not an int or any type of number? If i set letterGrade = '0' and enter, for example, -1, the program runs and prints: "The number -1 is invalid.
-1 is a


Then set letterGrade to F, a default value, and then you just test for whether the user got a D, C, B or A through the if/else logic.
Was This Post Helpful? 0
  • +
  • -

#7 apeterson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-February 13

Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:35 PM

View PostRazakel, on 10 February 2013 - 02:27 PM, said:

View Postapeterson, on 10 February 2013 - 02:17 PM, said:

letterGrade is a character though, not an int or any type of number? If i set letterGrade = '0' and enter, for example, -1, the program runs and prints: "The number -1 is invalid.
-1 is a


Then set letterGrade to F, a default value, and then you just test for whether the user got a D, C, B or A through the if/else logic.


Gives the printed message:

"
Enter a number: -1
The number -1 is invalid.
-1 is a F
"

Can't figure out how to keep it from saying "-1 is an F." It has to print the grade at the bottom, and it works perfectly for any number and grade that is within the range 0-100 but not for the invalid numbers. :(
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3722
  • View blog
  • Posts: 12,985
  • Joined: 12-December 12

Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:42 PM

Your code works for me. Set the letterGrade using either

char letterGrade = 'F'; // or
char letterGrade = '\0'; // the empty character literal


note, this requires apostrophes rather than quotes "".

This post has been edited by andrewsw: 10 February 2013 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1