please help me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

please help me

i want to exit in this java code at any time /* Now I'm going to create a simple java CLI program to determining results in a final examination such as : fail(0-49) accept(50-59) medium(60-69) good(70-79) very good(80-89) excellent(90-100) */ import java.util.*; class hama { public static void main(String[] args) { int mark; String m; Scanner input=new Scanner(System.in); System.out.println("**************"); System.out.println("|||WELCOME|||"); System.out.println("**************"); System.out.println(); m=input.nextLine(); while(!m.equals("exit")){ System.out.println("Please Enter Your Mark? "); System.out.println("------------------------"); System.out.print("mark = "); mark=input.nextInt(); if (m.equals("exit")){ System.exit(0); } //fail(0-49) else if(mark>=0 && mark<=49){ System.out.println(); System.out.println("Fail"); System.out.println(); } //accept(50-59) else if(mark>=50 && mark<=59){ System.out.println(); System.out.println("Accept"); System.out.println(); } //medium(60-69) else if(mark>=60 && mark<=69){ System.out.println(); System.out.println("Medium"); System.out.println(); } //good(70-79) else if(mark>=70 && mark<=79){ System.out.println(); System.out.println("Good"); System.out.println(); } //very good(80-89) else if(mark>=80 && mark<=89){ System.out.println(); System.out.println("Very Good"); System.out.println(); } //Excellent(90-100) else if(mark>=90 && mark<=100){ System.out.println(); System.out.println("Excellent"); System.out.println(); } //anything else is else{ System.out.println(); System.out.println("Sorry, I did not understand that."); System.out.println(""); } } } }

18th May 2018, 5:29 PM
Muhamad Ali Ibrahim
Muhamad Ali Ibrahim - avatar
1 Answer
+ 3
In your loop, you're never obtaining further input for the variable 'm', which is what's checking for 'exit' in order to exit. Since it isn't receiving further input to it, it'll never exit the loop. At the top of your loop, have it assign the next line of input into your variable. This will allow it to be able to do other options that's inside your loop, including if the option is to exit. Also, I'd recommend properly setting variable names. Best rule of thumb is to name them after something descriptive to its purpose. 'm' means what? If you're someone like me reading the code, what does that variable describe or do I have to read the code just to figure it out for myself? See what I mean? However, if you named it something like userInput, then I would immediately understand it's where I'm storing input from the user.
18th May 2018, 5:40 PM
Fata1 Err0r
Fata1 Err0r - avatar