主类中的函数是否必须包含在public static main的括号中?为什么

时间:2017-10-25 01:26:26

标签: java structure

这个小错误阻止了我的程序运行,我真的不明白为什么,我几乎尝试了所有的东西,并遵循程序为我推荐的所有修复,但它会给我另一个错误

import java.util.Scanner;
public class MiddleString {
    public static void main(String [] args) { 


// this part basically take 3 strings as inputs 
 Scanner in = new Scanner(System.in);
    System.out.println("Please enter your first string");
    String str1 = in.next();

    System.out.println("Please enter your second string");
    String str2 = in.next();

    System.out.println("Please enter your third string");
    String str3 = in.next();

    System.out.println("The middle string is: " + comparison(str1, str2, str3))*;*     

   // the ";" at the end of the line above is where I get an error that it needs 
//1 more "}" to finish the method but it will give another error

   // this function should return the middle string out of 3 string inputs 

public static String comparison(String string1, String string2, String string3) {   

if((string1.compareTo(string2) < 0 && string1.compareTo(string3) > 0) || (string1.compareTo(string2) > 0) && string1.compareTo(string3) < 0) {
            return string1;

    }
    else if((string2.compareTo(string1) < 0 && string2.compareTo(string3) > 0) || (string2.compareTo(string1) > 0 && string2.compareTo(string3) < 0)) {

            return string2;
    }
    else if((string3.compareTo(string1) < 0 && string3.compareTo(string2) > 0) || (string3.compareTo(string1) > 0 && string3.compareTo(string2) < 0)) {

            return string3;
    }


   } 
}

2 个答案:

答案 0 :(得分:1)

您只是在}方法的末尾错过了右括号(main)。此外,您的方法需要一个默认的return语句,因为如果所有三个if语句都评估为false而不执行,则线程将因缺少return语句而停止,程序也将停止。最后,我建议您使用in.nextLine();而不是in.next();,因为如果用户输入带有空格的字符串(例如"Hello world"),则程序将分配&#34; Hello&# 34;到str1和&#34;世界&#34;到str2

import java.util.Scanner;

public class MiddleString {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Please enter your first string");
        String str1 = in.nextLine(); //nextLine rather than next

        System.out.println("Please enter your second string");
        String str2 = in.nextLine(); //nextLine rather than next

        System.out.println("Please enter your third string");
        String str3 = in.nextLine(); //nextLine rather than next

        System.out.println("The middle string is: " + comparison(str1, str2, str3));
    } // Magical closing brace :O

    public static String comparison(String string1, String string2, String string3) {

        if ((string1.compareTo(string2) < 0 && string1.compareTo(string3) > 0) || (string1.compareTo(string2) > 0) && string1.compareTo(string3) < 0) {
            return string1;
        } else if ((string2.compareTo(string1) < 0 && string2.compareTo(string3) > 0) || (string2.compareTo(string1) > 0 && string2.compareTo(string3) < 0)) {
            return string2;
        } else if ((string3.compareTo(string1) < 0 && string3.compareTo(string2) > 0) || (string3.compareTo(string1) > 0 && string3.compareTo(string2) < 0)) {
            return string3;
        }
        return null; //Default return statement
    }
}

答案 1 :(得分:0)

如上所述,第一个问题:在main()的末尾缺少右括号'}'。  第二个问题:由于条件块中的“间隙”,您的return语句是“可选的”。当最小和中间字符串是相同的值(compareTo() - > 0)时,所有三个条件都是'false',因此没有返回和解析器错误。 也许你可以使用'&lt; ='来重新排列'if'块:

public static String comparison(String string1, String string2, String string3) {   

if(string1.compareTo(string2)<=0 && string1.compareTo(string3)<=0) { //1st smallest one
    if ( string2.compareTo(string3)<=0) return string2;  
    else return string3;
}else {  //1st  not smallest one
    if (string1.compareTo(string2)<=0 || string1.compareTo(string3)<=0) return string1;
    else if (string2.compareTo(string3)<=0) return string3;
    else return string2;
}

}