【问题标题】:Store contents from an array into another array将数组中的内容存储到另一个数组中
【发布时间】:2014-04-19 22:01:54
【问题描述】:

所以它的作用是将随机值存储在 31-58 的数组元素中,然后将它们加起来并平均,然后我只是将它们显示出来。

我的问题是:如何将天数数组用于从 31-58(2 月的 28 天)中为每个随机数指定一天并将其存储到第二个数组(工作日 [ ])。这样在程序的最后,我可以根据需要显示日期名称。

我的代码看起来像。(这只是一个片段)

double [] anArray = new double[366];//Keep track of all the days in the year
    String [] weekday = new String[366];//I hope to store the day names in this array
    String days[]={"monday","tuesday","wednesday","thursday",
        "friday","saturday","sunday"};

//---each letter is 0 to start each new month at monday---
int a=0;int b=0;int c=0;int d=0;int e=0;int f=0;int g=0;int h=0;int i=0;
int j=0;int k=0;int l=0;//Days counter
//--These start at 1 to start each new month on the 1st
int jandays=1;int febdays=1;int mardays=1;int aprdays=1;int maydays=1;int jundays=1;
int juldays=1;int augdays=1;int septdays=1;int octdays=1;int novdays=1;int decdays=1;

            //Temperatures for February
for(int feb=31;feb <= 58;feb++){
    anArray[feb]=(int)(26 + Math.random() * (40-26+1));

    //System.out.println(days[b++ %7] + " january "+ (febdays++) +" at "+ anArray[feb]+" degrees"); // This is just testing to see if everything checks out.

    febtotal += anArray[feb];
}
febavg= febtotal/31;
System.out.printf("The Average temperature in February: "+"%.2f",febavg);
System.out.println("");

我的要求实际上让我有点困惑 o.o.

【问题讨论】:

    标签: java arrays loops


    【解决方案1】:

    当您发现自己要处理许多不同的array 类型时,最好深呼吸一下,用对象来思考。

    public class WeekDay {
        private int number;
        private String name;
    
    
        public WeekDay(int number, String name) {
             // Your constructor here.
        }
    
        /** GETTERS AND SETTERS **/
    }
    

    然后你创建一个List 来包含它们..

    List<WeekDay> weekDays = new ArrayList<WeekDay>();
    

    WeekDay 对象被平均后,您可以循环遍历每个对象,然后调用getName() (您需要添加它) 以返回当天的名称。这提供了更清洁、更易于管理和更美观的代码,它将帮助您更有效地概念化您的目标。老实说,OOP 非常适合复杂的情况。

    编辑

    这里的技巧是将index 映射到日期名称。假设你有一个这样的数组..

     [0] = 0
     [1] = 0
     .
     .
     .
     [34] = 3
     [35] = 9
     .
     .
     .
     [62] = 7
     [63] = 0
    

    您的号码介于3462 之间。这些值需要映射到天,所以让我们从创建一个方法开始。

     public String[] mapDays(int[] dayNumbers, int startNumber, int endNumber) {
        // Your code in here.
     }
    

    传入包含所有数字的数组,并传入月份数字的开始和结束时间。然后,您将执行一些检查。

     // First, you make a new array.
     String[] dayNames = new String[dayNumbers.length];
    
     for(int x = startNumber; x <= endNumber; x++) {
          // You now need to map the value of x, to the week day name.
    
     }
    

    【讨论】:

    • 不幸的是,我不知道如何处理方法或对象,因为我在编程课上还没有走那么远。此分配基于数组。这相当困难,所以我想我必须只使用数组。有什么办法可以在没有对象的情况下做到这一点?
    • 对你直言不讳,兄弟。我还没有做过方法或任何其他方法。我得到的最高值也是数组。当我现在需要专注于数组时,我认为我不能花几个小时来理解那些。
    • 从我的编辑中可以看出,我所做的只是一些 if 声明,我相信你在查看 array 之前应该应该了解这些声明s.. 但我会为你简化它。
    • 我非常感谢您的帮助。甚至没有多少人愿意这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    相关资源
    最近更新 更多