2015-02-23 55 views
0

我們需要創建一個菜單,可以使用字符 (如A.B.C也可以選擇)來選擇。灣C。並使用Q.退出。如何創建菜單以在Java中使用字符A B C或b c進行選擇

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

這聽起來像一個家庭作業問題,我看不出你有任何嘗試自己解決它。 – DavidS 2015-02-23 03:33:27

回答

-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?

+0

謝謝,我會開始使用這個,感謝您的快速回答。 – 2015-02-23 03:21:01

+0

隨時。如果它真的有效,請不要忘記提高我的答案。如果沒有,告訴我,我會嘗試修復它。 – 2015-02-23 03:22:57

+0

我是否需要包含任何新庫以使其正常工作 – 2015-02-23 04:04:22

相關問題