如何接受整数或字符串JAVA

时间:2016-02-20 21:42:10

标签: java

   private void CreateAccount() {
        // TODO Auto-generated method stub
        String firstName, accountType="";
        double initialDesposit = 0;                      
        boolean valid = false;
        int Current = 1;
        int Saving = 2;
        while (!valid){                    //Only can select current or saving account
            System.out.println("Enter account Type");
            System.out.println("1:Current");          
            System.out.println("2:Saving");
             accountType = keyboard.nextLine(); //User Input of the Account Type 


            if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")|| accountType.equals(1) || accountType.equals(2) ){
                valid = true;  //If selection is true 
            }else{
                System.out.println("Invalid");
            }
        }

我试图说明当用户显示选项时,他们可以选择数字或字母选项,而不是输入"保存"用户只需按下" 2"它被记录为Saving。

5 个答案:

答案 0 :(得分:2)

我不知道,你的问题是什么,因为你的帖子不包含一个,但我认为你的if子句中至少有一个错误:

if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")|| accountType.equals(1) || accountType.equals(2) )

由于accountType是一个字符串,因此将其与整数进行比较将始终失败。您可以像下面一样编写它以将其与字符串进行比较:

if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")|| accountType.equals("1") || accountType.equals("2") )

这应该有效,因为你得到字符串作为输入。

答案 1 :(得分:0)

您可以使用Scanner类:

Scanner sc = new Scanner(System.in);
String userInput = "";

if (sc.hasNext()) {
    userInput = sc.next();
}

if (userInput.equals("Current") || userInput.equals("1")) {
    // option 1
} else if (userInput.equals("Saving") || userInput.equals("2")) {
    // option 2
} else {
    // bad or no input
}

答案 2 :(得分:0)

   private void CreateAccount() {
        // TODO Auto-generated method stub
        String firstName, accountType="";
        double initialDesposit = 0;                      
        boolean valid = false;
        int Current = 1;
        int Saving = 2;
        while (!valid){                    //Only can select current or saving account
            System.out.println("Enter account Type");
            System.out.println("1:Current");          
            System.out.println("2:Saving");
             accountType = keyboard.nextLine(); //User Input of the Account Type 


            if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")|| accountType.equals("1") || accountType.equals("2") ){
                valid = true;  //If selection is true 
            }else{
                System.out.println("Invalid");
            }
        }

答案 3 :(得分:0)

我认为这就是你要找的东西:

private void CreateAccount() {
    // TODO Auto-generated method stub
    String firstName, accountType = "";
    double initialDesposit = 0;
    boolean valid = false;
    int Current = 1;
    int Saving = 2;
    Scanner sc = new Scanner(System.in);
    boolean isInt = false;
    while (!valid) {                    //Only can select current or saving account
        System.out.println("Enter account Type");
        System.out.println("1:Current");
        System.out.println("2:Saving");
        accountType = sc.nextLine(); //User Input of the Account Type 

        isInt = isInteger(accountType);

        if (isInt) {
            if (Integer.parseInt(accountType) == 1 || Integer.parseInt(accountType) == 2) {
                valid = true;  //If selection is true 
            } else {
                System.out.println("Invalid");
            }
        } else {
            if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")) {
                valid = true;  //If selection is true 
            } else {
                System.out.println("Invalid");
            }
        }

    }
}

public boolean isInteger(String check) {
    for (int i = 0; i < check.length(); i++) {
        if (!Character.isDigit(check.charAt(i))) {
            return false;
        }
    }
    return true;
}

答案 4 :(得分:-1)

$(document).ready(function() {
    $("#contactForm").validator().on("submit", function(event) {
        if (event.isDefaultPrevented()) {
            console.log("rrrr");
        } else {
            event.preventDefault();
            submitForm();
        }
    });

    $("#msgSubmit").removeClass("hidden");
});

function submitForm() {
    $.ajax({
        type: "POST",
        url: "process.php",
        data: $("#contactForm").serialize(),
        success: function(text) {
            console.log(text); // should be "invalid" or "success"
        },
        error : function() {
            console.log('epic fail');
        }
    });
}