【问题标题】:for loops with nested if loop in javajava中带有嵌套if循环的for循环
【发布时间】:2013-11-30 19:16:23
【问题描述】:

对 Java 非常陌生,我在大学的入门课上做一个项目。我正在尝试创建一种方法来搜索字符串数组以查找输入状态并返回索引。如果用户输入了一个不在数组中的查询,我希望它请求一个新的状态来搜索。 我的例外是“变量 statePosition 可能尚未初始化”。 下面是代码。

提前谢谢你!

static final int NUM_STATES = 50;

public static int askState(String[] stateNames) {
    Scanner keyboard = new Scanner(System.in);
    String state;
    int statePosition;
    System.out.println("Please enter a state that you would like to search:");
    state = keyboard.next();
    {
        for (int i = 0; i < NUM_STATES; i++) {
            if (state.equals(stateNames[i])) {
                statePosition = i;
            } else {
                System.out.println("Please enter a valid state:");
            }
            state = keyboard.next();
        }
        return statePosition;
    }

【问题讨论】:

  • 初始化变量 statePosition。 0 就可以了。
  • 字符串状态=空; int statePosition=-1;
  • 也许是时候接受答案了……

标签: java if-statement for-loop nested


【解决方案1】:

你会用吗

 int statePosition = -1;

如果没有找到它会返回 -1。该错误表示您没有为 statePosition 赋值。

【讨论】:

    【解决方案2】:

    我认为您需要像这样初始化变量int statePosition;

    int statePosition =-1;
    

    还像这样初始化你的String state;

    String state = null;
    

    【讨论】:

    • @user3052882:- 不客气!如果对您有帮助,请接受此答案!
    【解决方案3】:

    像异常状态一样,你必须初始化 int statePosition:

    int statePosition = null;
    

    int statePosition = 0;
    

    ...你知道我的意思吗?

    【讨论】:

      【解决方案4】:

      你需要初始化两个变量

      String state= null;
      int statePosition=-1;
      

      【讨论】:

        【解决方案5】:

        这里的问题是你从不实例化 statePosition 变量,但你总是返回它。

        尝试给 statePosition 一个值(就像其他人所说的 -1)。

        如果你的 statePosition 被发现,也可以尝试使用 while(而不是 for 语句)来执行此操作。

        【讨论】:

          【解决方案6】:

          在代码中进一步使用之前,您的程序很容易在其中一个变量中遇到错误。 我建议每次编写代码时,都应该将变量置于初始状态,而不是将它们保留为空值。

          String state= null;
          int statePosition=-1;
          

          将其放入代码中以解决您的问题。

          【讨论】:

            【解决方案7】:

            您可能希望从使用 int 更改为 Integer。整数是一个对象,所以它可以为空。这样您就可以在执行任何进一步操作之前检查它是否已设置。

            static final int NUM_STATES = 50;
            
            public static Integer askState(String[] stateNames) {
                Scanner keyboard = new Scanner(System.in);
                String state;
                Integer statePosition;
                System.out.println("Please enter a state that you would like to search:");
                state = keyboard.next();
            
                for (int i = 0; i < NUM_STATES; i++) {
                    if (state.equals(stateNames[i])) {
                        statePosition = i;
                    } else {
                        System.out.println("Please enter a valid state:");
                    }
                    state = keyboard.next();
                }
                if (statePosition == null) {
                    throw new Exception("State Position Not Set :(");
                }
                return statePosition;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-07-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-08-21
              • 1970-01-01
              • 1970-01-01
              • 2019-06-27
              相关资源
              最近更新 更多