1

我有一个方法应该扫描 3 个大写或小写字母中的一个并返回该字母的小写版本。如果输入了不正确的字母,则警告用户并重新要求输入字母。我有两个问题,1:只要运行该方法,我就会得到带有错误消息的输出行,该错误消息告诉用户输入无效,而无需等待输入!(所以我High, low or sevens (H/L/S):Invalid entry. Please try again using H/L/S!在输入任何内容之前看到第二次运行该方法,然后再次调用该方法,并且除了我的下一个问题外,所有工作正常) 2:从扫描仪获取的条目永远不会通过我的任何 if 语句,即使它应该。

我的代码:

private static char getHighLow(Scanner inScanner) {
    System.out.print("High, low or sevens (H/L/S):");
    String entered = inScanner.nextLine();
    System.out.print(entered);
    if(entered.equals("H") || entered.equals("h")){
        return 'h';
    }
    else if (entered.equals("L") || entered.equals("l")){
        return 'l';
    }
    else if(entered.equals("S") || entered.equals("s")){
        return 's';
    }

        char result = 0;
        while(result != 'l' || result != 'h' || result != 's'){
        System.out.println("Invalid entry. Please try again using H/L/S!");
        result=getHighLow(inScanner);
    }
        return result;
}
4

5 回答 5

2

您可以简单地使用equalsIgnoreCase和修剪输入的字符串。并添加一个while循环 util 满足您的条件。

    Scanner scanner = new Scanner(System.in);
    boolean loop = true;
    String choice = null;
    while (loop) {
        System.out.print("High, low or sevens (H/L/S):");
        choice = scanner.nextLine();
        if ("H".equalsIgnoreCase(choice.trim())
                || "L".equalsIgnoreCase(choice.trim())
                || "S".equalsIgnoreCase(choice.trim())) {
            System.out.println("Correct Choice");
            loop = false;
        }
        else
        {
            System.out.println("Wrong Choice");
        }
    }
    System.out.print(choice);
于 2012-10-15T06:59:17.283 回答
2

您可以像这样使用“else”,而不是使用 while() -

private static char getHighLow(Scanner inScanner) {
    System.out.print("High, low or sevens (H/L/S):");
    String entered = inScanner.nextLine();
    System.out.print(entered);
    if(entered.equals("H") || entered.equals("h")){
        return 'h';
    }
    else if (entered.equals("L") || entered.equals("l")){
        return 'l';
    }
    else if(entered.equals("S") || entered.equals("s")){
        return 's';
    }
    else {
        System.out.println("Invalid entry. Please try again using H/L/S!");
        return getHighLow(inScanner);
    }   
}
于 2012-10-15T07:15:05.800 回答
1
char result; 
while(true){
    System.out.print("High, low or sevens (H/L/S):");
    String entered = inScanner.nextLine();
    System.out.print(entered);
    if(entered.equals("H") || entered.equals("h")){
        result = 'h';break;
    }
    else if (entered.equals("L") || entered.equals("l")){
        result = 'l';break;
    }
    else if(entered.equals("S") || entered.equals("s")){
        result = 's';break;
    }else{
        System.out.println("Invalid entry. Please try again using H/L/S!");
    }
}
于 2012-10-15T07:04:50.840 回答
0

嘿,你根本没有跳出 while 循环。你看到了吗 ?

于 2012-10-15T07:11:58.950 回答
0

这就是你想要的。这是迭代字符串中字符的程序。如果它们是 H、L 或 S,则将它们转换为小写字母。

package testproj;

import java.util.Scanner;

public class TestProj {

    public static void main(String[] args) {
        Scanner scanner = new Scanner("HLs");
        String result = getHighLow(scanner);
        System.out.println("Result :"+result);
    }

    private static String getHighLow(Scanner inScanner) {
        System.out.println("High, low or sevens (H/L/S):");

        String entered;
        String result = "";
        boolean isCharFound = false;
        String temp = "";
        while (inScanner.hasNext()) {
            temp = inScanner.next();

            System.out.println(temp);

            for (int index = 0; index < temp.length(); index++) {
                entered =new Character(temp.charAt(index)).toString() ;


                if (entered.equals("H") || entered.equals("h")) {
                    result = result + 'h';
                    isCharFound = true;
                } else if (entered.equals("L") || entered.equals("l")) {
                    result = result + 'l';
                    isCharFound = true;
                } else if (entered.equals("S") || entered.equals("s")) {
                    result = result + 's';
                    isCharFound = true;
                }

                if (!isCharFound) {
                    System.out.println("Invalid entry. Please try again using H/L/S!");
                }

                isCharFound = false;
            }


        }

        return result;
    }
}
于 2012-10-15T07:38:27.523 回答