gpt4 book ai didi

java - Java 中的菜单字符

转载 作者:行者123 更新时间:2023-12-01 04:35:50 24 4
gpt4 key购买 nike

我是 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;
}
}

最佳答案

strUserAnswer = input_stream.nextLine();
char c = strUserAnswer.charAt(0);

然后打开c

此外,在 while 循环内调用 input_stream.close(); 也会遇到问题。它必须位于循环的外部,以便您可以一次读取更多内容(您无法使用关闭的扫描仪读取任何内容)。

关于java - Java 中的菜单字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371054/

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