扫描程序的Java问题

时间:2012-10-15 06:57:04

标签: java java.util.scanner

我是java的新手,所以请光临我。我有一个方法,应扫描大写或小写的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;
}

5 个答案:

答案 0 :(得分:2)

您只需使用equalsIgnoreCase并修剪输入的字符串即可。并添加一个while循环,满足您的条件。

    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);

答案 1 :(得分:2)

不使用while(),您可以像这样使用'else' -

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);
    }   
}

答案 2 :(得分: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!");
    }
}

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这就是你想要的。这是迭代String中字符的程序。如果它们是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;
    }
}