-1

Hi my code compiles fine but when i run it i have to first enter the "mark" and then i need to enter the case. How do i alter the code so i do not need to enter anything for the case?

I know I can use a while loop or just else if but i want to get this working with switch case.

  import java.util.*;                                                             
  public class GradeCalcCASE {                                                                               
       public static void main(String[] args) {                                                                                   
       Scanner sc = new Scanner(System.in);                                        
       int choice;                                                                 
       double m;                                                                   
       //Sets ^^^ m as the mark that the user inputs                               
       System.out.println("Please enter the mark");                                
       m = sc.nextDouble();                                                        
       choice = sc.nextInt();                                                      
       if(m<0) {choice = 1;}                                                   

       else if(m>100) {choice = 2;}                                            

       else if(0<=m && m<50) {choice = 3;}                                     
    switch(choice) {                                                   
      case 1:                                                                     
      System.out.println("Invalid mark");                                         
      break;                                                                      

      case 2:                                                                     
      System.out.println("Invalid mark");                                         
      break;                                                                      

      case 3:                                                                     
      System.out.println("F");                                                    
      break;                                                                      

      }                                                                           
    }                                                                           
  }  
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
Ash
  • 3
  • 2
  • 1
    "stop switch case from requiring input" ? ehm .. what do you mean by that? you want to do something like: switch() { case "one": ... } ?? – Stultuske Mar 11 '19 at 09:37
  • currently when i run the program it asks firstly for the mark to be input then it sits and waits for another int to be entered. Sorry if i cant explain it better im new to the java world. – Ash Mar 11 '19 at 09:42
  • May be you mean the 'default' case which gets executed once neither of the cases are true? – Pavel Smirnov Mar 11 '19 at 09:42
  • @Ash change line 12 to `choice = 0;` – Laazo Mar 11 '19 at 09:43
  • @Ash that has nothing to do with your switch, that is your: m = sc.nextDouble(); 12 choice = sc.nextInt(); – Stultuske Mar 11 '19 at 09:43

2 Answers2

0

Remove choice = sc.nextInt(); (at line 12) from your code and initialize choice with value '0' as int choice = 0; (at line 7)

Prasann
  • 1,263
  • 2
  • 11
  • 18
  • Thanks @Prasann that worked a treat. Out of curiosity what does setting the choice value to '0' do? – Ash Mar 11 '19 at 09:52
  • @Ash `choice = sc.nextInt();` waits for input from the input stream hence you program hanged after entering the mark. Setting it to zero just initialises the variable to zero so that it can be set later from your switch statement – Laazo Mar 11 '19 at 09:57
  • @Ash, You would need to initialize the variables before you can use them. In your case as you had set `choice = sc.nextInt();` earlier, it wasn't asking to initialize the variable. One you remove this statement without initializing it, the compiler would report error. Look at this SO post for more details - https://stackoverflow.com/questions/1560685/why-must-local-variables-including-primitives-always-be-initialized-in-java – Prasann Mar 11 '19 at 10:01
0

Here is the line where your program waits for choice input:

choice = sc.nextInt();

You just need to remove/comment it.

Also note that choice calculation conditions do not cover range m >= 50 && m <= 100.

devmind
  • 344
  • 2
  • 10
  • Food for thought @Ash: you didn't need or want additional input, so why did you think `choice = sc.nextInt();`, was necessary or even appropriate? – Kevin Anderson Mar 11 '19 at 10:02