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

问题描述 投票:0回答:1
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时,我的程序打印选择您的...零件,但它不等待用户输入,并完成运行。

java intellij-idea switch-statement
1个回答
0
投票

您正在寻找的是Scanner#next(),而不是System.in#read()。您需要创建一个Scanner对象InputStream,在本例中为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;

}

© www.soinside.com 2019 - 2024. All rights reserved.