[英]How to organize and print the values of an array in java?
我创建了一个带有for循环的程序,该程序确定所创建的数组的数量。 我必须按升序组织每个arrey,所以我创建了另一个执行此操作的类( Organizer ), 但是我不知道如何导出每个数组(主类中的数组代码)
一旦整理好arrey,就需要计算并打印数组值的结果。
这是输出外观的示例。
如果arrey值相同,则总值加10
如果arrey值为顺数,则总值加20
输入回合:4
第一轮-> 1 3 7总价值:11
第二轮-> 1 4 5总价值:10
第三回合-> 1 1 1总价值:13
第4轮-> 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
一种可能的解决方案是仅制作1个2D数组,然后一次传递其一行以进行计算。
要从Organizer导出数组,只需编写访问器方法int [] getOrderedRandArray()即可创建一个新数组实例,该实例由排序值rand1,rand2,rand3初始化。
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 ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.