Results 1 to 13 of 13
  1. #1
    legionunity is offline legionunityproject.org
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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
    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

    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.
    Last edited by legionunity; 10-13-2012 at 06:11 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default 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)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default 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.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    legionunity is offline legionunityproject.org
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: New and have a question on a probably simple question.

    Quote Originally Posted by Humphrey Bogart View Post
    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

  6. #6
    legionunity is offline legionunityproject.org
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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?

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: New and have a question on a probably simple question.

    Quote Originally Posted by legionunity View Post
    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: New and have a question on a probably simple question.

    Why do you have the code set up like:
    Java Code:
            Menu = 0;
            while (Menu == 0)
            {
    AND
    Java 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..

  9. #9
    legionunity is offline legionunityproject.org
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: New and have a question on a probably simple question.

    what would you use instead?

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: New and have a question on a probably simple question.

    Quote Originally Posted by legionunity View Post
    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: New and have a question on a probably simple question.

    Quote Originally Posted by KevinWorkman View Post
    \
    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.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: New and have a question on a probably simple question.

    Quote Originally Posted by penguinCoder View Post
    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:

    Java 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Simple question
    By Games2Design in forum New To Java
    Replies: 10
    Last Post: 09-30-2012, 07:54 PM
  2. A simple question
    By chris.bos in forum New To Java
    Replies: 11
    Last Post: 12-02-2011, 05:37 PM
  3. simple question
    By agater in forum New To Java
    Replies: 5
    Last Post: 11-07-2011, 02:22 AM
  4. Please help. Simple question
    By owencain in forum New To Java
    Replies: 14
    Last Post: 06-16-2011, 01:07 AM
  5. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 01:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •