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

Thread: Multiplication program Loop

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking Multiplication program Loop

    Hi guys! I have created a program that is designed to ask students multiple equation questions. The program will only be designed to ask single digit numbers. I have managed to create the questions and a random number generator, but i want this program to constantly loop, any ideas on how to implement a loop?

    Heres my code:
    import java.util.Scanner;
     
    public class SubtractionQuiz {
    public static void main (String[] args) {
    int number1 = (int) (Math.random() * 10);
    int number2 = (int) (Math.random() * 10);
     
     
     
    System.out.println("What is" + number1 + " * " + number2 + "?");
    Scanner input = new Scanner(System.in);
    int answer = input.nextInt();
     
     
    if (number1 * number2 == answer)
    System.out.println("You are correct!");
    else
    System.out.println("Wrong!");
     
     
     
    }
    }

    Thanks in advance!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Multiplication program Loop

    Keep a flag that determines whether to quit or not, and evaluate that within a while or do/while loop
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiplication program Loop

    I want it to quit if a number like -1 is entered, could you maybe give me some examples of my code on how to do something like this?

    I have tried something like this:
     
    import java.util.Scanner;
     
    public class SubtractionQuiz {
    public static void main (String[] args) {
    int number1 = (int) (Math.random() * 10);
    int number2 = (int) (Math.random() * 10);
     
     
     
    do{
    System.out.println("What is" + number1 + " * " + number2 + "?");
    Scanner input = new Scanner(System.in);
    int answer = input.nextInt();
     
     
    if (number1 * number2 == answer)
    System.out.println("You are correct!");
    else
    System.out.println("Wrong!");
     
    }while (answer != -1);
    }
    }

    but it does not seem to be working
    Last edited by PhilThomspon; May 6th, 2012 at 01:20 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Multiplication program Loop

    but it does not seem to be working
    Phrases such as this are not useful to us. Define not working...

    Think about your logic - write it out on paper if you have to - step through line by line and think what would happen based upon the current code - the result should be what is produced when you run the code (if not you need to do this step again and understand what is happening). Then think about your goal and how you can rearrange the code to meet that goal

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiplication program Loop

    I really do not have time for something like that, this program needs to be working by tomorrow morning for the kids and i was only given this on Friday just gone, i will have my friend help me but anyone who can come in and save the day i will be very grateful.

  6. #6
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiplication program Loop

    Here, I put in a line of code that loops the program.

    ...removed by moderator, although too late I might add
    Last edited by copeg; May 6th, 2012 at 03:32 PM.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiplication program Loop

    Thank you so much! I can now go to sleep early!

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Multiplication program Loop

    @Firestar912, I highly recommend reading the forum rules and the following:

    http://www.javaprogrammingforums.com...n-feeding.html

    There is a high chance you just contributed to academic dishonesty...nice going.

Similar Threads

  1. Loop ordering in matrix multiplication
    By murph in forum Loops & Control Statements
    Replies: 3
    Last Post: November 24th, 2011, 03:08 AM
  2. Matrix multiplication problem with different dimensions
    By mightyking in forum Collections and Generics
    Replies: 5
    Last Post: September 25th, 2011, 04:59 PM
  3. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 01:41 PM
  4. Recursive multiplication and Karatsuba
    By jsp01 in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 14th, 2011, 02:04 AM
  5. Multiplication code
    By bomboy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 5th, 2011, 02:25 AM