Java中的菜单字符



我是Java编程的新手,在理解如何接受用户输入并将其转换为程序能够理解的内容方面一直存在问题。我试图在这个程序中输入用户的字符,并让程序接受它并在switch语句中使用它。请有人告诉我,我会用什么关键词或技术来输入一个字符,或者把一个字符串翻译成我的程序中的字符,而不会让它变得非常复杂?我感谢你的帮助,并提前感谢你!

import java.util.Scanner;
//import java.util.Scanner;
public class VendingMachine;
{
   public static void main(String[] args)
   {
      Scanner scanner = new Scanner (System.in);
      int selection=0;
      int x=0;
      float origItemNum = 15;
      float origDebitBal = 15;
      double balanceDebit;
      double balanceItemNum;
      double items = 0;
      char choice = 0;
      //initialize variables

      boolean validSelection;
      System.out.print("nInitial Account Settings:");
      System.out.print("nUnused Item Capacity: 15");
      System.out.print("nCost (so far) this month: $15");
      balanceDebit=origDebitBal;
      balanceItemNum = origItemNum;
      while(x==0)
      {
         System.out.print("nMenu:"); 
         System.out.print("nB (show Bill and starts new month)");  
         System.out.print("nU (show Unused capacity for the current month)"); 
         System.out.print("nC (Consume vending items now -- " +
               "purchase candy bar, bag of chips, etc.)"); 
         System.out.print("nA (buy Additional items for current month)"); 
         System.out.print("nQ (show bill and Quit)"); 
         String strUserAnswer;
         String strQuestion;
         String choiceVerify;
         Scanner input_stream = new Scanner(System.in);
         strQuestion = new String("What choice would you like? Please" +
               "enter in either option A, B, U, C- or enter E to quit. ");
         strUserAnswer = input_stream.nextLine();
         choice = (char) Integer.parseInt(strUserAnswer);
         choiceVerify = ("You chose choice: ");
         System.out.print(strUserAnswer);
         input_stream.close();
         switch (selection)
         {
         case 'b':
         case 'B': 
            System.out.print("nnClosing bill for month:");
         //   System.out.print("nUnused items (lost):" );
         //   System.out.print(balanceItemNum);
          //  System.out.print("nFinal amount due immediately: $" );
          //  System.out.print(balanceDebit);
           // System.out.print("nStarting new month ...Available items: 15");
           // balanceItemNum = 15;
           // balanceDebit = 15;
            break;
         case 'u':
         case 'U':
            System.out.print("nUnused capacity of items you can use: " );
           // System.out.print(balanceItemNum);
           // System.out.print("nYour debit balance: $" );
           // System.out.print(balanceDebit);
            break;
         case 'c':
         case 'C':
            System.out.print("nNumber of items you want to purchase:");
          //  Scanner input = new Scanner(System.in);
            //items = input.nextDouble();
            //balanceItemNum = balanceItemNum-items;
           // balanceDebit = balanceDebit - items;
           // System.out.print("nAvailable Items: " );
           // System.out.print(balanceItemNum);
            break;
         case 'a':
         case 'A': 
            String numberString = JOptionPane.showInputDialog("nAdditional " +
                  "items purchase in sets of 10 (1-3):");
           // double number = Double.parseDouble(numberString);
            //      while (number == 1 || number == 2 || number == 3)
           // number = number * 11;
           // balanceItemNum = balanceItemNum + number;
           // balanceDebit = balanceDebit + number;
            break;
         case 'e':
         case 'E':
            System.out.print("nYour debit balance: $" );
           // System.out.print(balanceDebit);
            break;
         }
      }
      while (choice != 'e' && choice != 'E');
      System.out.print("nError: Please enter in either B, U, C, A or Q.");
      return;
   }
}
strUserAnswer = input_stream.nextLine();
char c = strUserAnswer.charAt(0);

然后打开c

此外,在while循环中调用input_stream.close();也会遇到问题。它必须在循环的外部,才能让你一次读更多(用关闭的扫描仪无法读取任何内容)。

您应该更改这一行

choice = (char) Integer.parseInt(strUserAnswer);

所以它看起来像这个

choice = strUserAnswer.charAt(0);

通过此更改,使用charAt(0)可以获得用户输入的字符的位置0(第一个),然后将其保存在名为choice的变量中。

然后将此部分中的变量选择更改为选项

switch(selection){
    //all of your code
} 

所以它看起来像这个

switch(choice){ 
    //all of your code
}

因此,开关将使用之前存储使用过的输入的变量。

这看起来是一个良好的开端,但也有一些地方需要尝试和改进。您可以通过以下操作来减少代码的长度并显著提高代码的可读性:

String choiceVerify;
choiceVerify = ("You chose choice: ");

成为

String choiceVerifyc = "You chose choice: ";

这个:

String strUserAnswer;
strUserAnswer = input_stream.nextLine();

变为:

String strUserAnswer = input_stream.nextLine();

通过缩短和删除代码中不必要的部分,您可以让人们更容易地在StackOverflow上帮助您。

祝你的编码好运!

相关内容

  • 没有找到相关文章

最新更新