Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图在Java中操作字符串并不断获取“布尔值”_Java_String - Fatal编程技术网

试图在Java中操作字符串并不断获取“布尔值”

试图在Java中操作字符串并不断获取“布尔值”,java,string,Java,String,我在Eclipse中编写代码,因此我的一些错误消息可能会有所不同。我正在尝试打印一条消息,以便在字符串s3中没有字符“a”时,它会告诉用户尝试另一行代码。我语句的if部分没有问题,但else部分有问题。我似乎无法确定如何修复我的语句,因此如果ne可以帮忙,谢谢 //print the index of the last occurrence of the character 'a' in string s3 if (s3.indexOf('a')>= 0 { System.out.prin

我在Eclipse中编写代码,因此我的一些错误消息可能会有所不同。我正在尝试打印一条消息,以便在字符串s3中没有字符“a”时,它会告诉用户尝试另一行代码。我语句的if部分没有问题,但else部分有问题。我似乎无法确定如何修复我的语句,因此如果ne可以帮忙,谢谢

//print the index of the last occurrence of the character 'a' in string s3
if (s3.indexOf('a')>= 0 {
System.out.println("The first occurrence of the character 'a' in both strings is at index " + s3.indexOf('a'));
}
else (s3.indexOf('a') > 0 )
System.out.println("There is no occurrence of the character 'a' in either string.");
}
您的else在第二次测试中缺少else if。这是无效的,并且在第一次if测试中无法访问您在那里设置的条件。您可以使用else。类似

if (s3.indexOf('a')>= 0 {
    System.out.println("The first occurrence of the character 'a' "
            + "in both strings is at index " + s3.indexOf('a'));
} else {
    System.out.println("There is no occurrence of the character 'a' "
            + "in either string.");
}
或改变

使用elseif语句

if(s3.indexOf('a')>= 0     {
    System.out.println("The first occurrence of the character 'a' in both strings is at index " + s3.indexOf('a'));
}
else if (s3.indexOf('a') < 0 )
    System.out.println("There is no occurrence of the character 'a' in either string.");
}

当if表达式的计算结果为false时,if-then-else语句提供了执行的辅助路径。因此,在else子句中,您不需要条件

if (s3.indexOf('a')>= 0 {
    System.out.println("The first occurrence of the character 'a' in both strings is at index " + s3.indexOf('a'));
} else {
    System.out.println("There is no occurrence of the character 'a' in either string.");
}
而else子句中缺少了开头大括号。
有关更多信息,您可以阅读

,只需使用else即可
如果第二个else包含相同的条件,则第二个else与第一个else不可访问

首先在第一行添加一个右括号,这样代码将编译。
if(s3.indexOf('a')>= 0     {
    System.out.println("The first occurrence of the character 'a' in both strings is at index " + s3.indexOf('a'));
}
else if (s3.indexOf('a') < 0 )
    System.out.println("There is no occurrence of the character 'a' in either string.");
}
if (s3.indexOf('a')>= 0 {
    System.out.println("The first occurrence of the character 'a' in both strings is at index " + s3.indexOf('a'));
} else {
    System.out.println("There is no occurrence of the character 'a' in either string.");
}