1

私は 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("\n\nClosing 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;
   }
}
4

3 に答える 3

0

これは良いスタートのように見えますが、改善すべき点がいくつかあります。次のようなことを行うことで、コードの長さを短縮し、可読性を大幅に向上させることができます。

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

になる

String choiceVerifyc = "You chose choice: ";

この:

String strUserAnswer;
strUserAnswer = input_stream.nextLine();

になります:

String strUserAnswer = input_stream.nextLine();

コードの不要な部分を短くして削除することで、ここ StackOverflow であなたを助けようとしている人々がはるかに簡単になります。

コーディング頑張ってください!

于 2013-06-28T18:53:56.633 に答える
0

この行を変更する必要があります

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
}

したがって、スイッチは、以前に使用した入力を保存した変数を使用します。

于 2013-06-28T18:38:04.657 に答える
0
strUserAnswer = input_stream.nextLine();
char c = strUserAnswer.charAt(0);

次にスイッチをオンにします c

input_stream.close();また、 while ループ内で呼び出しを行うと、問題が発生します。一度に複数を読み取るには、ループの外側にある必要があります (閉じたスキャナーでは何も読み取ることができません)。

于 2013-06-28T18:16:48.780 に答える