无法从 java 中的布尔方法获取 return
can't get a return from a boolean method in java
尝试检查单词的字母是否已排序alphabetically.but我没有从方法中得到任何return。
import java.util.Scanner;
public class A284 {
//Write a Java program to check
// if each letter of a given word (Abecadrian word) is less than the one before it.
public static boolean abecidarianWord(String word){
int index=word.length()-1;
for(int i=0;i<index;i++){
if (word.charAt(i)<=word.charAt(i+1)){
return true;
}else return false;
}
return true;
}
public static void main(String[] args) {
String entry;
System.out.println("input a word: ");
Scanner s1=new Scanner(System.in);
entry=s1.next();
abecidarianWord(entry);
}
}
您已成功返回value
。
import java.util.Scanner;
public class A284 {
public static boolean abecidarianWord(String word){
//you are getting length of "word" here
int index=word.length()-1;
for(int i=0;i<index;i++){
if (word.charAt(i)<=word.charAt(i+1)){
//If condition are correct return true.
return true;
}else{
//If condition are incorrect return false
return false;
}
}
return true;
}
public static void main(String[] args) {
String entry;
//Printing a text
System.out.println("input a word: ");
//getting values from user
Scanner s1=new Scanner(System.in);
entry=s1.next();
//calling a class
abecidarianWord(entry);
//You have get the value. But, you are actually trying to say that why it's not printing in output. When you return something you have to put them in another function to print-out
System.out.println(abecidarianWord(entry));
//If you don't wanna do it than you have to write SOUT instead of return. Than you can output the way you wrote
}
}
您在第一次比较时返回 true,因此您的循环只运行一次。相反,如下所示更改 for 循环内的 if 条件。
if (word.charAt(i)>word.charAt(i+1)){
return false;
}
@Istiak 完全正确。
但只是为了优化您的代码以执行我认为您理想中想要的操作,我只想说 if 语句 -> if (word.charAt(i)<=word.charAt(i+1))
会针对单词中的每两个字符进行迭代,而您不会想要 return true 如果只有两个字母是有序的,理想情况下用一个空的 ;
替换 return true;
否则你的函数将在找到一对两个连续正确放置的字母后立即停止。
你这里有两个问题。
首先,您没有使用 return 从 abecidarianWord
编辑的值,您只是调用它并忽略结果,因此您无法知道该方法将 return。所以你应该将 return 值分配给一个变量并用它做一些事情。例如,在你的 main
结束时,一个天真的实现会做这样的事情:
boolean isOrdered = abecidarianWord(entry);
if (isOrdered) {
System.out.println("String is ordered");
} else {
System.out.println("String is not ordered");
}
其次,在 abecidarianWord
中,您在循环的第一次迭代后立即 returning,这只会告诉您您的条件是否适用于前两个字符。
相反,您可能希望 return false
一旦发现 不 符合条件并且 return true
如果你在没有“意外”的情况下到达循环的末尾,那么类似于:
public static boolean abecidarianWord(String word) {
for (int i=0; i < word.length -1; i++) {
if (word.charAt(i) > word.charAt(i+1)) {
return false;
}
}
return true;
}
尝试检查单词的字母是否已排序alphabetically.but我没有从方法中得到任何return。
import java.util.Scanner;
public class A284 {
//Write a Java program to check
// if each letter of a given word (Abecadrian word) is less than the one before it.
public static boolean abecidarianWord(String word){
int index=word.length()-1;
for(int i=0;i<index;i++){
if (word.charAt(i)<=word.charAt(i+1)){
return true;
}else return false;
}
return true;
}
public static void main(String[] args) {
String entry;
System.out.println("input a word: ");
Scanner s1=new Scanner(System.in);
entry=s1.next();
abecidarianWord(entry);
}
}
您已成功返回value
。
import java.util.Scanner;
public class A284 {
public static boolean abecidarianWord(String word){
//you are getting length of "word" here
int index=word.length()-1;
for(int i=0;i<index;i++){
if (word.charAt(i)<=word.charAt(i+1)){
//If condition are correct return true.
return true;
}else{
//If condition are incorrect return false
return false;
}
}
return true;
}
public static void main(String[] args) {
String entry;
//Printing a text
System.out.println("input a word: ");
//getting values from user
Scanner s1=new Scanner(System.in);
entry=s1.next();
//calling a class
abecidarianWord(entry);
//You have get the value. But, you are actually trying to say that why it's not printing in output. When you return something you have to put them in another function to print-out
System.out.println(abecidarianWord(entry));
//If you don't wanna do it than you have to write SOUT instead of return. Than you can output the way you wrote
}
}
您在第一次比较时返回 true,因此您的循环只运行一次。相反,如下所示更改 for 循环内的 if 条件。
if (word.charAt(i)>word.charAt(i+1)){
return false;
}
@Istiak 完全正确。
但只是为了优化您的代码以执行我认为您理想中想要的操作,我只想说 if 语句 -> if (word.charAt(i)<=word.charAt(i+1))
会针对单词中的每两个字符进行迭代,而您不会想要 return true 如果只有两个字母是有序的,理想情况下用一个空的 ;
替换 return true;
否则你的函数将在找到一对两个连续正确放置的字母后立即停止。
你这里有两个问题。
首先,您没有使用 return 从 abecidarianWord
编辑的值,您只是调用它并忽略结果,因此您无法知道该方法将 return。所以你应该将 return 值分配给一个变量并用它做一些事情。例如,在你的 main
结束时,一个天真的实现会做这样的事情:
boolean isOrdered = abecidarianWord(entry);
if (isOrdered) {
System.out.println("String is ordered");
} else {
System.out.println("String is not ordered");
}
其次,在 abecidarianWord
中,您在循环的第一次迭代后立即 returning,这只会告诉您您的条件是否适用于前两个字符。
相反,您可能希望 return false
一旦发现 不 符合条件并且 return true
如果你在没有“意外”的情况下到达循环的末尾,那么类似于:
public static boolean abecidarianWord(String word) {
for (int i=0; i < word.length -1; i++) {
if (word.charAt(i) > word.charAt(i+1)) {
return false;
}
}
return true;
}