I'm an introvert to Java

Asked 2 years ago, Updated 2 years ago, 31 views

package project; import java.util.Scanner;

public class Grading {
    public static void main (String[] args) {
        char grade;
        Scanner a = new Scanner(System.in);
        while (a.hasNext()) {
            int score = a.nextInt();
            If (score > = 90.0) // score is 90.0 or higher
                grade = 'A';
            else if (score > = 80.0) // score is greater than or equal to 80.0 and less than or equal to 90.0
                grade = 'B';
            else if (score > = 70.0) // score is greater than or equal to 70.0 and equal to 80.0 only
                grade = 'C';
            else if (score > = 60.0) // score is greater than or equal to 60.0 and equal to 70.0 only
                grade = 'D';
            If else // score is only 60.0
                grade = 'F';
            System.out.println ("credit is " +grade+");
        }
    }
}




This syntax is all understood, but you want to create a program that ends when you enter a value of "y". Is there a way? I don't know anything because I'm a beginner.I think we need to distinguish between numeric characters first.

java

2022-09-22 20:38

1 Answers

If the source is understood, you shouldn't ask "I don't know because I'm a beginner" about what you want to do. After all, it's no different from not understanding the source.

You need to be specific in order to get a helpful answer.

int score = a.nextInt(); // Receive keyboard numeric input.

If the requirement is simply Y, it is a termination process, but two more things need to be considered. What happens when a user inserts a letter according to the Y envelope? int What if you put a number that exceeds the range?

Currently, only numbers are received and processed, so to receive text, use the nextLine method in Scanner to accept text. Since it is a String that has been received, it is possible to verify that the input value is Y. To display grades, you can convert to int type using the parseInt method in the Integer class.

If parseInt is a character, NumberFormatException occurs. If the size is more than 100 after converting to a number, it should be judged as an input error.

Finally, the exit method of the System class can be used to exit.

It's like homework, so please think about it yourself. My skills improve as much as I thought about it.


2022-09-22 20:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.