簡體   English   中英

如何從int []數組返回正確的值?

[英]How to return the correct value from an int[] array?

我是Java(和一般編程)的新手,如果其中的某些道理不合理和/或代碼真的很糟糕,則深表歉意:

我正在嘗試從用戶輸入中獲取唯一的四位數代碼(這是Keyboard.readInput行所做的事情),並且我輸入了一些條件語句來確保輸入的代碼只能為四位數,並且每個數字必須與其他三個數字不同。

if語句按預期的方式工作,因為它們會輸出錯誤消息並提示它們重新輸入(即,調用getPlayerGuess()方法)。 例如,如果用戶輸入123(即userGuess.length() != 4 ),它將提示他們再次輸入。 如果他們隨后輸入1234,則該方法將完成。

但是,我的問題是,當我在另一個類中調用此方法時,要拉入的代碼是輸入的第一個代碼(即123),而不是我想要的四位數(即1234)-導致一個數組IndexOutOfBoundsException

關於如何確保所返回的代碼是通過條件的四位數字而不是輸入的第一個數字的任何想法?

public int[] getPlayerGuess() {

    System.out.print("Guess a four digit code: ");
    String userGuess = Keyboard.readInput();

    if (userGuess.length() != 4) {
        System.out.print("Your code must be 4 digits - ");
        getPlayerGuess();
    }

    int[] userCode = createArrayFromGuess(userGuess);

    for (int i = 0; i < userCode.length-1; i++){
        for (int j = i+1; j < userCode.length; j++){
            if (userCode[i] == userCode[j]) {
                System.out.print("Code must have four unique digits - ");
                getPlayerGuess();
            }
        }
    }
    return userCode;
}

您的問題出在這里:

if (userGuess.length() != 4) {
      System.out.print("Your code must be 4 digits - ");
      getPlayerGuess();
    }

是的,您第二次調用該方法,但是您完全忽略了該方法,並且對結果不做任何事情。

更改為此:

if (userGuess.length() != 4) {
      System.out.print("Your code must be 4 digits - ");
      return getPlayerGuess();
    }

因此,您將返回新調用的結果,而不是在if塊之后完成代碼。

編輯:更好的方法是:

System.out.print("Guess a four digit code: ");
String userGuess = Keyboard.readInput();

  while(userGuess.length() != 4) {
      System.out.print("Your code must be 4 digits - ");
      System.out.print("Guess a four digit code: ");
      userGuess = Keyboard.readInput();
    }

調用getPlayerGuess()會返回一個int[]但您不會收集也不分配返回值。 您也不會退還它...因此,以下內容可能對您有用:

public int[] getPlayerGuess() {

  System.out.print("Guess a four digit code: ");
  String userGuess = Keyboard.readInput();

  if (userGuess.length() != 4) {
    System.out.print("Your code must be 4 digits - ");
    return getPlayerGuess(); // either assign or return the value that is actually calculated within that call...
  }

  int[] userCode = createArrayFromGuess(userGuess);

  for (int i = 0; i < userCode.length-1; i++){
      for (int j = i+1; j < userCode.length; j++){
          if (userCode[i] == userCode[j]) {
              System.out.print("Code must have four unique digits - ");
              return getPlayerGuess(); // either assign or return the value that is actually calculated within that call...
          }
      }
  }
  return userCode;
}

稍微簡化代碼(不要太多;-)):

public int[] getPlayerGuess(String msg) {
    System.out.print(msg);
    String userGuess = Keyboard.readLine();
    if (userGuess.length() != 4) {
        return getPlayerGuess("\nYour code must be 4 digits - ");
    }
    if (userGuess.chars().distinct().count() != 4) {
        return getPlayerGuess("\nCode must have four unique digits - ");
    }
    return createArrayFromGuess(userGuess);
}

初始呼叫為:

getPlayerGuess("Guess a four digit code: ");

請注意,您也可以使用迭代方法來編寫此代碼。 但我會留給你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM