为什么布尔值没有被识别?

huangapple go评论63阅读模式
英文:

Why isn't the Boolean value recognized?

问题

public class HelloWorld {

    public static void main(string[] args) {
        boolean found;
        String[] caseArray = new caseArray {
            "apple", "orange", "mango", "pineapple"
        } ;
        for (int i = 0; i < caseArray.length; i++) {
            if (caseArray[i].equals("pineapple")) {
                found = true;
                break;
            } else {
                found = false;
            }
        }
        if (found == true) {
            System.out.println("product found");
            break;
        } else {
            System.out.print("product not found");
            break;
        }
    }
}
英文:
public class HelloWorld {

    public static void main(string[] args) {
        boolean found;
        String[] caseArray = new caseArray {
            &quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot;
        } ;
        for (int i = 0; i &lt; caseArray.length; i++) {
            if (caseArray[i].equals(&quot;pineapple&quot;)) {
                found = true;
                break;
            } else {
                found = false;
            }
        }
        if (found == true) {
            System.out.println(&quot;product found&quot;);
            break;
        } else {
            System.out.print(&quot;product not found&quot;);
            break;
        }
    }
}

In the above code I would like to return found = true when the for loop searches pineapple in the array. If found is equal to true I would like it to print "product found", if found = false I would like it to print "product not found".

答案1

得分: 1

我认为这个代码不会编译,因为你的布尔变量没有初始化。在长度为0的数组情况下,这个布尔值永远不会被初始化,而Java不允许这样做。

public class HelloWorld {

    public static void main(String[] args) {

        boolean found = false;

        String[] caseArray = new String[]{"apple", "orange", "mango", "pineapple"};

        for (int i = 0; i < caseArray.length; i++) {

            if (caseArray[i].equals("pineapple")) {

                found = true;
                break;
            } else {

                found = false;
            }

        }

        if (found == true) {

            System.out.println("product found");
            break;

        } else {
            System.out.print("product not found");
            break;
        }

    }
}
英文:

I don't think this will compile, because of your uninitialized boolean variable. In the case of a 0 length array, the found boolean will never get initialed, and Java won't let you do that

public class HelloWorld{

    public static void main(string [] args){

    boolean found = false;

    String [] caseArray = new caseArray{&quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot; };

    for(int i = 0; i&lt; caseArray.length; i++){

                                    if(caseArray[i].equals(pineapple)){

                                        found = true;
                                        break;
                                    } else{

                                        found = false;
                                    }

                                }

                                if(found == true){

                                    System.out.println(&quot;product found&quot;);
                                    break;

                                } else {
                                    System.out.print(&quot;product not found&quot;);
                                    break;
                                }    

    }
}

答案2

得分: 1

你的代码没有正确遵循Java语法。
以下是可以正常工作的代码:

public class HelloWorld {
    public static void main(String[] args) {
        boolean found = false;
        String[] caseArray = new String[]{"apple", "orange", "mango", "pineapple"};

        for (int i = 0; i < caseArray.length; i++) {
            if (caseArray[i].equals("pineapple")) {
                found = true;
                break;
            }
        }

        if (found) {
            System.out.println("product found");
        } else {
            System.out.print("product not found");
        }
    }
}

在你的帖子中,以下部分不符合语法:
首先,在你的代码中:

public static void main(string[] args) {

上述代码中,args应为String[](首字母大写),而不是小写的string[]

其次,在你的代码中:

String[] caseArray = new caseArray{...

这里,caseArray不是Java的类型,应该更新为以下形式:

String[] caseArray = new String[]{...

还有,在第三个问题中,我已将found的初始值设置为false,如果我们这样使用,就不需要在for循环中再次设置found = false

英文:

Your code does not follow the java grammar correctly.
This will work.

public class HelloWorld{
    public static void main(String[] args){
        boolean found = false;
        String[] caseArray = new String[]{&quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot; };

        for(int i = 0; i&lt; caseArray.length; i++) {
            if(caseArray[i].equals(&quot;pineapple&quot;)) {
                found = true;
                break;
            }
        }
        
        if(found == true) {
            System.out.println(&quot;product found&quot;);
        } else {
            System.out.print(&quot;product not found&quot;);
        }
    }
}

On your post, those are not following the grammar.
First, on your code

public static void main(string[] args) {

On above code, args is string[] (small s first) and it should be uppercase. (String[])

Second, on your code

String[] caseArray = new caseArray{...

Here, caseArray is not the java type and it should be updated as follows.

String[] caseArray = new String[]{...

And third, I have set the initial value of found to false and if we use like this, no need to set found = false in for query.

答案3

得分: 0

以下是已经翻译好的部分:

首先,代码中有几个语法问题。

  • 字符串数组的初始化方式不正确
String[] caseArray = new caseArray{"apple", "orange", "mango", "pineapple"};
  • equals()方法的字符串比较部分有语法错误
if(caseArray[i].equals(pineapple)){
  • 没有对布尔变量进行初始化,但在后面尝试使用了该变量。

  • break; 关键字在if条件中的循环之外被使用。

我已经纠正了语法错误,以便您可以执行代码。

public class Test {
    public static void main(String[] args) {
        try {
            boolean found = false;
            String[] caseArray = { "apple", "orange", "mango", "pineapple" };
            for (int i = 0; i < caseArray.length; i++) {
                if (caseArray[i].equals("pineapple")) {
                    found = true;
                    break;
                }
            }

            if (found) {
                System.out.println("product found");
                // break;
            } else {
                System.out.print("product not found");
                // break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出结果:

product found
英文:

First of all there are couple of syntax issue with this code.

  • the String array initialization is wrong

    String [] caseArray = new caseArray{&quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot; };
    
  • in equals() check String has a syntax error

    if(caseArray[i].equals(pineapple)){
    
  • didn't initialize the boolean but you try to use that variable later.

  • break; keyword is used outside a loop in an if condition.

I have corrected the syntax so that you can execute it.

public class Test {
	public static void main(String[] args) {
		try {
			boolean found = false;
			String[] caseArray = { &quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot; };
			for (int i = 0; i &lt; caseArray.length; i++) {
				if (caseArray[i].equals(&quot;pineapple&quot;)) {
					found = true;
					break;
				}
			}

			if (found) {
				System.out.println(&quot;product found&quot;);
				// break;
			} else {
				System.out.print(&quot;product not found&quot;);
				// break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output:

product found

huangapple
  • 本文由 发表于 2020年9月18日 22:47:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63957985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定