Java – how to use the switch () statement to convert numbers to letter levels?

It's not homework, but it's a practice for the teacher to help us study the exam But I won't hand it over to the teacher (I hope this is allowed)

He wants users to enter grades and assign them an alphabetic grade The if statement is easy, but I can't use anything! I have to use the switch method

This is my feature class:

public class Grade {

public double getGrade(int input)
 {
  double inputGrade;
  switch(input)
  {
   case 1: inputGrade >= 90;
       break;
   case 2: inputGrade >= 80;
       break;
   case 3: inputGrade >= 70;
       break;
   default:
       grade = 60;
  }
  return grade;

 }

}

This is my test class:

import java.util.*;
public class TestGrade
{
 public static void main(String[] args)
 {
  Scanner scan = new Scanner(system.in);
  int input = scan.nextInt();
  Grade lGrade = new Grade();
  double finalGrade = lGrade.getGradeSwitch(input);
  System.out.println("Your toll is $" + finalGrade);
 }

}

I just don't have enough programming to have this analytical thinking I tried to do it, but I just couldn't find a way to convert user input (int) to an alphabetic level (string) without an IF statement

I know it's incomplete, but I can go without making mistakes

Editor: Wow! Thank you, I hate to choose a correct answer, because many people have helped me!

This is my final result (effective: D)

public String getGrade(int input)
{
    String letterGrade;
    switch(input/10)
    {
        case 9: letterGrade = "A";
                  break;
        case 8: letterGrade = "B";
                  break;
        case 7: letterGrade = "C";
                  break;
        case 6: letterGrade = "D";
        default:
                  letterGrade = "F";
    }
    return letterGrade;

}

Solution

Well, my assumption is that you will enter a numerical level of 0-100, and the letter level you want is "90-100: a; 80-89: B;...; 60-69: D; 0 - 59: F"

If not, your first lesson in programming is that the most important part of coding is to clearly write specifications and requirements

Having said that, there is a clever and foolish way

>Stupid way:

>As now, use the switch on the input > since the values 90, 91... 100 should be considered the same, put them all in a separate switch box, but only work in the last one (called pass through; see switchdemo2 on this page for how that works) > for the last value (for example, 90 if starting from 100 and down), The switch statement will be lettergrade = "a"; Break; > Once you reach 89, you repeat the same logic bubble as 100 to 90, rinse, repeat, until 60 > handle Fs as default.

It will be very long, quite stupid (such as brute force) and very annoying writing, as well as an excellent teaching tool, why do you no longer want to write stupid code if you can help it

>More elegant solution (insert appropriate Star Wars quotation here)

>What do you have in common with all the people involved in "a"?

Yes, the fact is, except for 100, they all start with 9 2-diit#

So if you want to deal with them in a case statement, wouldn't it be good to #s open some values of "9" for all between 90 and 99?

I hope you know the answer right away

Yes, just divide the integer by 10!

Therefore, you need to convert your number level to 10, including 10, 9, 8, 7, 6 and the default mapping to F

I hope this will provide you with enough work solutions and show you how the analysis works

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>