简体   繁体   English

为什么这种方法行不通?

[英]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. 这可能是一个非常简单的更正,我看不到,但是我敢肯定,你们可以帮助我,这段代码应该读取用户输入的1-12(一年中的一个月)并添加一个到数组的位置(即,如果用户在数组中输入3,则它将使数组中的“空间” 2加1以计算发生的次数。),此代码将继续执行而不会发生任何操作,并且不执行任何操作后即可成功完成通常的构建。

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) . 在您的主要方法中,您没有调用方法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) 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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