Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: my loops are not working like they should

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default my loops are not working like they should

    import javax.swing.*;

    import java.awt.Container;
    import java.math.*;

    public class arithmetic extends JApplet {

    public void init() {

    String input = JOptionPane.showInputDialog
    ("Select an option (1(multiply),2(divide),3(add),4(subtract))");
    int input2 = Integer.parseInt(input);
    int choice = input2;


    switch (choice) {

    case 1:
    for (int x = 1; x < 4; x++) {


    double number1 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number1,null,JO ptionPane.
    INFORMATION_MESSAGE);
    double number2 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number2,null,JO ptionPane.
    INFORMATION_MESSAGE);
    String numberx = JOptionPane.showInputDialog("Enter result");
    double number3 = Double.parseDouble(numberx);
    double result = number1 * number2;

    if (result == number3) {
    JOptionPane.showMessageDialog(null,"correct",null,
    JOptionPane.INFORMATION_MESSAGE);

    }
    else
    JOptionPane.showMessageDialog(null,"wrong",null,
    JOptionPane.INFORMATION_MESSAGE);


    }
    break;
    case 2:
    for (int x = 1; x < 4; x++) {
    double number4 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number4,null,JO ptionPane.
    INFORMATION_MESSAGE);
    double number5 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number5,null,JO ptionPane.
    INFORMATION_MESSAGE);
    String number7 = JOptionPane.showInputDialog("Enter result");
    double number6 = Double.parseDouble(number7);
    double result2 = Math.round(number4 / number5);


    if (result2 == number6) {
    JOptionPane.showMessageDialog(null,"correct",null,
    JOptionPane.INFORMATION_MESSAGE);
    }
    else
    JOptionPane.showMessageDialog(null,"wrong",null,
    JOptionPane.INFORMATION_MESSAGE);

    }
    break;
    case 3:
    for (int x = 1; x < 4; x++) {
    double number8 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number8,null,JO ptionPane.
    INFORMATION_MESSAGE);
    double number9 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number9,null,JO ptionPane.
    INFORMATION_MESSAGE);
    String number10 = JOptionPane.showInputDialog("Enter result");
    double number11 = Double.parseDouble(number10);
    double result3 = number8 + number9;


    if (result3 == number11) {
    JOptionPane.showMessageDialog(null,"correct",null,
    JOptionPane.INFORMATION_MESSAGE);
    }
    else
    JOptionPane.showMessageDialog(null,"wrong",null,
    JOptionPane.INFORMATION_MESSAGE);
    }
    break;
    case 4:
    for (int x = 1; x < 4; x++) {
    double number12 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number12,null,J OptionPane.
    INFORMATION_MESSAGE);
    double number13 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number13,null,J OptionPane.
    INFORMATION_MESSAGE);
    String number14 = JOptionPane.showInputDialog("Enter result");
    double number15 = Double.parseDouble(number14);
    double result4 = number12 - number13;


    if (result4 == number15) {
    JOptionPane.showMessageDialog(null,"correct",null,
    JOptionPane.INFORMATION_MESSAGE);
    }
    else
    JOptionPane.showMessageDialog(null,"wrong",null,
    JOptionPane.INFORMATION_MESSAGE);


    }
    }
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: my loops are not working like they should

    Did you have a question, or did you just want to dump your code here?

    In other words, please use the highlight tags and ask a specific technical question, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my loops are not working like they should

    My question is what is wrong with the loops, as they are exceeding 3.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: my loops are not working like they should

    Like I said, please use the highlight tags and show us exactly what line you're talking about, exactly what behavior you're seeing, and exactly why that behavior is incorrect.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my loops are not working like they should

    Quote Originally Posted by KevinWorkman View Post
    Like I said, please use the highlight tags and show us exactly what line you're talking about, exactly what behavior you're seeing, and exactly why that behavior is incorrect.
    case 1:
    for (int x = 1; x < 4; x++) {

    case 2:
    for (int x = 1; x < 4; x++) {


    case 3:
    for (int x = 1; x < 4; x++) {

    case 4:
    for (int x = 1; x < 4; x++) {

  6. #6
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: my loops are not working like they should

    That is a lot of closing brackets at the end. It is hard to tell with it unformatted, but I'd suspect you need to clean up and check your brackets.

Similar Threads

  1. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  2. My loops are working, but not giving right output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 14th, 2013, 07:55 PM
  3. Working with loops for the first time.
    By JBtheIV in forum What's Wrong With My Code?
    Replies: 24
    Last Post: February 11th, 2013, 05:45 AM
  4. [SOLVED] Boolean Flag Not Working to Move Between While Loops?
    By verbicidalmaniac in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 8th, 2011, 09:04 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM