为什么这个方法不起作用?

发布于 2024-10-31 17:47:15 字数 930 浏览 3 评论 0 原文

这可能是一个非常简单的更正,我看不到,但我很确定你们可以帮助我,这部分代码应该读取用户输入的内容 1-12 (一年中的一个月)并添加一个到数组位置(即,如果用户向数组输入 3,那么它将在数组中将“空格”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;               
    }
}

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 (i.e. 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;               
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灰色世界里的红玫瑰 2024-11-07 17:47:15

您必须在主方法中调用 inputMonths 方法...;)

You have to call your inputMonths method in your main method... ;)

千纸鹤 2024-11-07 17:47:15

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

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) 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文