【问题标题】:Beginner Java project: what is wrong with my Array?初学者 Java 项目:我的数组有什么问题?
【发布时间】:2020-11-16 11:45:25
【问题描述】:

我刚开始在大学课程中学习 Java,但在第一个项目中遇到了问题。我刚刚开始创建一个基本上对硬币进行分类的项目。我正在尝试创建一个名为 printCoinList() 的方法,该方法打印硬币列表的内容,指示当前流通的面额(即“当前流通的硬币面额:200,100,50,20,10),以便士为单位。

到目前为止,我已经声明了我的实例字段,创建了一个参数并尝试创建此方法。我唯一的问题是,当我尝试在 main() 方法中对其进行测试时,使用数组作为 coinList 参数似乎有问题。这是我目前所拥有的:

public class CoinSorter {

    //Instance Fields
    String currency;
    int minCoinIn;
    int maxCoinIn;
    int[] coinList;
    
    //constructor
    public CoinSorter(String Currency, int minValueToExchange, int maxValueToExchange, int[] initialCoinList) {
        currency=Currency;
        minCoinIn=minValueToExchange;
        maxCoinIn = maxValueToExchange;
        coinList= initialCoinList;
        
    }
    
                public void printCoinList() {
        System.out.println("The current coin denominations are in circulation"
                + coinList);
    }
    
    
    public static void main(String[] args) {
        //An example
        
        CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, {10,20,50,100,200});

唯一的问题似乎在 exampleOne 中,因为当我将其取出时,其余代码似乎运行良好。 错误信息是:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The constructor CoinSorter(String, int, int, int, int, int, int, int) is undefined
    Syntax error on token "{", delete this token
    Syntax error on token "}", delete this token

那么有谁知道我做错了什么?

【问题讨论】:

    标签: java arrays sorting methods


    【解决方案1】:

    这是因为 数组初始值设定项 只能在声明站点指定或作为数组创建表达式的一部分。 (JLS § 10.6)

    下面是声明站点的数组初始化器。

    int[] array = { 2, 3, 5, 7, 11 };
    

    这是的缩写

    int[] array = new int[] { 2, 3, 5, 7, 11 };
    

    但是,与字符串文字不同,它不能用作“数组文字”。这意味着你必须写出数组创建表达式

    new CoinSorter("pounds", 0, 10000, new int[] { 10, 20, 50, 100, 200 });
    

    【讨论】:

    • 您好,谢谢!这已经奏效了。我使用了 new CoinSorter("pounds", 0, 10000, new int[] { 10, 20, 50, 100, 200 });但是现在当我尝试打印我的方法时,它会出现当前硬币面额正在流通[I@1c4af82c 而不是打印硬币面额,它只是打印内存。知道如何解决这个问题吗?非常感谢!
    • @Millyfg 这个问题已经是answered here了。
    • 谢谢!全部排序。
    • @Millyfg 不要忘记将其中一个答案标记为已接受,以让社区知道此问题已得到解决。
    【解决方案2】:

    可以使用以下方式之一声明/初始化 java 中的数组。

    int[] myIntArray = {10,20,50,100,200};
    int[] myIntArray = new int[]{10,20,50,100,200};
    

    替换CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, {10,20,50,100,200});

      CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, myIntArray );
    

       CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, new int[]{10,20,50,100,200});
    

    【讨论】:

      【解决方案3】:

      首先,在 java 中,您需要指定 Array 的类型:

      CoinSorter exampleOne = new CoinSorter("磅", 0, 10000, new int[]{10,20,50,100,200});

      那么您的“printCoinList”方法将无法正常工作,这应该打印:

      目前流通的硬币面额 [I@7852e922

      你的最终代码应该是:

      导入 java.util.Arrays;

      公共类 CoinSorter {

      //Instance Fields
      String currency;
      int minCoinIn;
      int maxCoinIn;
      int[] coinList;
      
      //constructor
      public CoinSorter(String Currency, int minValueToExchange, int maxValueToExchange, int[] initialCoinList) {
          currency=Currency;
          minCoinIn=minValueToExchange;
          maxCoinIn = maxValueToExchange;
          coinList= initialCoinList;
          
      }
      
                  public void printCoinList() {
          System.out.println("The current coin denominations are in circulation : "
                  + Arrays.toString(coinList));
      }
      
      
      public static void main(String[] args) {
          //An example
          CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, new int[]{10,20,50,100,200});
          exampleOne.printCoinList();
          
      }
      

      }

      结果: 目前流通的硬币面额:[10, 20, 50, 100, 200]

      祝你好运 :) 我希望我能在未来的交易所访问你并购买并持有一些加密货币 :D

      【讨论】:

        【解决方案4】:

        在创建对象CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, {10,20,50,100,200});之前声明并初始化一个数组int arr[]={10,20,50,100,200}然后在构造函数中传递它而不是传递{10,20,50,100,200}

        喜欢这个

        int arr[]={10,20,50,100,200};
        CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, arr);
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-10-17
          • 1970-01-01
          • 2022-11-14
          • 1970-01-01
          • 1970-01-01
          • 2017-07-31
          • 1970-01-01
          相关资源
          最近更新 更多