简体   繁体   English

for循环与嵌套if循环在Java中

[英]for loops with nested if loop in java

Very new to Java, I am in an intro class in college and doing a project. 我对Java非常陌生,我正在上大学的入门班,正在做一个项目。 I am trying to make a method that searches a String array for an inputted state and returns the index. 我正在尝试一种方法,该方法在String数组中搜索输入的状态并返回索引。 If the user enters a query that is not in the array, I would like it to ask for a new state to search. 如果用户输入不在数组中的查询,我希望它请求搜索新状态。 My exception is saying "variable statePosition may not have been initialized." 我的例外是说“变量statePosition可能尚未初始化”。 Below is the code. 下面是代码。

Thank you in advance! 先感谢您!

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

Can you use 你能用吗

 int statePosition = -1;

That would return -1 if it's not found. 如果找不到,它将返回-1。 The error means you didn't assign a value to statePosition. 该错误表示您未为statePosition分配值。

I think you need to initialize your variable int statePosition; 我认为您需要初始化int statePosition;变量int statePosition; like this: 像这样:

int statePosition =-1;

Also initialize your String state; 还初始化您的String state; like this: 像这样:

String state = null;

Like the exception states, you have to initialize the int statePosition: 像异常状态一样,您必须初始化int statePosition:

int statePosition = null;

or 要么

int statePosition = 0;

... you know what I mean? ... 你知道我的意思?

you need to initialize both variables 您需要初始化两个变量

String state= null;
int statePosition=-1;

The problem here is that you never instantiate the statePosition variable but you are always returning it. 这里的问题是,您永远不会实例化statePosition变量,但总是返回它。

Try give statePosition a value (like others said -1). 尝试给statePosition一个值(就像其他人所说的-1)。

Also try to do this with a while (instead of a for statement) which breaks if your statePosition was found. 还尝试用一段时间(而不是for语句)来执行此操作,这会在找到statePosition时中断。

Your program is prone to encounter an error in one of it variables prior to their use further in the code. 在代码中进一步使用它们之前,您的程序很容易在其中一个变量中遇到错误。 I would suggest that every time you code, you should put the variables to initial state rather than leaving them with a null value. 我建议每次编写代码时,都应将变量置于初始状态,而不是将它们保留为空值。

String state= null;
int statePosition=-1;

Put this in the code to have your problems solved. 将其放在代码中可以解决您的问题。

You might want to change from using int to Integer . 您可能需要将使用int更改为Integer Integer is an object so it can be null. 整数是一个对象,因此可以为null。 This way you can check to see if it has been set before performing any further actions. 这样,您可以在执行任何其他操作之前检查它是否已设置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM