intro/LoopExamples

From ggc

Jump to: navigation, search

01 package intro;
02 //start auto-imports
03 import java.util.*;
04 //end auto-imports
05 
06 import wiki.Wiki;
07 
08 /**
09  * All about my application.
10  @author My Name Here
11  */
12 public class LoopExamples
13 {
14   public static void main(String[] args)
15   {
16     System.out.print("Enter numerator: ");
17     Scanner scanner=new Scanner(Wiki.in);
18     double n=0;
19     String number=null;
20     boolean success=false;
21     while(success==false)
22     {
23       try
24       {
25         number=scanner.next();
26         n=Integer.parseInt(number);
27         System.out.println("Thanks, I see you want to divide "+number);
28         success=true;
29       }
30       catch(NumberFormatException e)
31       {
32         //success=false;
33         System.out.println("An error occurred: "+e);
34         System.out.println("Sorry, invalid input: "+number);
35         System.out.println("Please enter only integers like 5, 2, 24.");
36         System.out.print("Enter numerator: ");
37       }
38     }
39     System.out.print("Enter denomenator: ");
40     int d=scanner.nextInt();
41     double result=n/d;
42     if(d==0)
43     {
44       System.out.println("Sorry little brother, you can't divide by zero.");
45     }
46     else
47     {
48       System.out.println("When you divide "+n+" by "+d+" you get "+result);
49     }
50   }
51 }


Download/View intro/LoopExamples.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