简体   繁体   中英

why won't this method work?

this is probably a very simple correction that I cannot see but I'm pretty sure you guys can help me, this section of the code is supposed to read what the user inputs 1-12 (for a month of the year) and add one to the array location (ie if a user inputs 3 to the array then it will increment 'space' 2 in the array by one to tally the amount of occurences so to speak.), this code just goes through without any action taking place and gives the usual build successful after doing nothing.

anyway, I was hoping someone could give me a few pointers on where I'm going wrong.

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方法...;)

In your main method you're not calling your method inputMonths(int[] months, Scanner input) . So, your program won't do anything except creating the array and initialising the scanner. You have to add the call in your main method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM