如何创建一个数组?

huangapple go评论70阅读模式
英文:

How to do an array?

问题

我有这个程序,它不起作用,他们要求我返回一个包含barraCaramelo、chicles和cuponesRestantes的数组。这些产品分别花费10个优惠券、3个优惠券和剩余的优惠券。

如果有人能解决这个问题,将为我做一件巨大的好事,我真的不明白这行代码(public static int[] candyCalculator(int num_coupons))是如何工作的,他们要求我创建一个public static void main(String args[]),但我不知道该怎么做。
谢谢

class main {
    public static int[] candyCalculator(int num_coupons) {
        int barraCaramelo = num_coupons / 10;//这个花费10个优惠券
        int cuponesChicles = num_coupons % 10;
        int chicles = cuponesChicles / 3;//花费3个
        int cuponesRestantes = cuponesChicles % 3;//剩余的优惠券
        int[] arr = new int[3];
        arr[0] = barraCaramelo;
        arr[1] = chicles;
        arr[2] = cuponesRestantes;
        return arr;
    }

    public static void main(String args[]) {
        int num_coupons = 10;
        int[] a = candyCalculator(num_coupons);
        for (int i = 0; i < 3; i++) {
            System.out.print(a[i]);
        }
    }
}
英文:

I have this program that doesn't work, they ask me to return an array with barraCaramelo, chicles and cuponesRestantes. Each of this products cost coupons barraCaramelo (10 coupons), chicles(3 coupons), and cuponesRestantes are the coupons that remains.

If someone can solve this will do me a enormous favor, I don't really understand how this line (public static int[] candyCalculator(int num_coupons)) works, they ask me to create a public static void main(String args[]) but I don't know how to do it.
Thanks

      public static int[] candyCalculator(int num_coupons) {
        int barraCaramelo = num_coupons / 10;//this cost 10 coupons
        int cuponesChicles = num_coupons % 10;
        int chicles = cuponesChicles / 3;// cost 3
        int cuponesRestantes = cuponesChicles % 3;//coupons that remain
        int[] arr=new int[3];
        arr[0]=barraCaramelo;
        arr[1]=chicles;
        arr[2]=cuponesRestantes;
        return arr;

    }
    public static void main(String args[]){
        num_coupons=10;
        int[] a=candyCalculator();
        for (int i=0;i&lt;4;i++){
            System.out.print(a[i]);
    }
 

</details>


# 答案1
**得分**: 2

我认为我们混淆了两种不同的变量,并且知道哪个是哪个非常重要。

在继续之前,确切了解您的问题的上下文非常重要,因为目前我们无法确定请求是关于barraCaramelo(数量)还是barraCaramelo(用券的成本)。

现在,关于您对这行代码如何工作的问题:

public static int[] candyCalculator(int num_coupons) {

这就是我们所谓的“方法声明”,意味着您的程序正在声明代码的一部分来执行指定的任务,然后可以从代码的其他部分调用它。从您的描述来看,似乎您想打印数组的每一行,但不是完全清楚。

<details>
<summary>英文:</summary>

I think we&#39;re mixing two kind of variables here and it&#39;s important to know what is what. 

It&#39;s very important we know for sure the context of your issue before going any further, as it stands we can&#39;t tell if the request is for barraCaramelo (quantity) or barraCaramelo (cost in coupons). 

Now, regarding your question of  how this line works: 

      public static int[] candyCalculator(int num_coupons) {
    
This is what we call a &quot;method declaration&quot;, means your program is declaring a part of the code to perform a designated task, which can then be called from other parts of the code. For what it looks like you want to print each line of the array, but it&#39;s not entirely clear. 


</details>



# 答案2
**得分**: 2

你应该在方法调用中传递一个参数,像这样:

```java
int[] a = candyCalculator(10);

这个循环:

for (int i=0;i<4;i++){
    System.out.print(a[i]);
}

由于你的数组只有3个元素,将会引发一个数组越界异常,因此条件应该更正为:

for (int i=0;i<3;i++){
    System.out.print(a[i]);
}

至于你关于方法 public static int[] candyCalculator(int num_coupons) 的问题:

  1. public 是一个访问修饰符,指定谁可以访问这个方法。

  2. static 表示这个方法可以在不创建类实例的情况下访问。(参见这个答案

  3. int[] 是方法的返回类型。

  4. candyCalculator 是方法的名称。

  5. int num_coupons 是传递的参数,其中 int 是数据类型,num_coupons 是参数名称。

英文:

You should pass a parameter to your method call like,

int[] a = candyCalculator(10);

This loop,

for (int i=0;i&lt;4;i++){
            System.out.print(a[i]);
    }

will give an indexOutOfBounds Exception as your array only has 3 elements therefore the condition should be corrected to,

for (int i=0;i&lt;3;i++){
                System.out.print(a[i]);
        }

As for your question regarding the method public static int[] candyCalculator(int num_coupons)

  1. public is an access modifier which specifies who can access this method.如何创建一个数组?

  2. static means that this method can be accessed without creating an instance of a class. (See this answer)

  3. int[] is the return type of the method.

  4. candyCalculator is the method name.

  5. int num_coupons is the parameter passed, where int is the datatype and num_coupons is the parameter name.

答案3

得分: 0

你的代码中有很多错误。以下是可运行的代码示例,与你的代码进行了比较:

public class Main {
    public static int[] candyCalculator(int num_coupons) {
        int candyBars = num_coupons / 10; // 这需要10张优惠券
        int remainingCoupons = num_coupons % 10;
        int gums = remainingCoupons / 3; // 每个需要3张优惠券
        int remainingCouponsAfterGums = remainingCoupons % 3; // 剩余的优惠券数量
        int[] result = new int[]{candyBars, gums, remainingCouponsAfterGums};
        return result;
    }

    public static void main(String[] args) {
        int num_coupons = 10;
        int[] result = candyCalculator(num_coupons);
        for (int i = 0; i < 3; i++) {
            System.out.println(result[i]);
        }
    }
}

希望这个修正后的代码对你有所帮助。

英文:

You have many mistake in your code.
Compare the following which is runnable with your code.


public class Main
{
	    public static int[] candyCalculator(int num_coupons) {
        int barraCaramelo = num_coupons / 10;//this cost 10 coupons
        int cuponesChicles = num_coupons % 10;
        int chicles = cuponesChicles / 3;// cost 3
        int cuponesRestantes = cuponesChicles % 3;//coupons that remain
        int[] arr=new int[3];
        arr[0]=barraCaramelo;
        arr[1]=chicles;
        arr[2]=cuponesRestantes;
        return arr;

    }
    public static void main(String args[]){
        int num_coupons=10;
        int[] a=candyCalculator(num_coupons);
        for (int i=0;i&lt;3;i++)
            System.out.println(a[i]);
    }
}

答案4

得分: 0

主方法

你已经有一个主方法:

public static void main(String args[]){
    num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i<a.length;i++){
        System.out.print(a[i]);
    }
}

num_coupons

你在这里将 num_coupons 的值设置为十,但是至少你的代码没有包含它的声明。你可以在主方法中声明它作为一个局部变量,像这样:

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i<a.length;i++){
        System.out.print(a[i]);
    }
}

越界索引

因为 candyCalculator 创建了一个包含三个项目的数组,最后一个索引是2,所以你的主循环将在达到值3并尝试在数组中查找该索引的值时崩溃。让我们使你的循环适应数组的长度:

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i<a.length;i++){
        System.out.print(a[i]);
    }
}

传递参数

candyCalculator 期望你传递一个整数给它,但你在那里有空的括号,这意味着你没有提供它期望获得的参数。让我们传递正确的值:

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator(num_coupons);
    for (int i=0;i<a.length;i++){
        System.out.print(a[i]);
    }
}
英文:

Main method

You already have a main method:

public static void main(String args[]){
    num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i&lt;4;i++){
        System.out.print(a[i]);
}

num_coupons

You set the value of num_coupons to ten here but, at least your code does not contain its declaration. You could declare it in your main method as a local variable, like

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i&lt;4;i++){
        System.out.print(a[i]);
}

Index out of bounds

Since candyCalculator creates an array of three items, the last index being 2, your for cycle in your main will crash and burn when it reaches the value of 3 and try to find the value of that index in your array. Let's make your loop fit your array's length:

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator();
    for (int i=0;i&lt;a.length;i++){
        System.out.print(a[i]);
}

Passing a parameter

candyCalculator expects you to pass an integer to it, but you have empty paranthesis there, which means that you do not provide the parameter it expects to get. Let's pass the proper value:

public static void main(String args[]){
    int num_coupons=10;
    int[] a=candyCalculator(num_coupons);
    for (int i=0;i&lt;a.length;i++){
        System.out.print(a[i]);
}

huangapple
  • 本文由 发表于 2020年8月5日 19:10:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63263942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定