我们需要创建一个可以使用Characters选择的菜单 如A. B. C.也a。湾C。并使用Q.退出。
package mainmenu;
import java.util.Scanner;
public class MainMenu{
public static void main(String[] args){
System.out.println(" Welcome to our project");
System.out.println("Please Choose an option from the menu");
System.out.println("Type the letter to select the option");
System.out.println();
// We need to create a menu that can be selected using Characters
//such as A. B. C. also a. b. c. and use Q. to quit.
这里我们将设置一个包含7个选项的菜单:
正在寻找一种使用字母创建菜单的方法以及将它们转换为大写字母的方法。
/* A. The Radius of a circle.
B. The Cost of gym membership over a year.
C. Compound Interest of CD over 6 years.
D. The Square Root of a 5 digit number.
E. Guess the number Game.
F. Day of your birth day.
G. Little Shop of Horrors.
Q. Quit / Exit.
*/
// A. Radius of a Circle
// Scan for the Diameter then display the Results
// B. Cost of a Gym Membership
// Scan for the Gym info, Name, joining fee, monthly cost, yearly dues, cancellation fee.
// double joinFee, monthCost, yearDues, cancelFee.
// double initialCost = joinFee + monthCost;
// double yearlyCost = initialCost + (monthCost * 11) + yearDues;
// double earlycancel = (monthCost * 2 ) + cancelFee;
// Display the options
// C. Compound Interest of CD over 6 year period
/*
A=P(1+R)^N
double amount;
double principal = 10000 ;
double rate = .01;
for(double months=1; months<=60; months++){
amount=principal * Math.pow(1 + rate, months);
System.out.println (months + " " + amount); }
*/
// D. Calculate the Square Root of a 5 digit number
// E. Guess the Number Game
// Too low
// Too high
// F. Day of the week of your Birthday.
// Calculate the Day and display
// G. When should Seymour feed the plant
// Feed me Seymour!!
// Feed him in the morning, in the afternoon, in the evening and at midnight.
// Q. Quit / Exit
答案 0 :(得分:-1)
此代码应该有所帮助:
Scanner sc = new Scanner(System.in);
String options = "A. The Radius of a circle.\n" +
"B. The Cost of gym membership over a year.\n"
+"C. Compound Interest of CD over 6 years.\n"
+"D. The Square Root of a 5 digit number.\n"
+"E. Guess the number Game.\n"
+"F. Day of your birth day.\n"
+"G. Little Shop of Horrors.\n"
+"Q. Quit / Exit.\n";
String input = null;
System.out.println("Enter the option you would like to select. Your choices are: ");
System.out.println(options);
boolean validInput = false;
while (!validInput) {
input = sc.next(); //asks for the user input
input = input.toUpperCase(); // makes the entire String uppercase
if (!input.equals("A") || !input.equals("B") || !input.equals("C") || !input.equals("D") || !input.equals("E") || !input.equals("F") || !input.equals("G") || !input.equals("Q")) {
System.out.println(input+" is not an option");
System.out.println("Your choices are: ");
System.out.println(options);
} else
validInput = true; //exits the loop when they give something useful.
}
if (input.equals("A")) {
//do A stuff
} else if (input.equals("B")) {
//do B stuff
} else if (input.equals("C")) {
//do C stuff
} else if (input.equals("D")) {
//do D stuff
} else if (input.equals("E")) {
//do E stuff
} else if (input.equals("F")) {
//do F stuff
} else if (input.equals("G")) {
//do G stuff
} else if (input.equals("Q")) {
//do Q stuff
}
sc.close(); //do this when you are done with the scanner
它的作用是询问用户的输入,然后检查它是否等于其中一个选项。我用
input = input.toUpperCase();
使输入大写,以便“a”和“A”被识别为相同的东西。确保你不要尝试
input.toUpperCase();
因为那不会做任何事情。字符串是不可变的。以下是有关该问题的更多信息:String is immutable. What exactly is the meaning?
另外,请确保使用
input.equals("whatever")
而不是
input == "whatever"
因为第二个例子不可靠。有关此内容的更多信息,请访问:How do I compare strings in Java?