gpt4 book ai didi

java - 内部开关无法接受用户输入

转载 作者:行者123 更新时间:2023-12-01 19:28:32 25 4
gpt4 key购买 nike

System.out.print("Please Select an Option...");
selected = (char) System.in.read();
}
while (selected < '1' || selected > '5'); // || => OR
//System.out.println("\n");

switch (selected)
{
case '1':
System.out.println("$: 6.08₺");
System.out.println("€: 6.88₺");
System.out.println("Gram Gold: 327₺");
System.out.println("Bitcoin/TL: 57.462₺");
System.out.println("Ripple/TL: 1.42₺");
break;
case '2':
System.out.print("Select Your Bank Account...");
**selectBankAccNum = (char) System.in.read();**
System.out.println(selectBankAccNum + " sd");

当外部开关盒为2时,我的程序打印Select your...part,但它不等待用户输入,并完成运行。

最佳答案

您正在寻找的是 Scanner#next(),而不是 System.in#read()。您需要使用InputStream 创建Scanner 对象,在本例中为System.in,因为它是实现InputStream 的实现PrintStream。

Scanner scanner = new Scanner(System.in);

System.out.print("Please Select an Option...");

int selected = scanner.nextInt();

switch(selected) {
case 1:
//your code
break;

case 2:
System.out.print("Select Your Bank Account...");

int account = scanner.nextInt();
break;

}

关于java - 内部开关无法接受用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60589090/

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