3

Hello everyone I am new to the site and this is my first question from my Java programming class. I have to create a program that asks a math question and tells the user if he is right or wrong, but the requirements also state that I need to create a method that generates a new question if the first question is correct, so when the computer asks what is 5 times 5 and the user inputs 25 the method should generate two new random numbers and ask the user for a result.

This is my code so far. I don't expect the answers as this is a school assignment but if anyone could give a direction it would be greatly appreciated it as this is my first java college course.

import java.security.SecureRandom; //program uses class SecureRandom
import java.util.Scanner; //program uses class Scanner

public class CAI 
{
public static void main(String[] args)
{   

    System.out.println("Alex - Assignment 4\n");

    //create Scanner for input from command window
    Scanner input = new Scanner(System.in);

    //randomNumbers object will produce secure random numbers
    SecureRandom randomNumbers = new SecureRandom();

    //generates two random numbers from 1 to 9 excluding 0
    int random1 = 1+ randomNumbers.nextInt(9);
    int random2 = 1+ randomNumbers.nextInt(9);

    int answer; // declares answer from user

    //calculates real result of first integer times second integer
    int result = (random1 * random2);

    //display generated integers
    System.out.printf("What is %d times %d?\n",random1, random2);

    do 
    {
    answer = input.nextInt(); //keeps taking answer from user if wrong
     if(answer == result) //if correct answer then print very good!
      System.out.println("Very Good!");
     else // if wrong answer then print no please try again
      System.out.println("No. Please try again");
    }
    while (answer != result);
 }
2
  • 1
    You should move the code that generates a number and prompts a user until they get it right into a method; then, add a new loop to call that method repeatedly Commented Apr 18, 2016 at 21:55
  • 1
    Since this appears to be homework, I don't want to give too much away. But it looks like you've understood the basics... You understand loops (you have one checking the answer). So you'll want to loop through the creation of a question step too. You just need to figure out where the loop starts/ends (i.e. what's inside it). Commented Apr 18, 2016 at 22:11

3 Answers 3

1

I think you have the basic way that your loop statements work mixed up. A do statement is going to execute its block of code once and wont inherently loop on its own. A while loop will repeat until you tell it to stop. So without telling you exactly how to structure your assignment ;) you should look at those two things. But your code does compile and does do one run through of what you want it to do. So this means that the problem you have is in the logic aspect of your code. This means that the computer doesn't understand based on the structure of your code when to execute the sections of your code.

So my advice is to try writing it out in plain English first (pseudocode) that way you can work out how the logic of your program should run and then translate it into code. Sometimes just saying "I want x to happen when y. But I only want this to happen if event z has happened." can help you understand logical how something has to work.

Best of luck

Sign up to request clarification or add additional context in comments.

Comments

0

You could add a while loop before the generation of the random numbers that would repeat until answer== "exit". Something along those lines would work fine

2 Comments

answer is an int, so it'll never be equal to "exit", and if it could you wouldn't want to use == to check.
You are right, answer==-1 should work instead since the answer will always be positive
0

You should put a break; statement in the bottom else loop and put everything from your public static void declaration in an "infinite" for loop. When the user inputs an incorrect answer, the program will go to the else loop and break. Otherwise, it will keep on repeating in the "infinite" for loop. Here is a sample code showing what you could do.

import java.security.SecureRandom; //program uses class SecureRandom
import java.util.Scanner; //program uses class Scanner

public class CAI 
{
public static void main(String[] args)
{   

boolean loopTest = false;

while (loopTest= true)
{

System.out.println("Alex - Assignment 4\n");

//create Scanner for input from command window
Scanner input = new Scanner(System.in);

//randomNumbers object will produce secure random numbers
SecureRandom randomNumbers = new SecureRandom();

//generates two random numbers from 1 to 9 excluding 0
int random1 = 1+ randomNumbers.nextInt(9);
int random2 = 1+ randomNumbers.nextInt(9);

int answer; // declares answer from the user

//calculates real result of first integer times second integer
int result = (random1 * random2);

//display generated integers
System.out.printf("What is %d times %d?\n",random1, random2);

do 
{
answer = input.nextInt(); //keeps taking answer from user if wrong
 if(answer == result) //if correct answer then print very good!
  System.out.println("Very Good!");
 else // if wrong answer then print no please try again
  System.out.println("No. Please try again");
  break;
}
while (answer != result);
}

}
}

*Note that the last } was not included in your program.

1 Comment

while(true) is way easier than your for loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.