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

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

Why isn't the Boolean value recognized?

问题

  1. public class HelloWorld {
  2. public static void main(string[] args) {
  3. boolean found;
  4. String[] caseArray = new caseArray {
  5. "apple", "orange", "mango", "pineapple"
  6. } ;
  7. for (int i = 0; i < caseArray.length; i++) {
  8. if (caseArray[i].equals("pineapple")) {
  9. found = true;
  10. break;
  11. } else {
  12. found = false;
  13. }
  14. }
  15. if (found == true) {
  16. System.out.println("product found");
  17. break;
  18. } else {
  19. System.out.print("product not found");
  20. break;
  21. }
  22. }
  23. }
英文:
  1. public class HelloWorld {
  2. public static void main(string[] args) {
  3. boolean found;
  4. String[] caseArray = new caseArray {
  5. &quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot;
  6. } ;
  7. for (int i = 0; i &lt; caseArray.length; i++) {
  8. if (caseArray[i].equals(&quot;pineapple&quot;)) {
  9. found = true;
  10. break;
  11. } else {
  12. found = false;
  13. }
  14. }
  15. if (found == true) {
  16. System.out.println(&quot;product found&quot;);
  17. break;
  18. } else {
  19. System.out.print(&quot;product not found&quot;);
  20. break;
  21. }
  22. }
  23. }

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不允许这样做。

  1. public class HelloWorld {
  2. public static void main(String[] args) {
  3. boolean found = false;
  4. String[] caseArray = new String[]{"apple", "orange", "mango", "pineapple"};
  5. for (int i = 0; i < caseArray.length; i++) {
  6. if (caseArray[i].equals("pineapple")) {
  7. found = true;
  8. break;
  9. } else {
  10. found = false;
  11. }
  12. }
  13. if (found == true) {
  14. System.out.println("product found");
  15. break;
  16. } else {
  17. System.out.print("product not found");
  18. break;
  19. }
  20. }
  21. }
英文:

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

  1. public class HelloWorld{
  2. public static void main(string [] args){
  3. boolean found = false;
  4. String [] caseArray = new caseArray{&quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot; };
  5. for(int i = 0; i&lt; caseArray.length; i++){
  6. if(caseArray[i].equals(pineapple)){
  7. found = true;
  8. break;
  9. } else{
  10. found = false;
  11. }
  12. }
  13. if(found == true){
  14. System.out.println(&quot;product found&quot;);
  15. break;
  16. } else {
  17. System.out.print(&quot;product not found&quot;);
  18. break;
  19. }
  20. }
  21. }

答案2

得分: 1

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

  1. public class HelloWorld {
  2. public static void main(String[] args) {
  3. boolean found = false;
  4. String[] caseArray = new String[]{"apple", "orange", "mango", "pineapple"};
  5. for (int i = 0; i < caseArray.length; i++) {
  6. if (caseArray[i].equals("pineapple")) {
  7. found = true;
  8. break;
  9. }
  10. }
  11. if (found) {
  12. System.out.println("product found");
  13. } else {
  14. System.out.print("product not found");
  15. }
  16. }
  17. }

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

  1. public static void main(string[] args) {

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

其次,在你的代码中:

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

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

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

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

英文:

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

  1. public class HelloWorld{
  2. public static void main(String[] args){
  3. boolean found = false;
  4. String[] caseArray = new String[]{&quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot; };
  5. for(int i = 0; i&lt; caseArray.length; i++) {
  6. if(caseArray[i].equals(&quot;pineapple&quot;)) {
  7. found = true;
  8. break;
  9. }
  10. }
  11. if(found == true) {
  12. System.out.println(&quot;product found&quot;);
  13. } else {
  14. System.out.print(&quot;product not found&quot;);
  15. }
  16. }
  17. }

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

  1. 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

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

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

  1. 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

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

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

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

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

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. try {
  4. boolean found = false;
  5. String[] caseArray = { "apple", "orange", "mango", "pineapple" };
  6. for (int i = 0; i < caseArray.length; i++) {
  7. if (caseArray[i].equals("pineapple")) {
  8. found = true;
  9. break;
  10. }
  11. }
  12. if (found) {
  13. System.out.println("product found");
  14. // break;
  15. } else {
  16. System.out.print("product not found");
  17. // break;
  18. }
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }

输出结果:

  1. product found
英文:

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

  • the String array initialization is wrong

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

    1. 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.

  1. public class Test {
  2. public static void main(String[] args) {
  3. try {
  4. boolean found = false;
  5. String[] caseArray = { &quot;apple&quot;, &quot;orange&quot;, &quot;mango&quot;, &quot;pineapple&quot; };
  6. for (int i = 0; i &lt; caseArray.length; i++) {
  7. if (caseArray[i].equals(&quot;pineapple&quot;)) {
  8. found = true;
  9. break;
  10. }
  11. }
  12. if (found) {
  13. System.out.println(&quot;product found&quot;);
  14. // break;
  15. } else {
  16. System.out.print(&quot;product not found&quot;);
  17. // break;
  18. }
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }

Output:

  1. 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:

确定