简体   繁体   English

为什么我必须初始化变量?

[英]Why do I have to initialise variable?

My code is checking if a given string is in a certain format.我的代码正在检查给定的字符串是否采用某种格式。 The first char of the string has to be an uppercase letter and the rest of the string has to be any number that is from 1 to given dimension.字符串的第一个字符必须是大写字母,字符串的其余部分必须是从 1 到给定维度的任何数字。

If the first char of the string contains a string from the alphabet array, then the code checks if the rest of the string contains a number from the numbers array.如果字符串的第一个字符包含来自字母数组的字符串,则代码检查字符串的其余部分是否包含来自数字数组的数字。 To be a valid coordinate both conditions must be true, if one of them is false it is not a valid coordinate.要成为有效坐标,两个条件都必须为真,如果其中一个为假,则它不是有效坐标。 I want to return the boolean isValidCoordinate but however IntelliJ is telling me that I have to initialise isValid coordinate.我想返回布尔值 isValidCoordinate 但 IntelliJ 告诉我我必须初始化 isValid 坐标。 Why do I have to initialise it, the boolean expression depends on the 'if' conditions.为什么我必须初始化它,布尔表达式取决于“if”条件。

Thanks.谢谢。

public static boolean validCoordinate(String coordinate, int dimension) {
        boolean isValidCoordinate;
        String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        int [] numbers = new int [dimension];
        int one = 1;
        for(int i = 0; i < dimension; i++){
            numbers[i] = one + i;
        }
        for(int i = 0; i < dimension; i++){
            if((Character.toString(coordinate.charAt(0))).contains(alphabet[i])) {
                for(int j = 0; j < dimension; j++) {
                    if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
                        isValidCoordinate = true;
                    }
                    else {
                        isValidCoordinate = false;
                    }
                }

            }
            else {
                isValidCoordinate = false;
            }

        }

        return isValidCoordinate;
    }

This is my final code:这是我的最终代码:

public static boolean validCoordinate(String coordinate, int dimension) {
        boolean isValidCoordinate;
        String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        int [] numbers = new int [dimension];
        int one = 1;
        for(int i = 0; i < dimension; i++){
            numbers[i] = one + i;
        }
        for(int i = 0; i < dimension; i++){
            if((Character.toString(coordinate.charAt(0))).contains(alphabet[i])) {
                for(int j = 0; j < dimension; j++) {
                    if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
                        isValidCoordinate = true;
                    }
                    else {
                        isValidCoordinate = false;
                    }
                }

            }
            else {
                isValidCoordinate = false;
            }

        }

        return true;
    }

All the cases inside the for loop are covered, but not the case when dimension is 0. for循环内的所有情况都被覆盖,但dimension为 0 的情况除外。

The method will go straight to return isValidCoordinate;该方法将直接return isValidCoordinate; , where the variable isn't initialized yet. ,其中变量尚未初始化。

It is possible that your for loop will never be entered (for example, if dimension == 0 ).您的 for 循环可能永远不会被输入(例如,如果dimension == 0 )。 In that case, you will never assign a value to isValidCoordinate , but you will attempt to return the value of that variable in the last statement of your method.在这种情况下,您永远不会为isValidCoordinate ,但您将尝试在方法的最后一条语句中返回该变量的值。

What would be the value of isValidCoordinate in such case?在这种情况下isValidCoordinate的值是isValidCoordinate

It wouldn't have any value.它不会有任何价值。

Therefore, you are forced by the compiler to assign an initial value to isValidCoordinate , to make sure that it has a value before it is accessed.因此,编译器强制您为isValidCoordinate分配一个初始值,以确保它在访问之前具有值。

EDIT:编辑:

Following your comments, I suggest you eliminate the boolean variable, and use return statements instead:根据您的评论,我建议您消除boolean变量,并改用 return 语句:

public static boolean validCoordinate(String coordinate, int dimension) {
    String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    int [] numbers = new int [dimension];
    int one = 1;
    for(int i = 0; i < dimension; i++){
        numbers[i] = one + i;
    }
    for(int i = 0; i < dimension; i++){
        if((Character.toString(coordinate.charAt(0))).contains(alphabet[i])) {
            for(int j = 0; j < dimension; j++) {
                if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
                    return true;
                }
            }
        }
    }

    return false;
}

This way you don't have to worry about breaking out of nested loops when the boolean variable is set to true .这样,当boolean变量设置为true时,您不必担心会中断嵌套循环。

import java.util.regex.*;

public static boolean validCoordinate(String coordinate, int dimension)
{
    Pattern p = Pattern.compile("[A-Z]+");
    Matcher m = p.matcher(coordinate);
    return m.matches();
}

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

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