Thread: Java Help
View Single Post
  #3 (permalink)  
Old 04-01-2008, 03:29 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.util.Random; import java.util.Scanner; public class MultiplyTest { static Random seed = new Random(); static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); multiply(); scanner.close(); } private static void multiply() { int response = 0; while(response != -1) { int x = 1 + seed.nextInt(10); // [ 1 <= x <= 10] int y = 1 + seed.nextInt(10); System.out.print("How much is " + x + " times " + y + " ? : "); try { response = Integer.parseInt(scanner.nextLine()); } catch(NumberFormatException e) { System.out.println("number format: " + e.getMessage()); } while(response != -1 && response != x*y) { System.out.print("No. Please try again: "); try { response = Integer.parseInt(scanner.nextLine()); } catch(NumberFormatException e) { System.out.println("number format: " + e.getMessage()); } } if(response != -1) System.out.println("Very Good"); } } }
Reply With Quote