我hava用for循环创建了一个程序,它确定了创建的数组的数量。我必须组织升序排列每个阿雷,所以我创建了另一类做到这一点(主办),但我不知道如何导出的每个阵列(阵列码主类)如何组织和打印java中数组的值?
一旦我orginised我需要计算并打印数组值的结果。
下面是输出结果的例子。
如果阿雷值相同的合计值增加了10
如果阿雷值是SQUENCE总价值增加了20
输入轮:1 4
轮 - > 1 3 7总计值:11
回合2 - > 1 4 5合计值:10
回合3 - > 11 1个合计值:13
回合4 - > 1 2 3总价值:26
PS我新在java.I认为我靠近到该溶液中,但我不能弄清楚如何克服这个问题
管理类
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
你可以给我一个例子 –
你可以声明像 –
INT 2D阵列ARR [] [] =新INT [两轮] [3] –