Results 1 to 9 of 9
Thread: Help with loops
- 10-17-2010, 04:15 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
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");}}
}
- 10-17-2010, 05:00 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 10-17-2010, 05:59 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
Last edited by pg5678pg; 10-17-2010 at 06:25 PM.
- 10-17-2010, 06:00 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 10-17-2010, 06:02 PM #5
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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:
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.Java Code:while (!question1()) { System.out.println("Please try again!"); }
Much luck!
[edit: beaten by Moon!!]
- 10-17-2010, 06:08 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
- 10-17-2010, 06:23 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 10-17-2010, 06:38 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
-
I was the one who deleted Bgreen7887's post. I apologize for any confusion it caused.
Similar Threads
-
Help with loops!
By jrelvi23 in forum New To JavaReplies: 9Last Post: 09-04-2012, 01:36 PM -
need some help with loops!
By Chewart in forum New To JavaReplies: 2Last Post: 12-03-2009, 11:32 PM -
when should we use loops
By shahemaan in forum New To JavaReplies: 1Last Post: 10-31-2009, 01:38 AM -
these loops...
By Blaedel in forum New To JavaReplies: 0Last Post: 10-01-2009, 06:59 PM -
how to use do while loops
By mikeitalydz in forum New To JavaReplies: 32Last Post: 09-26-2009, 08:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks