如何在java中组织和打印数组的值?

时间:2016-12-14 13:19:03

标签: java arrays

我hava使用for循环创建了一个程序,用于确定创建的数组的数量。我必须按升序组织每个arrey,所以我创建了另一个执行此操作的类( Organizer ),但我不知道如何导出每个数组(主类中的数组代码)< /强>

一旦我对这些arreys进行了编程,我需要计算并打印数组值的结果。

以下是输出必须显示的示例。

如果arrey值相同,则总值增加10

如果arrey值是一个序列,则总值增加20

  

输入轮数:4

     

第1轮 - &gt; 1 3 7总值:11

     

第2轮 - &gt; 1 4 5总值:10

     

第3轮 - &gt; 1 1 1总值:13

     

第4轮 - &gt; 1 2 3总值:26

PS我是java的新手。我认为我接近解决方案,但我无法弄清楚如何克服这个问题

组织者类

public class Organizer {
     // ---------------------- ATTRIBUTES ---------------------
     protected int rand1;
     protected int rand2;
     protected int rand3;

     // ------------------ CONSTRUCTOR -------------------
     public Organizer(int o1, int o2, int o3) {
          // This puts the three values from arrey in ascending order.
          int tmp;
          if (o2 < o1) {
              tmp = o2;
              o2 = o1;
              o1 = tmp;
          }
          if (o3 < o2) {
              tmp = o3;
              o3 = o2;
              o2 = tmp;
          }
          if (o2 < o1) {
              tmp = o2;
              o2 = o1;
              o1 = tmp;
          }
          rand1 = o1;
          rand2 = o2;
          rand3 = o3;
     }

     // --------------------- METHODS ---------------------
     // Accessor methods
     public int getRand1() {
          return rand1;
     }

     public int getRand2() {
          return rand2;
     }

     public int getRand3() {
          return rand3;
     }

     // Will return true if all values are the same. 
     // This depends on the values being ordered.
     public boolean threeSame() {
          return (rand1 == rand3);
     }

     // Will return true if we have a run e.g. "one, two, three", or "four, five, six".
     // Again, this depends upon the values being ordered.  
     public boolean sequence() {
          return (( (rand1 + 1) == rand2) && ( (rand2 + 1) == rand3));
     }


     public void printResult() {
          if (threeSame())
              System.out.println("The values are all the same.");
          else if (sequence())
              System.out.println("The values are a sequence.");

    }

}

计算器类

public class Calculator extends Organizer {

public static int totalValue;

   public void calcSumOfValues(int s1, int s2, int s3) {
     int sumOfArrey;
   sumOfArrey= s1 + s2 + s3;
   }
    public void calcSumOfExtraValues(int sumOfArrey) {
            if (threeSame())
              totalValue= sumOfArrey + 10;
            else if (sequence())
              totalValue= sumOfArrey + 20;
            else
              totalValue= sumOfArrey
   }
        public void printResult() {
          System.out.println("round " + (r+1) + "--> " + randomArray [i] + "Total value" + totalValue  );
        }
   }

主要类

import java.util.Scanner;

public class UserClass {

  // ------------------- FIELDS ------------------------    
        // Create instance of Scanner class
        public static Scanner input = new Scanner(System.in);
        // variables
        public static Organizer orga;
        public static Calculator calc;

        public static int randomArray [];

    // ------------------ METHODS ------------------------  
        public static void main(String[] args) {

        int rounds; // input by user

        System.out.print("Please input number of rounds (grater or equal than 0) --> ");
        rounds = input.nextInt();
        System.out.print("\n");

        for (int r = 0; r < rounds; r++) {  // loop for number of rounds
        int randomArray [] = new int [3];
        for (int i = 0; i < randomArray.length; i++) { // loop for random Array 
        randomArray [i] = (int)(Math.random()*8);   
        }               
        }

        // Create new organizer and calculator instances
        orga = new Organizer(randomArray [0], randomArray [1], randomArray [2]);
        calc = new Calculator();

        //Calculate
        calc.getRand1();
        calc.getRand2();
        calc.getRand3();
        calc.threeSame();
        calc.sequence();
        calc.calcSumOfValues();     
        calc.calcSumOfExtraValues();

    }//end Main Method  
}// end Class

2 个答案:

答案 0 :(得分:0)

一种可能的解决方案是只制作一个2D数组,然后一次传递一行来进行计算。

答案 1 :(得分:0)

要从Organizer导出数组,您只需编写一个访问器方法int [] getOrderedRandArray(),它创建一个由排序值rand1,rand2,rand3初始化的新数组实例。

&#13;
&#13;
public class Organizer {
     // ---------------------- ATTRIBUTES ---------------------
     protected int rand1;
     protected int rand2;
     protected int rand3;

     // --------------------- METHODS ---------------------
     // ... your code ...
     public int[] getOrderedRandArray(){
         int[] orderedRandArray = new int[]{rand1, rand2, rand3};
         return orderedRandArray;
     }
    
     // ... your code ...

}
&#13;
&#13;
&#13;