The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Code

2 replies on 1 page. Most recent reply: Mar 9, 2002 12:13 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Tim

Posts: 5
Nickname: anna
Registered: Mar, 2002

Java Code Posted: Mar 8, 2002 8:08 PM
Reply to this message Reply
Advertisement
I am trying to write a program for the following output:
How many problems would you like?2
Question 1: What is 6 x 8?48
Correct!
Question 2: What is 7 x 3? 28
No, 7 x 3 is 21
Your score is 50.0%

What i have so far is
int count, problems;
System.out.print( "How many problems would you like? " );
problems = kb.getInt();
count = 1;
while (count <=problems)
{
System.out.println ( "Question" + count + ": What is " +
(int) (Math.random() * 12) + 1);
System.out.println ( "Correct" );
System.out.println ( "No" );
count++;
}
System.out.println ( "Your score is" );

I know i need to use a randomly generated expression like (int) (Math.random() * 12) + 1 but i am having trouble trying to get it to work. Any ideas on this program?


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Java Code Posted: Mar 8, 2002 11:21 PM
Reply to this message Reply
You need to get two random numbers and then multiply them to get a result. That means, at least three variables, probably. Then, you want to read in the user's guess (!) to another variable (using Integer.parseInt(), probably) and compare that to the result and say whether the guess was right. Of course, you also need to keep track of the tally in order to give the percentage each time and at the end.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Java Code Posted: Mar 9, 2002 12:13 PM
Reply to this message Reply

/* ProblemGenerator.java
* @author: Charles Bell
* @version: Mar 9, 2002
* email: charbell@bellsouth.net
*
*/

import java.io.*;
import java.util.*;

/** ProblemGenerator
*/
public class ProblemGenerator{

Random random = new Random();

/**
*/
public static void main(String[] args){

ProblemGenerator problemgenerator = new ProblemGenerator();
problemgenerator.generate();

}

/** Queries the user to enter the number of problems to
* generate, generates them, poses the problem, and prompts
* for an answer, displays feedback and the correct answer,
* if missed, and the cumulative score.
*/
public void generate (){

showMessage("How many problems would you like?");
int problems = getPositiveInteger();
showMessage("Generating " + String.valueOf(problems) + " problems.");
int score = 0;
int correctresponses = 0;
for (int i = 0; i < problems;i++){
int first = getRandomInt(1,12);
int second = getRandomInt(1,12);
String question = String.valueOf(first) + " x " +
String.valueOf(second);
showMessage("Question: " + question + "?");
int useranswer = getPositiveInteger();
int correctanswer = first * second;
if (useranswer == correctanswer){
showMessage("Correct!");
correctresponses++;
}else{
showMessage("No, " + question + " is " +
String.valueOf(correctanswer));
}
showMessage(score(i+1,correctresponses));
}
showMessage("Your final score was " + score(problems,correctresponses));

}

/** Returns a random integer between low and high, inclusive.
* Random.nextInt(int n) returns next random between 0
* inclusive and n exclusive, so must adjust for low and high.
*/
public int getRandomInt(int low, int high){
int highinclusive = high + 1;
int n = random.nextInt(highinclusive - low);
return low + n;
}

/** Returns a string corresponding to the percentage
* of a correct answers a out of n problems.
*/
public String score(int n, int a){
float ratio = 0f;
if (n > 0) {
ratio = (float) a / (float) n;
}
float percent = ratio * 100f;
return String.valueOf(percent + "%");
}

/** Displays message string to System.out.
*/
public void showMessage(String message){
System.out.println(message);
}

/** Loops until the user enters a positive integer.
*/
public int getPositiveInteger(){
int n = -1;
while (n < 0){
String input = getUserInput();
if (isInteger(input)){
n = toInt(input);
}
}
return n;
}

/** Obtains a line of input from the user using
* InputStream System.in.
*/
public String getUserInput(){
String lineread = "";
try{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
lineread = br.readLine();
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
return lineread;
}

/** Converts the input string to an integer
* or returns -1 if unsucessful.
*/
public int toInt(String s){
int n = -1;
try{
n = Integer.parseInt(s);
}catch(NumberFormatException nfe){
System.err.println("NumberFormatException: " + nfe.getMessage());
System.err.println(s + " can not be converted to an integer.");
}
return n;
}

/** Returns true if the input string can be converted
* to an integer. False otherwise.
*/
public boolean isInteger(String s){
boolean status = false;
try{
int n = Integer.parseInt(s);
//program execution will not reach next line
//unless parse is sucessful.
status = true;
}catch(NumberFormatException nfe){
status = false;
}
return status;
}
}

Flat View: This topic has 2 replies on 1 page
Topic: Variables Previous Topic   Next Topic Topic: JSP And Servlets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use