为什么这种方法行不通?

| 这可能是一个非常简单的更正,我看不到,但是我很确定你们可以帮助我,这段代码应该读取用户输入的1-12(一年中的一个月)并添加一个到数组的位置(即,如果用户在数组中输入3,则它将在数组中将\'space \'2递增1,以计算发生的次数。),此代码仅需执行任何操作无所事事,并成功完成了通常的构建。 无论如何,我希望有人能给我一些提示,指出我要去哪里错了。
import java.util.Scanner;
public class BirthMonth {

    public static void main(String[] args){                               
        Scanner input = new Scanner(System.in); 
        int months [] = new int [12];    
    }

    public static int[] inputMonths(int[] months, Scanner input){

        System.out.println(\"please enter the first month with a birthday:\");
        int month = input.nextInt();
        months[month - 1] ++;
        //arr[i] = Input.nextInt();

        while (month != -1){
            System.out.println(\"please enter the next month to be tallied\");
            month = input.nextInt();
            months[month - 1] ++;
        }
        return months;               
    }
}
    
已邀请:
您必须在主方法中调用
inputMonths
方法...;)     
在您的主要方法中,您没有调用方法
inputMonths(int[] months, Scanner input)
。因此,您的程序除了创建阵列和初始化扫描器外不会做任何事情。您必须在主方法中添加调用。
public static void main(String[] args){                               
        Scanner input = new Scanner(System.in); 
        int months [] = new int [12];   
        inputMonths(months, input) 
    }
    

要回复问题请先登录注册