⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 user_menu_choice.txt

📁 Program Description: The program asks the user to choice from the menu an option
💻 TXT
字号:
/
 * Program Description: The program asks the user to choice from the menu an option
 *                      A. Check to see if a number is prime. 
 *                      B. Count the number of vowels in a line. 
 *                      X. Exit the program.
 */

import java.io.*;
import java.util.*;
import java.lang.String;

public class Assignment_1
{
    public static void main(String[] args) 
    throws IOException 
    {
        Scanner selection = new Scanner(System.in);
        BufferedReader stdin = new BufferedReader
        (new InputStreamReader (System.in));

        String menuSel, text;
                
        for( ;; ){


        // The menu section for the user to an option.
        
        System.out.println("\n *******************Assignment 1 Menu*******************");
        System.out.println("\t A. Check to see if a number is Prime");
        System.out.println("\t B. Count the number of Vowels in a line.");
        System.out.println("\t X. Exit the program.");
        System.out.println("********************************************************\n");
       
        System.out.print("Please select one of the above options (A, B or X):  ");
        menuSel = selection.next();

        // calculates if the number you entered is a prime number
         if (menuSel.equalsIgnoreCase("A"))
             {
                 System.out.println("\n Please enter a Integar:   ");

                 int num = Integer.parseInt(stdin.readLine());
                 
                 if (num > 0 && (num % 2) != 0)
                     {
                         System.out.println("\n The number " +num+ " is prime.");
                         
                     }
                 
                     else if (num == 0)
                         {
                             System.out.println("\n Please enter a number larger than 0.");
                             
                         }
                         
                         else
                             System.out.println("\n The number " +num+ " is not prime");                         
                            
              }


             // The section is to calculate the numbers of vowels in an inputed text.
             else if (menuSel.equalsIgnoreCase("B"))
                 {

                     System.out.println("\n Please enter a line of text: ");
                     BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
                     text = bf.readLine();

                     int vowelCount = 0;

                     for (int i = 0; i < text.length(); i++)
                         {
                             char currentChar = text.charAt(i);

                             if (currentChar == 'A' || currentChar == 'a'
                             || currentChar == 'E' || currentChar == 'e'
                             || currentChar == 'I' || currentChar == 'i'
                             || currentChar == 'O' || currentChar == 'o'
                             || currentChar == 'U' || currentChar == 'u')
                             vowelCount++;
                         }

                     System.out.println("");
                     System.out.println("Number of vowels found in the line is " + vowelCount);
                 }  


             // The section terminates the program.
             else if (menuSel.equalsIgnoreCase("X"))
                 {
                      System.out.println("Existing program goodbye...");
                      System.exit(0);
                 }    

             else if (menuSel != ("X"))
                 {
                      System.out.println("Error! invalid Selection");
                      
                 }
        } // End of the for loop
    }        
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -