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: how to loop correctly this program

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

    Wink how to loop correctly this program

    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 < 2; x++) {
    int i = 1;
    while( i < 2) {
    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);

    }
    }

    case 2:
    for (int x = 1; x < 2; 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);

    }
    case 3:
    for (int x = 1; x < 2; 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);
    }
    case 4:
    for (int x = 1; x < 2; 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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to loop correctly this program

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Can you explain the problem you are having?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: how to loop correctly this program

    [code = java]
    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 < 2; x++) {
    int i = 1;
    while( i < 2) {
    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);

    }
    }

    case 2:
    for (int x = 1; x < 2; 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);

    }
    case 3:
    for (int x = 1; x < 2; 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);
    }
    case 4:
    for (int x = 1; x < 2; 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);


    }
    }


    }
    }
    [/code]

    I am trying to loop 3 times; however the third time I loop, the answer is 99% wrong although the answer is correct.

    Eric

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to loop correctly this program

    Did you look at the code you just posted? Was it formatted?
    The statements should NOT all start in the first column. Nested statements need to be indented.

    the answer is 99% wrong although the answer is correct.
    Please explain how that works. 99% wrong and correct
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] For Loop not working correctly.
    By chrisob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 15th, 2012, 03:50 PM
  2. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 09:13 PM
  3. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 04:20 PM
  4. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM
  5. Need help getting things to loop correctly
    By egruna2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 9th, 2010, 04:53 PM