Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Basic Java Help

Options
  • 19-10-2007 2:36pm
    #1
    Registered Users Posts: 1,380 ✭✭✭


    Hi guys, I'v just started learning Java, and am experimenting with making a program to do so basic maths. The way I'v done it is probably pretty inefficient, but it's all I know so far. The problem is that the menu selection system I'v tryed to make dosent work, the program dosen't recognize which option has been picked and just runs down through the whole sequence. Any advice on how to do this properly? So the computer recognizes which option has been picked and runs the correct one

    note: It compiles perfectly
    // Math program


    import javax.swing.JOptionPane;

    public class calc
    {
    public static void main(String[] args)
    {
    String mainmenuS = JOptionPane.showInputDialog("Please select one of the following calculator operations: 1. Arithmetic 2. Area & Volume");
    int mainmenu = Integer.parseInt(mainmenuS);

    // Option A - Basic Arithmetic
    boolean arselect = mainmenu==1;
    if (arselect);

    String armenuS = JOptionPane.showInputDialog("What kind of arithmetic would you like to perform?: 1. Addition 2. Subtraction 3. Multiplication 4. Divison");
    int armenu = Integer.parseInt(armenuS);



    // Option A1 - Addition
    boolean addselect = armenu==1;
    if (addselect);
    String addnumoneS = JOptionPane.showInputDialog("Enter the first number you would like to add");
    int addnumone = Integer.parseInt(addnumoneS);
    String addnumtwoS = JOptionPane.showInputDialog("Enter a number to add to " +addnumone);
    int addnumtwo = Integer.parseInt(addnumtwoS);
    int addsum = addnumone + addnumtwo;
    JOptionPane.showMessageDialog(null, "The sum of the two numbers is " +addsum);



    // Option A2 - Subtraction
    boolean subselect = armenu==2;
    if (subselect);
    String subnumoneS = JOptionPane.showInputDialog("Enter the number you would like to subtract from");
    int subnumone = Integer.parseInt(subnumoneS);
    String subnumtwoS = JOptionPane.showInputDialog("Enter a number to take away from " +subnumone);
    int subnumtwo = Integer.parseInt(subnumtwoS);
    int subsum = subnumone-subnumtwo;
    JOptionPane.showMessageDialog(null, "Answer is equal to " +subsum);




    // Option B - Basic Area & Volume
    boolean volselect = mainmenu==2;
    if (volselect);
    String volmenuS = JOptionPane.showInputDialog("Which calculation would you like to perform?: 1. Area Of A Circle 2. Area Of A Rectangle 3. Volume Of A Sphere");
    int volmenu = Integer.parseInt(volmenuS);

    // Option B1 - Circle Area
    boolean circselect = volmenu==1;
    if (circselect);
    String radiusS = JOptionPane.showInputDialog("Enter circle radius:");
    int radius = Integer.parseInt(radiusS);
    JOptionPane.showMessageDialog(null, "Circle of radius " +radius +" has an area of " +radius*2*Math.PI);

    // Option B2 - Rectangle Area
    boolean rectselect = volmenu==2;
    if (rectselect);
    String lengthS = JOptionPane.showInputDialog("Enter rectangle length:");
    String breadthS = JOptionPane.showInputDialog("Enter rectangle breadth:");
    int length = Integer.parseInt(lengthS);
    int breadth = Integer.parseInt(breadthS);
    JOptionPane.showMessageDialog(null, "Rectangle of length " +length +" and breadth " +breadth +" has an area of " +length*breadth);

    // Option B3 - Sphere Volume
    boolean sphselect = volmenu==3;
    if (sphselect);
    String radius2S = JOptionPane.showInputDialog("Enter sphere radius:");
    int radius2 = Integer.parseInt(radius2S);
    JOptionPane.showMessageDialog(null, "Sphere of radius " +radius2 +" has a volume of " +radius2*3*4/3*Math.PI);




    System.exit(0);
    }
    }


Comments

  • Registered Users Posts: 1,636 ✭✭✭henbane


    You should surround all the statements you want executed in the if sections with {}

    e.g
    if (arselect){
       String armenuS = JOptionPane.showInputDialog("What kind of arithmetic would you like to    perform?: 1. Addition 2. Subtraction 3. Multiplication 4. Divison");
       int armenu = Integer.parseInt(armenuS);
    }
    


  • Registered Users Posts: 901 ✭✭✭EL_Loco


    now I haven't done Java since college, but your "IF" statement doesn't seem to start anything, and your program just runs through all of the code because the statements aren't enclosed within an "if" condition.

    eg: yours seems to have just

    if(condition); // then nothing is happening

    it should look something like:

    if(condition)
    {
    statments
    }

    hope I'm close to the mark. ;) otherwise, apologies.

    EDIT: I see someone got there by the time I'd composed the message ;)


  • Registered Users Posts: 1,380 ✭✭✭remus808


    I'd thought of that, but unfortunatley it results in Java being unable to find the variables armenu and volmenu. Why I don't know, as they are defined just fine. Confusing :(


  • Registered Users Posts: 1,636 ✭✭✭henbane


    That's probably because they're both declared inside your if clauses. That would make them only exist in those clauses. Instantiate those variables at the start of the program and it should be fine.

    Post the revised code if you have any other problems


  • Closed Accounts Posts: 71 ✭✭Mach


    Hey instead of using if-statements try a switich

    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html


  • Advertisement
  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    Switch is more efficient - makes for cleaner code too.


Advertisement