用里面的方法编写Java编程

时间:2016-06-26 23:01:42

标签: java

$('.performer').sort(function(a,b){
    if(a.dataset.username < b.dataset.username) return -1;
    if(a.dataset.username > b.dataset.username) return 1;
    return 0;
}).appendTo('#listPerformers');

我正在尝试通过一个示例,希望我编写一个程序,如果用户给出的数字在6到12之间,import java.util.Scanner ; public class ProcessNumbers { public static void main( String[] args ) { Scanner in = new Scanner(System.in) ; System.out.print("Please enter an integer between 6 and 12, inclusive: ") ; int num = in.nextInt() ; boolean result = shouldProcess(num); String result1 = processInput(result) ; } public static boolean shouldProcess(int n) { if (n>=6 && n<12) { return true; } else { return false; { } public static String processInput(String result2) { if (result2 = result) { System.out.println("Yes") ; } else { System.out.println("No") ; } } } 方法boolean返回shouldProcess,我做过的。接下来,我想使用名为true的方法,该方法使用之前的processInput来查看值是shouldProcess还是true。如果是false,我会继续进行其他计算;但如果它是假的,那么我想说这个数字是无效的。

当我输入一个值时,我收到此错误:

true

2 个答案:

答案 0 :(得分:0)

您将方法放在方法中:

public static void main(String[] args)
{
    //...
    public static boolean shouldProcess(int n)
    {
        //...
    }
}

该语法/结构无效。方法属于类,而不是彼此:

public static void main(String[] args)
{
    //...
}

public static boolean shouldProcess(int n)
{
    //...
}

答案 1 :(得分:0)

方法内部的方法在Java中是不可接受的。但是,当您调用方法时,如果第二个方法返回的数据类型与第一个方法的显式参数的数据类型匹配,则可以在括号内调用另一个方法。

以下是我发现的错误:

  1. 当您将result2设置为等于某个方法时,您已将A BOOLEAN作为显式参数传递,而您的processInput()方法将STRING数据类型定义为参数。这将导致数据类型不匹配错误。

  2. 你有一个开放的支架,你需要一个紧密的支架。

  3. 注意:如果收到错误消息,请找到错误行,如果您不知道错误的含义,请在本网站或任何其他已知网站上查找。