1

// 我一直试图让我的程序只接受来自扫描仪的单个字符。现在该程序将接受任意数量的字符,只要第一个字母是列出的字母之一。如果可能的话,我想用 charAt(0) 重写这段代码,或者添加一个 if 语句。if(sea.length > 1){} 类似的东西。我希望我能很好地解释这个问题以便理解。任何帮助表示赞赏,并感谢您的时间。

    public static char promptForChoice(Scanner in) {

    System.out.println("High, Low or Seven(H/L/S");
    char sel = in.next().charAt(0);
    sel = Character.toUpperCase(sel);
    int i = 1;
    while (i != 0) {
        System.out.println("High, Low or Seven(H/L/S");
        if (sel == 'H' || sel == 'L' || sel == 'S') {
            i = 0;
        } else {
            System.out.println("You must enter only H, L or S.");
            i = 1;
        }
    }
    return sel;


} 
4

3 回答 3

2

有 char.length 命令吗?

不,没有这样的命令。您需要使用获取输入Scanner::nextLine()并检查输入的长度String

执行以下操作:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Test
        Scanner in = new Scanner(System.in);
        System.out.println(promptForChoice(in));
    }

    public static char promptForChoice(Scanner in) {
        char sel = 0;
        String input;
        boolean valid;
        do {
            valid = true;
            System.out.print("Enter H/L/S [High/Low/Seven]: ");
            try {
                input = in.nextLine();
                sel = input.toUpperCase().charAt(0);
                if (input.length() == 1 && (sel == 'H' || sel == 'L' || sel == 'S')) {
                    return sel;
                } else {
                    throw new IllegalArgumentException("You must enter only H, L or S.");
                }
            } catch (IllegalArgumentException e) {
                valid = false;
                System.out.println(e.getMessage());
            }
        } while (!valid);
        return sel;
    }
}

示例运行:

Enter H/L/S [High/Low/Seven]: hello
You must enter only H, L or S.
Enter H/L/S [High/Low/Seven]: High
You must enter only H, L or S.
Enter H/L/S [High/Low/Seven]: M
You must enter only H, L or S.
Enter H/L/S [High/Low/Seven]: H
H

如有任何疑问/问题,请随时发表评论。

于 2020-03-31T02:32:23.107 回答
0

您可以从扫描仪获取输入并将其放入字符串中,然后您可以执行 string.length 并检查它!

于 2020-03-31T02:32:21.490 回答
-1

字符长度始终为 1,我想你可以实现你所需要的

public static char promptForChoice(Scanner in) {

if(in.next() == "H" || in.next() == "L" || in.next() == "S") {
 System.out.println("High, Low or Seven(H/L/S");
 return (char) in.next();
}
else {
 System.out.println("You must enter only H, L or S.");
 // you must always have a return value, in this case 
 //e.g. the automatically initialized char value
 return '\0';
}
}

使用您的代码,您可以执行类似的操作

public static char promptForChoice(Scanner in) {

System.out.println("High, Low or Seven(H/L/S");
char sel = in.next().charAt(0);
if(in.next().length == 1) {
  sel = Character.toUpperCase(sel);
  int i = 1;
 while (i != 0) {
    System.out.println("High, Low or Seven(H/L/S");
       if (sel == 'H' || sel == 'L' || sel == 'S') {
        i = 0;
       } else {
         System.out.println("You must enter only H, L or S.");
         i = 1;
       }
    }
   }
   else System.out.println("You must enter only one single character");
    return sel;
    } 
于 2020-03-31T02:04:07.610 回答