d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Question > Hm
123Next
Add Reply New Topic New Poll
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 05:21pm
Right now I'm stuck trying to configure my app and was wondering if there's a simplistic way of ending the app run by prompting the user to select command "x"?

It's a requirement in the app, using eclipse.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 05:35pm
Quote (carteblanche @ Jan 25 2015 07:24pm)


Would I have to make a separate class for Runtime, and allocate the "Exit" option to "Runtime.getRuntime().exit(n)"?

I am mostly confused with the (n) part. I see that when a non-zero is entered it reads as an irregular error and terminates.. Just not sure how to go about all of this in a program.

This post was edited by axid3nt on Jan 25 2015 05:38pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 05:59pm

Code
String choice;
Scanner console = new Scanner(System.in);

System.out.println("Make a choice: 1) Run 2) Jump 3) Fly 4) Exit");
choice = console.nextLine();

switch(choice)
{
case "1": Run(); break;
case "2": Jump(); break;
case "3": Fly(); break;
case "4": System.exit(0);
}
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 06:48pm
Quote (Minkomonster @ Jan 25 2015 07:59pm)
Code
String choice;
Scanner console = new Scanner(System.in);

System.out.println("Make a choice: 1) Run 2) Jump 3) Fly 4) Exit");
choice = console.nextLine();

switch(choice)
{
case "1": Run(); break;
case "2": Jump(); break;
case "3": Fly(); break;
case "4": System.exit(0);
}


I am not sure what I'm doing wrong when I input the System.exit(0); in, but it doesn't do anything.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 06:50pm
What do you mean input it in?
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 07:00pm
Quote (Minkomonster @ Jan 25 2015 08:50pm)
What do you mean input it in?


I'm not sure myself..I guess I should be asking why I am getting an error and am prompted to make "system" in the "system.exit" part a: variable, class, constant, or.. etc.

Is this a necessary step? I'm monkeying with different stuff in eclipse right now and just testing each piece trying to understand the reason its being used. I'm not certain when and why we use different tabs for classes and if it's necessary. I know on the BMI demo you showed me, you used multiple tabs to represent a class per tab. I've grasped the definition of class, but foggy on mechanics.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 07:05pm
Quote (axid3nt @ Jan 25 2015 08:00pm)
I'm not sure myself..I guess I should be asking why I am getting an error and am prompted to make "system" in the "system.exit" part a: variable, class, constant, or.. etc.

Is this a necessary step? I'm monkeying with different stuff in eclipse right now and just testing each piece trying to understand the reason its being used. I'm not certain when and why we use different tabs for classes and if it's necessary. I know on the BMI demo you showed me, you used multiple tabs to represent a class per tab. I've grasped the definition of class, but foggy on mechanics.


Show me what you have so far. Copy and paste it here
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 07:14pm
Quote (Minkomonster @ Jan 25 2015 09:05pm)
Show me what you have so far. Copy and paste it here


I hit the X on the left task bar, let me figure out how to restore it (the place that shoes your projects, packages, classes open)
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 07:16pm
okay.. this is just a blob of code I've constantly been changing to experiment with:

Code
import java.util.Random;
import java.util.Scanner;

public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Menu: Welcome to Basic Calculator \n "
+ "1. Add \n "
+ "2. Subtract \n "
+ "3. Divide \n "
+ "4. Multiply \n "
+ "5. Generate Random Number \n "
+ "6. Quit \n ");
//Menu for Operations

String in = input.next();
char oper = in.charAt(0);

System.out.print("Enter your first number ");
in = input.next();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.next();
double num2 = Double.parseDouble(in);




if(oper == '1') {
//double result = num1 + num2;
//System.out.println(result);
System.exit(0);
} else if(oper == '5') {
double result = (num1 * num2 + num1 + num2 + 77);
System.out.println(result);
} else if(oper == '2') {
double result = num1 - num2;
System.out.println(result);
} else if(oper == '4') {
double result = num1 * num2;
System.out.println(result);
} else if(oper == '3') {
double result = num1 / num2;
System.out.println("Your answer is: " + result);
}
else if(oper == '6') {
System.exit(0);
}
{

}
System.out.println("Thank you for using Basic Calculator!");
}
}
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll