intermediate/HappyFun

From ggc

Jump to: navigation, search

01 package intermediate;
02 //start auto-imports
03 import sun.security.krb5.internal.ccache.*;
04 //end auto-imports
05 import java.util.*;
06 
07 public class HappyFun
08 {
09   public static void main(String[] args)
10   {
11     Scanner scanner=new Scanner(System.in);
12     int number=(int)(Math.random()*100)+1;
13     System.out.println("Guess a number 1-100.");
14     int guess=0;
15     while(guess!=number && guess!=-1)
16     {
17       String line=scanner.nextLine();
18       try
19       {
20         guess=Integer.parseInt(line);
21         if(guess>number)
22           System.out.println("Guess lower.");
23         else if(guess<number)
24           System.out.println("Guess higher.");
25         else
26           System.out.println("You guessed it!");
27       }
28       catch(NumberFormatException nfe)
29       {
30         System.out.println("Sorry, "+line+" is invalid.");
31         System.out.println("Please enter a number such as 1, 6, 67");
32         System.out.println("Enter -1 to quit.");
33       }
34     }
35   }
36 }


Download/View intermediate/HappyFun.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter