English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to write a Java Program that teaches multiplication by displaying a multiplication problem (i.e. How much is 6 times 7 with 6 and 7 being the random number). Then the person is supposed to guess/answer the problem. If they get it right then a message is displayed "Very Good" and ask another question and if wrong "No. Please try again." and repeat the question. I have managed with some assistance to figure out how to get most of the program but I cannot figure out how to loop the whole thing so that it continues to ask more questions after the user gets the answer correct. Help is appreciated
import java.util.Random;
import java.util.Scanner;

public class Multiplication1
{
public static void main (String args [] )
{
Scanner input = new Scanner( System.in );
Random generator = new Random(); // random number generator
int x = (int)(Math.random()*12);
int y = (int)(Math.random()*12);
int z = x*y;
int studentAnswer;
System.out.print("\nHow much is " +x+ " times " +y+ "?" );
studentAnswer = input.nextInt();
if ( studentAnswer == z )
System.out.println( "\nVery Good!\n" );
while (studentAnswer != z )
{
System.out.println( "\nNo. Please try again.\n" );
System.out.print("\nHow much is " +x+ " times " +y+ "?" );
studentAnswer = input.nextInt();
} // end inner while
}
}

2007-03-01 05:59:04 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

1 answers

There are dozens of answers, but let me give you two path trees to follow.

1st: The goal of your main program (the answer to the question your program is answering) appears to be checking the answer to one question rather than your stated goal. Make it instead a check to see if the program should end or not. while input != "quit" then all of your other stuff wrapped up in brackets.

2nd: The model that will get you farther is to start parting up your program into pieces. Make your problem and answer solution into a class that returns values of x,y, and z
Then make your input loop from above invoke the problems. In the future this will allow you to add new types of problems just by overloading the object.

2007-03-01 06:22:37 · answer #1 · answered by Jason W-S 4 · 0 0

fedest.com, questions and answers