Homework 3 Solution 2

Nextint whileguess1 answercheckresponseguessanswer

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: "+ side2); System.out.println("Hypotenuse = "+ hypotenuse +"\n"); } }}} }} 3.Multiply.java The program successfully compiles and runs. import java.util.*; public class Multiply { //random number generator Random randomNumbers = new Random(); Scanner input = new Scanner(System.in); int answer; //the correct answer //ask the user to answer multiplication problems public void quiz() { Scanner input = new Scanner(System.in); int guess = 0; //the users guess answer = checkResponse(guess,answer); System.out.println("Enter your answer (­1 to exit): "); guess = input.nextInt(); while(guess != ­1) { answer = checkResponse(guess,answer); System.out.println("Enter your answer (­1 to exit)"); guess = input.nextInt(); }//end while loop }//end quiz method public int createQuestion() { //get two random numbers between 0 and 9 int digit1 = randomNumbers.nextInt(9); int digit2 = randomNumbers.nextInt(9); //write code to multiply the two variables and store them in a variable answer = digit1 * digit2; System.out.printf("\nHow much is %d times %d?\n", digit1, digit2); return answer; }//end method createQuestion public int checkResponse(int guess,int answer) { if(guess != answer) { System.out.println("Wrong. Please try again. "); } else { System.out.println("You...
View Full Document

This document was uploaded on 03/26/2014 for the course CSE 292 at CSU San Bernardino.

Ask a homework question - tutors are online