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 4 of 4

Thread: loop issues

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

    Default loop issues

    I am having loop issues:

    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);
     
     
    }
    }
    }
    }
    Last edited by jps; May 27th, 2013 at 09:40 PM. Reason: fixed code tag


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: loop issues

    What issues particularly? Can you describe them?

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

    Default Re: loop issues

    The loops repeat more than 3 times
    Quote Originally Posted by angstrem View Post
    What issues particularly? Can you describe them?

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: loop issues

    I've just tested your code. Everything correct, it executes exactly 3 times.

Similar Threads

  1. Split String and for loop issues. (Beginner problems)
    By AT-LOW in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2012, 02:02 PM
  2. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  3. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  4. Basic while loop issues
    By Litost in forum Loops & Control Statements
    Replies: 1
    Last Post: October 8th, 2012, 07:15 AM
  5. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM