Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 400009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:54:08+00:00 2026-05-12T16:54:08+00:00

This IS NOT homework, but it is a practice that the teacher gave us

  • 0

This IS NOT homework, but it is a practice that the teacher gave us to help study for a test. But i will not be turning this in to the teacher. (i’m hoping that is allowed)

He wants a user to input a grade and have it assigned a letter grade. It would be easy with if statements, but i can’t use ANY! I have to use the switch method.

Here is my functional 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;

 }

}

Here 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 haven’t been programming enough to have this analytical mind. I HAVE tried to complete it, i just haven’t found a way to covert the user input (int) into a letter grade (string) without if statements.

I know this is incomplete, but this is as far as I could go without making errors.

EDIT:WOW! Thanks guys, i hate to pick a single correct answer, because a lot of you helped me!

This is what i ended up with (that worked :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;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:54:09+00:00Added an answer on May 12, 2026 at 4:54 pm

    OK, my assumption is that you will enter a numeric grade 0-100, and that the letter grades you want go as “90-100: A; 80-89: B; …; 60-69: D; 0-59: F”.

    If that is not the case, your first lesson to be learned about programming is: the most important part of coding is clearly writing out specifications and requirements

    Having said that, there are smart and dumb approaches.

    • Dumb approach:

      • Use a switch on the “input” as you do now

      • Since values 90, 91… 100 are all supposed to be treated the same, put them all in as separate switch cases but only do the work in the last one (known as fall-through; see SwitchDemo2 on this page for how that works)

      • For the last value (say, 90 if you start from 100 and go down), the switch statement would be letterGrade = “A”; break;

      • once you reach case of 89, you repeat the same logic as you did for 100 through 90

      • Lather, Rinse, Repeat, till 60

      • Handle Fs as a default case.

    This will be very long, quite dumb (as in, brute force) and very annoying to write, and a brilliant teaching tool for why you never want to write dumb code again if you can help it.

    • More elegant solution (insert appropriate Star Wars quote here)

      • What do you see in common between every # that correspons to “A”?

      Right, it’s the fact that, aside from 100, they all are 2-diit #s starting with 9.

      So, to handle them all in 1 case statement, wouldn’t it be wonderful to switch on some value that gives you “9” for all #s between 90 and 99?

      I hope you know the answer right away… hint below…

      Yes, just divide whole by 10!

      So, you need to have a switch on your numeric grade diveded by 10,
      with cases of 10, 9, 8, 7, 6 and default mapping to F.

    Hope this covers enough ground for you to come up with working solution, while showing how the analysis might work as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: This is for a homework assignment, but the question is not regarding the
This one is a case of not doing your homework.:-) Apart from dynamic loading
Sorry for this not being a real question, but Sometime back i remember seeing
This is not a new topic, but I am curious how everyone is handling
This is not exactly a programming question, but it's highly related. We are writing
This is not a technical problem, but very annoying. Does anyone know how to
This may not be the correct way to use controllers, but I did notice
Under what circumstances would this or would this not be safe? I have a
Ok, so, my visual studio is broken. I say this NOT prematurely, as it
$(#dvMyDIV).bind(resize, function(){ alert(Resized); }); or $(#dvMyDIV).resize(function(){ alert(Resized); }); The questions Why is this not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.