t805

Menu Program

Sep 10th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. package week2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Question3 {
  6.  
  7.     public static void main(String[] args){
  8.        
  9.             Scanner input = new Scanner(System.in);
  10.        
  11.             char letter = '\0';
  12.        
  13.             while(letter != 'q'){
  14.            
  15.             System.out.println("Enter one of these letters: a, b, c, d, e, q. q terminates the program!");
  16.             System.out.println("Enter here: ");
  17.            
  18.             letter = input.nextLine().trim().toLowerCase().charAt(0);
  19.            
  20.                     switch(letter){
  21.                         case 'a':
  22.                                 optionA();
  23.                                 break;
  24.                         case 'b':
  25.                                 optionB();
  26.                                 break;
  27.                         case 'c':
  28.                                 optionC();
  29.                                 break;
  30.                         case 'd':
  31.                                 optionD();
  32.                                 break;
  33.                         case 'e':
  34.                                 optionE();
  35.                                 break;
  36.                         case 'q':
  37.                                 optionQ();
  38.                                 break;
  39.                         default:
  40.                             System.out.println("Invalid letter entered. Try again!");
  41.             }
  42.         }
  43.     }
  44.  
  45.    
  46.         static void optionA(){
  47.        
  48.             System.out.println("Name: Sher Stark.");
  49.             System.out.println("Tutor: Mark Abernethy.");
  50.             System.out.println("Tutorial: Wednesday, 10:30 - 12:30.");        
  51.         }
  52.    
  53.         static void optionB(){
  54.        
  55.             Scanner input = new Scanner(System.in);
  56.        
  57.             float x;
  58.             float y;
  59.             float z;
  60.        
  61.             System.out.println("Enter the first number: ");
  62.             x = input.nextFloat();
  63.        
  64.             System.out.println("Enter the second number: ");
  65.             y = input.nextFloat();
  66.        
  67.             System.out.println("Enter the third number: ");
  68.             z = input.nextFloat();
  69.        
  70.             if((x > y)&&(x > z))
  71.             System.out.println("The first number is the largest. Its value is: " + x);
  72.             else
  73.             if((y > x)&&(y > z))
  74.                         System.out.println("The second number is the largest. Its value is: " + y);
  75.                     else
  76.                                 System.out.println("The third number is the largest. Its value is: " + z);
  77.        
  78.             float average = (x + y + z)/3;
  79.        
  80.             System.out.println("The average of the three numbers is: " + average);
  81.     }
  82.    
  83.     static void optionC(){
  84.        
  85.             Scanner input = new Scanner(System.in);
  86.        
  87.             int m;
  88.             int n;
  89.             int count = 0;
  90.             int sum = 0;
  91.        
  92.             System.out.println("Enter the first number: ");
  93.             m = input.nextInt();
  94.        
  95.             System.out.println("Enter the second number: ");
  96.             n = input.nextInt();
  97.        
  98.             if(m > n){
  99.            
  100.                 int temp = m;
  101.                 m = n;
  102.                 n = temp;
  103.            
  104.             }
  105.      
  106.             for(int i = m; i<= n; i++){
  107.                 System.out.print(i + " "); 
  108.            
  109.                 count++;
  110.                 if(count == 5){
  111.                     count = 0;
  112.                     System.out.println();
  113.                 }
  114.            
  115.                 if(i % 2 != 0)
  116.                     sum = sum + i;
  117.             }
  118.             if(count > 0)
  119.                 System.out.println();
  120.        
  121.             System.out.println("The sum of all the odd numbers is: " + sum);
  122.        
  123.     }
  124.    
  125.     static void optionD(){
  126.        
  127.             Scanner input = new Scanner(System.in);
  128.        
  129.             int s1;
  130.             int s2;
  131.             int s3;
  132.        
  133.             System.out.println("Enter the first number: ");
  134.             s1 = input.nextInt();
  135.        
  136.             System.out.println("Enter the second number: ");
  137.             s2 = input.nextInt();
  138.        
  139.             System.out.println("Enter the third number: ");
  140.             s3 = input.nextInt();
  141.        
  142.             System.out.println("The numbers you have entered are: " + s1 + " " + s2 + " " + s3);
  143.        
  144.             if((s1 + s2 > s3)&&(s1 + s3 > s2)&&(s2 + s3 > s1))
  145.                 System.out.println("This is a valid triangle.");
  146.             else
  147.                 System.out.println("This is not a valid triangle.");
  148.     }
  149.    
  150.     static void optionE(){
  151.        
  152.             Scanner input = new Scanner(System.in);
  153.        
  154.             int num;
  155.        
  156.             System.out.println("Please enter a positive integer: ");
  157.             num = input.nextInt();
  158.        
  159.             while(num > 0){
  160.            
  161.                 boolean prime = true;
  162.            
  163.             for(int i = 2; i <= num/2; i++){
  164.                
  165.                         if(num % i == 0){
  166.                         prime = false;
  167.                             break;
  168.                 }
  169.                     }
  170.                
  171.             if(prime == true){
  172.                         System.out.println("It is a prime number.");
  173.                         break;
  174.                     }
  175.                     else{
  176.                         System.out.println("It is not a prime number.");
  177.                         break;
  178.             }
  179.                 }
  180.     }  
  181.  
  182.     static void optionQ(){
  183.        
  184.             System.out.println("Quitting program. See you next time!");
  185.         }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment