mardi 21 avril 2015

Java using if statement, for loop and method call

Create a method called getLetterGrade. The getLetterGrade method takes a grade between 0-100 and then prints out the corresponding letter grade. The grade ranges are A: 100-90 B: 80-89 C: 70-79, D: 60-69, F: 0-59. In main, create a for loop that will ask for the numeric grade and uses the getLetterGrade method to print out the letter grade, until the user enters a -1."

Here's my code: I wrote using if,else if condition. what i don't understand is that do i have to write the condition in main or the method. Usually i dont have a problem with coding but this thing has me puzzled. I though about using case but if, else if conditions seems alot more straight forward.

import java.util.Scanner;

public class getLetterGrade {

public static void getLetterGrade(String getLetterGrade){


int studentGrade = 0;

    if(studentGrade >= 90) getLetterGrade("A");
    else if(studentGrade >= 80) getLetterGrade("B");
    else if(studentGrade >= 70) getLetterGrade("C");
    else if(studentGrade >= 60) getLetterGrade("D");
    else if(studentGrade >= 0) getLetterGrade("F");
    else 
        System.out.println("you have entered incorrect integer");

int i = 0;

for (i = 0; i < 1; i++){
    System.out.print(studentGrade + " ");
}

System.out.println();

}

public static void main(String[] args) {

    int studentGrade = 0;
    getLetterGrade("A");
    getLetterGrade("B");
    getLetterGrade("C");
    getLetterGrade("D");
    getLetterGrade("F");

    System.out.println("Please enter you grade total: ");
    Scanner Keyboard = new Scanner(System.in);
    studentGrade = Keyboard.nextInt();


    for(studentGrade = 0; studentGrade < 1; ++studentGrade){
        System.out.print(studentGrade);
    }
}




}

i know i have to print getLetterGrade but when i try to print getLetterGrade it throws errors, i dont get it.....

thanks for any and all help!!

Aucun commentaire:

Enregistrer un commentaire