New and have a question on a probably simple question.
I am new to Java and I am working on a simple Algebra converter program to help out some of my friends in math class.
I know the code is probablly not done properly ( I'm still working out the kinks and am no where near done), but I am having a menu problem
when I import a class ive made for each individual part of the program the whole program closes when I exit just that one class.
Here's the code for each one.
Main.java
Code:
import java.util.Scanner;
public class Main
{
static int Choice;
static int Menu;
public static void main(String[] args)
{
Menu = 0;
while (Menu == 0)
{
System.out.println("Welcome to the Algebra Converter V-1.0");
System.out.println();
System.out.println("choose one of the following conversion types below:");
Scanner x = new Scanner(System.in);
System.out.println("1) for Quadratic to Vertex form");
System.out.println("2) for Quadratic formula");
System.out.println("3) to exit");
Choice = x.nextInt();
System.out.println();
System.out.println();
if(Choice == 1)
{
QuadToVertex prog1 = new QuadToVertex();
prog1.main();
}
if(Choice == 2)
{
QuadForm prog2 = new QuadForm();
prog2.main();
}
if (Menu == 3);
System.exit(0);
System.out.println();
System.out.println("---------------------------------------------------");
System.out.println();
}
}
}
QuadForm.java
Code:
import java.util.Scanner;
public class QuadForm
{
static double a;
static double b;
static double c;
static double r;
static double R;
static String imag;
public void main()
{
int program;
program =0;
while(program ==0)
{
imag = "";
System.out.println("Please enter your a, b, and c values below:");
Scanner input = new Scanner(System.in);
System.out.print("a = ");
a = input.nextDouble();
System.out.println();
System.out.print("b = ");
b = input.nextDouble();
System.out.println();
System.out.print("c = ");
c = input.nextDouble();
System.out.println();
if((b*b)-(4*a*c) >= 0)
{
r = (-b - Math.sqrt((b*b)-(4*a*c)))/(2*a);
R = (-b + Math.sqrt((b*b)-(4*a*c)))/(2*a);
}
if((b*b)-(4*a*c)< 0)
{
imag = "i";
r = Math.abs((-b - Math.sqrt((b*b)-(4*a*c)))/(2*a));
R = Math.abs((-b + Math.sqrt((b*b)-(4*a*c)))/(2*a));
}
System.out.println("x = "+imag+r+" or "+imag+R);
}
}
}
if I could get some help on this and maybe some help on how to better clean up my code I would greatly appreciate it.
Re: New and have a question on a probably simple question.
Calling System.exit() is going to close your whole program. You probably want a branching statement instead: Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: New and have a question on a probably simple question.
Hello. I'm not a java expert, so be sure to consult with an expert. But...
In class Main - shouldn't line 31 read if (choice == 3) { and then the system.exit will only operate if user chooses 3.
Re: New and have a question on a probably simple question.
Ah, good catch. Yeah, ending your if statement with a semicolon basically short-circuits it and always performs the next line.
Re: New and have a question on a probably simple question.
Quote:
Originally Posted by
Humphrey Bogart
Hello. I'm not a java expert, so be sure to consult with an expert. But...
In class Main - shouldn't line 31 read if (choice == 3) { and then the system.exit will only operate if user chooses 3.
yeah i was in the middle of just testing the menu and never changed menu back to choice lol
and thank you KevinWorkman ill check out the tutorial
Re: New and have a question on a probably simple question.
i fixed the problem, I had used break before but i guess i wasnt using it right or something, but thank you for the help
btw how do i finish the tread? do i mark it as [SOLVED] or what? and how?
Re: New and have a question on a probably simple question.
Quote:
Originally Posted by
legionunity
i fixed the problem, I had used break before but i guess i wasnt using it right or something, but thank you for the help
btw how do i finish the tread? do i mark it as [SOLVED] or what? and how?
You should be able to edit the post and mark it as solved.
Re: New and have a question on a probably simple question.
Why do you have the code set up like:
Code:
Menu = 0;
while (Menu == 0)
{
AND
Code:
program =0;
while(program ==0)
{
This is what a 'do while' loop is for...
If you have to use 'breaks', your generally doing something wrong..
Re: New and have a question on a probably simple question.
what would you use instead?
Re: New and have a question on a probably simple question.
Quote:
Originally Posted by
legionunity
what would you use instead?
Like he said, he'd use a do-while. You should look into them, although I don't necessarily agree that a break means you're doing something wrong.
Re: New and have a question on a probably simple question.
Quote:
Originally Posted by
KevinWorkman
\
although I don't necessarily agree that a break means you're doing something wrong.
It is what our teacher tells us about once a week.. Mind giving me an example, excluding recursion, of where they would be needed? Where regular code would not suffice... Or a link.
Re: New and have a question on a probably simple question.
Validating Lists of things.
If one of them is wrong, break.
The likes of indexOf on a Collection does the equivalent, in that it returns when it finds the object in question, essentially breaking the loop.
Re: New and have a question on a probably simple question.
Quote:
Originally Posted by
penguinCoder
It is what our teacher tells us about once a week.. Mind giving me an example, excluding recursion, of where they would be needed? Where regular code would not suffice... Or a link.
It might make sense in the context of your class, I'm not sure how your instructor is teaching things. But as a general rule, I don't think you can say that branching statements are inherently bad.
Another example is finding something in a List:
Code:
Object thingToLookFor = null;
for(Object thing : things){
if(thing.isSomeCriteria()){
//found it!
thingToLookFor = thing;
//we don't need to finish the loop
break;
}
}
There are a ton of other examples, such as doing some calculations in a loop (and once some stopping criteria is met, breaking out of that loop). So you can't say as a general rule that branching statements are bad. But maybe that makes more sense for how your instructor has organized your lessons, I don't know.