本文介绍了局部变量未使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试一个输入的单词是否是回文(相同的拼写正向和反向)。从我可以看到它应该工作,但Eclipse说局部变量isPalindrome的值不被使用,但它被使用。问题是,即使这个词不是一个回文,它说是这样的。

  import java.util.Scanner; 

public class Palindrome {
public static void main(String [] args){
String phrase;
char [] phraseLetters;
int endChar;
boolean isPalindrome;

扫描仪输入=新的扫描仪(System.in);
System.out.println(输入单词或短语);
phrase = input.nextLine();
input.close();

phrase = phrase.toLowerCase();
phrase = phrase.replaceAll(,);
phraseLetters = phrase.toCharArray();

endChar = phraseLetters.length - 1; (int i = 0; i< phraseLetters.length; i ++){
if(phraseLetters [i]!= phraseLetters [endChar]){
isPalindrome = false ;
} else {
isPalindrome = true;
endChar - = 1;
}
}

if(isPalindrome = true){
System.out.println(输入的这个单词或短语是一个回文)​​;
} else {
System.out.println(这个词或短语不是回文);
}
}
}

编辑:我尝试过if(isPalindrome == true)

  if(isPalindrome)

在这两种情况下,Eclipse说本地变量isPalindrome可能未被初始化,在此if条件中。



FINAL EDIT:



我已经移动了,并重写了这段代码,但是如果还有人仍然修改了我的原始代码看看这个。



我在代码开头初始化了isPalindrome:



Boolean isPalinddrome = True;



我将for循环条件更改为:



for(int i = 0;(i< phraseLetters.length)&&(isPalindrome); i ++)


$ b $如果(isPalindrome = true)到 if(isPalindrome)


应该是 if(isPalindrome == true),则div class =h2_lin>解决方案

(或 if(isPalindrome) 更好!实际上这个错误是另一个很好的理由,为什么不问,如果 someBoolean == true 这是一个坏的风格)



By键入 if(isPalindrome = true)您再次将值 true 分配给变量 isPalindrome 。而且由于你只是为它赋值,所以编译器会向你发出关于未使用的变量的警告。



这也是很好的: p>

所以,当你执行 if(isPalindrome = true)然后if条件总是满足


I am trying to make a test of whether an inputted word is a palindrome or not (the same spelled forward and backward). From what I can see it should work but Eclipse says "The value of the local variable isPalindrome is not used" , but it is used. The problem is that even if the word is not a palindrome it says it is.

import java.util.Scanner;

public class Palindrome {
    public static void main(String[] args) {
        String phrase;
        char[] phraseLetters;
        int endChar;
        boolean isPalindrome;

        Scanner input = new Scanner(System.in);
        System.out.println("Enter a word or phrase.");
        phrase = input.nextLine();
        input.close();

        phrase = phrase.toLowerCase();
        phrase = phrase.replaceAll(" ","");
        phraseLetters = phrase.toCharArray();

        endChar = phraseLetters.length - 1;

        for (int i = 0; i < phraseLetters.length; i++) {
            if (phraseLetters[i] != phraseLetters[endChar]) {
                isPalindrome = false;
            } else {
                isPalindrome = true;
                endChar -= 1;
            }
        }

        if (isPalindrome = true) {
            System.out.println("This word or phrase entered is a palindrome.");
        } else {
            System.out.println("This word or phrase is not a palindrome.");
        }
    }
}

EDIT: I have tried the if statement being

    if (isPalindrome == true)

and

    if (isPalindrome)

In both cases Eclipse says "The local variable isPalindrome may not have been initialized," in this if condition.

FINAL EDIT:

I have since moved on, and rewritten this code, however I just went back and fixed my original code if anyone still looks at this.

I initialized isPalindrome at the beginning of the code:

Boolean isPalinddrome = True;

I changed the for-loop condition to:

for (int i = 0; (i < phraseLetters.length) && (isPalindrome); i++)

Finally I changed if (isPalindrome = true) to if (isPalindrome)

解决方案

if (isPalindrome = true) should be if (isPalindrome == true) (or if (isPalindrome) better! Actually this error is another good reason why not asking if someBoolean == true which is a bad style)

By typing if (isPalindrome = true) you're assigning, again, the value true to the variable isPalindrome. And since you're only assigning value to it, the compiler warns you about unused variable.

It's also good to know this:

So, when you do if (isPalindrome = true) then the if condition is always satisfied.

这篇关于局部变量未使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 06:36