英文:
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 {
"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;
}
}
}
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{"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;
}
}
}
答案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[]{"apple", "orange", "mango", "pineapple" };
for(int i = 0; i< caseArray.length; i++) {
if(caseArray[i].equals("pineapple")) {
found = true;
break;
}
}
if(found == true) {
System.out.println("product found");
} else {
System.out.print("product not found");
}
}
}
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{"apple", "orange", "mango", "pineapple" };
-
in
equals()
check String has a syntax errorif(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 anif
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 = { "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();
}
}
}
Output:
product found
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论