0

switch statement problem

package voting_age_using_switch_statement; import javax.management.Attribute; import java.util.Scanner; public class voting_age_using_switch_statement { public static void main(String[] args) { System.out.println("enter your "); Scanner scanner1 = new Scanner(System.in); int marks = scanner1.nextInt() ; switch (marks) { int x = marks /100; case 1 ( =>100 && >90 ) System.out.println("excellent thats "); System.out.print(x); System.out.print("accuracy"); break; case 2 ( =>89 && >=79 ) System.out.println("good!! but can be better "); System.out.print(x); System.out.print("% accuracy"); break; case 3 ( >=78 && >=69 ) System.out.println("obove average thats "); System.out.print(x); System.out.print("% accuracy"); break; case 4 ( >=68 && >=59 ) System.out.println("average "); System.out.print(x); System.out.print("accuracy"); break; case 5 ( >= 58 && >= 49 ) System.out.println("excellent thats "); System.out.print(x); System.out.print("accuracy"); break; case 6 ( >=48 && >= 39 ) System.out.println("you barely passed "); System.out.print(x); System.out.print("accuracy"); break; case 7 ( >= 38 && >= 29 ) System.out.println("excellent that’s a failure "); System.out.print(x); System.out.print("accuracy"); break; case 8 ( >= 28 && >= 19 ) System.out.println("excellent that’s a royal failure "); System.out.print(x);

20th Apr 2017, 3:08 AM
Hridyansh Thakur
Hridyansh Thakur - avatar
3 Antworten
+ 8
Instead of switch(...) {} use if..else if..else construct as it is more suitable for this type of question int x = marks /100; if(x>=100){ System.out.println("excellent thats "); System.out.print(x); System.out.print("accuracy"); } else if(x>=90){ System.out.println("good!! but can be better "); System.out.print(x); System.out.print("% accuracy"); } else if(x>=70){ System.out.println("obove average thats "); System.out.print(x); System.out.print("% accuracy"); } else if(x>=60){ System.out.println("average "); System.out.print(x); System.out.print("accuracy"); } else if(x>50){ System.out.println("excellent thats "); System.out.print(x); System.out.print("accuracy"); } else if(x>=40){ System.out.println("you barely passed "); System.out.print(x); System.out.print("accuracy"); } else if(x>30){ System.out.println("excellent that’s a failure "); System.out.print(x); System.out.print("accuracy"); else{ System.out.println("excellent that’s a royal failure "); System.out.print(x); }
20th Apr 2017, 4:30 AM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 2
you are confused how a switch statement works. it cant use boolean expressions like x >= 7 this is how it works int x = 8; switch (x) { case 2: x++; break; case 8: x+=2 break; } case means what the value of the variable being switched is since x is 8, it goes to case 8
20th Apr 2017, 3:29 AM
Edward
+ 1
what you are looking for are if else statements
20th Apr 2017, 3:30 AM
Edward