Results 1 to 9 of 9

Thread: Help with loops

  1. #1
    pg5678pg is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Post Help with loops

    I am trying to to make a simple math question that the user will answer, and if the answer is solved it will move on to the next question, if not it will repeat the question until its solved. I cant figure out how to loop the question until solved. Ive tried using the if and while staement nut cant get it to work. Is there a simple way to do this, I only started learning java yesterday so it has to be fairly simple. This is what ive got so far:


    import java.util.Scanner;


    public class MathQuestion {

    public static void main(String[] args) {

    Question1();
    Question2();

    }
    public static void Question1(){
    Scanner scan = new Scanner(System.in);
    System.out.println("23 + 19 = ");
    int x = scan.nextInt();
    if (x == 42) {System.out.println("Correct!");}
    else if (x != 42) {System.out.println("Wrong");}}

    public static void Question2(){
    Scanner scan = new Scanner(System.in);
    System.out.println("46 - 16 =");
    int y = scan.nextInt();
    if (y == 30) {System.out.println("Correct!");}
    else if (y != 30) {System.out.println("Wrong");}}

    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bgreen7887 View Post
    try something like this...
    Better the OP doesn't; throw that crappy program away.

    kind regards,

    Jos

  3. #3
    pg5678pg is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Better the OP doesn't; throw that crappy program away.

    kind regards,

    Jos
    Im sorry but did you read the post, it says i only started to learn java yesterday, im sorry im not an expert programmer after one day, why dont you piss off if you have no actual help.

    Sorry JosAH i thought that post was aimed at me, my mistake.
    Last edited by pg5678pg; 10-17-2010 at 06:25 PM.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    How about making your question methods return a boolean, that way you can use it as a condition of a do while loop.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    There are several ways to solve something like this. One thing you can do is rather than have your Question method (which should be renamed question method since methods should all begin with lower case letters) return a boolean, true if the question has been answered and false if not. Then your main method could do something as simple as:

    Java Code:
    while (!question1()) {
       System.out.println("Please try again!");
    }
    Please note that again this is not the only way to solve this, and in fact not how I'd solve this, as try to use OOPS ideas including creating a class to read in the question text from a file, creating a class to encapsulate the concept of a question (with fields to hold question text, correct answer, and possibly incorrect answers if multiple choice), creating a class that accepts a Question object and uses this to ask the user the question, .... but I'm guessing that you're not quite into creating classes yet.

    Much luck!

    [edit: beaten by Moon!!]

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by pg5678pg View Post
    Im sorry but did you read the post, it says i only started to learn java yesterday, im sorry im not an expert programmer after one day, why dont you piss off if you have no actual help.
    If you look at Jos' post, it should be clear that he is referring to a deleted post, not yours.

    @curmudgeon: maybe I beat you to the punch, but you elaborated way more :D
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pg5678pg View Post
    Im sorry but did you read the post, it says i only started to learn java yesterday, im sorry im not an expert programmer after one day, why dont you piss off if you have no actual help.
    I didn't refer to your original post; I was replying to an utmost crappy answer and I won't piss off because you suggested me to do so. Please pay attention, I didn't remove that crappy post.

    kind regards,

    Jos

  8. #8
    pg5678pg is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    There are several ways to solve something like this. One thing you can do is rather than have your Question method (which should be renamed question method since methods should all begin with lower case letters) return a boolean, true if the question has been answered and false if not. Then your main method could do something as simple as:

    Java Code:
    while (!question1()) {
       System.out.println("Please try again!");
    }
    Please note that again this is not the only way to solve this, and in fact not how I'd solve this, as try to use OOPS ideas including creating a class to read in the question text from a file, creating a class to encapsulate the concept of a question (with fields to hold question text, correct answer, and possibly incorrect answers if multiple choice), creating a class that accepts a Question object and uses this to ask the user the question, .... but I'm guessing that you're not quite into creating classes yet.

    Much luck!

    [edit: beaten by Moon!!]
    Thank you very much it works now.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 01:36 PM
  2. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-03-2009, 11:32 PM
  3. when should we use loops
    By shahemaan in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 01:38 AM
  4. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 06:59 PM
  5. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 08:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •