gpt4 book ai didi

java - 如何连续循环菜单直到选择退出选项,以及如果选择无效操作则重复菜单

转载 作者:行者123 更新时间:2023-12-01 17:00:32 25 4
gpt4 key购买 nike

抱歉,如果标题有点令人困惑。我一整天都在做这个工作,所以我的大脑有点烧焦了。我想做的是允许用户继续接收菜单选项,直到选择“退出”,但只输入一个字符串;如果做出了无效的菜单选择,则程序将向用户报告并再次提供菜单选项。我只是非常不知道该做什么,非常感谢任何帮助。

    public class McLemorePProg5 {

public static void main(String[] args) {

//Create scanner
java.util.Scanner mango = new java.util.Scanner(System.in);

//Print Statements

//User is requested to enter a string
System.out.printf("Please enter a string:\n");

//User enters a string
String s = mango.nextLine();

//Operation menu is displayed
System.out.println("(1) Report the number of vowels in the string (a, e, i, o, u, y)");

System.out.println("(2) Report the number of consonants in the string");

System.out.println("(3) Report the number of lower case letters in the string");

System.out.println("(4) Print out the characters of the strings that are in even positions in the string");

System.out.println("(5) Exit program ");

System.out.printf("\nPlease enter an operation number: (1), (2), (3), (4) or (5): \n");

//User enters an operation
int op = mango.nextInt();

switch (op) {

case 1:
System.out.println("The number of vowels in the string is " + countVowels(s));
break;
case 2:
System.out.println("The number of consonants is " + countConsonants(s));
break;
case 3:
System.out.println("The number of lowercase letters in the string is " + countLowercase(s));
break;
case 4:
System.out.println("the characters in even positions are:"); printEvenChars(s);
break;
case 5:
System.out.println("Program ended"); System.exit(1);
default:
System.out.println("Invalid operation choice. Please choose again");

}//end switch

//Close scanner
mango.close();

}//end main

//Methods

public static int countVowels(String s) { //Calculate the number of vowels
int vowelCount = 0;

for ( int i = 0; i < s.length(); i++) {

if (s.charAt(i) == 'A' || s.charAt(i) == 'a' || s.charAt(i) == 'E' || s.charAt(i) == 'e' ||
s.charAt(i) == 'I' || s.charAt(i) == 'i' || s.charAt(i) == 'O' || s.charAt(i) == 'o' ||
s.charAt(i) == 'U' || s.charAt(i) == 'u' || s.charAt(i) == 'Y' || s.charAt(i) == 'y') {
vowelCount++; } //end if
}//end for loop

return vowelCount;

}//end countVowels

public static int countConsonants(String s) { //Calculate the number of consonants
int consonantCount = 0;

for (int i = 0; i < s.length(); i++) {

if (s.charAt(i) == 'A' || s.charAt(i) == 'a' || s.charAt(i) == 'E' || s.charAt(i) == 'e' ||
s.charAt(i) == 'I' || s.charAt(i) == 'i' || s.charAt(i) == 'O' || s.charAt(i) == 'o' ||
s.charAt(i) == 'U' || s.charAt(i) == 'u' || s.charAt(i) == 'Y' || s.charAt(i) == 'y') {
} //end if

else {
consonantCount++;
}//end else

}//end for loop

return consonantCount;

}//end countConsonants

public static int countLowercase(String s) { //Calculate the number of lower case letters
int lowercaseCount = 0;

for (int i = 0; i < s.length(); i++) {

if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
lowercaseCount++; }//end if statement

}//end for loop

return lowercaseCount;

}//end countLowercase

public static void printEvenChars(String s) { //Prints the characters in even positions in the string
String evenChars = "";

for (int i = 0; i < s.length(); i++) {

if(i % 2 == 0) {
evenChars += s.charAt(i); }//end if statement

}//end for loop

System.out.println(evenChars);

}//end printEvenChars
}//end class

最佳答案

除非绝对必要,否则不要诉诸System.exit()。以下是您应该采取的行动的概述:

int op = mango.nextInt();

while (op != 5) {
switch(op) {
// your switch cases
case 5:
// just do nothing
break;
}

// once you've processed that command, read in the next one.
// If it's 5, the loop will exit
// you may want to also print the menu here, so it appears after each command
op = mango.nextInt();
}

mango.close();

关于java - 如何连续循环菜单直到选择退出选项,以及如果选择无效操作则重复菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61511937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com