Answer the question
In order to leave comments, you need to log in
How to add data to an object and then use it?
The question was originally
There is a class SomeMulti. It contains both data processing and output. You need to separate the output into another class. That is, transfer the processed data, for example, to the Draw class, and then add the necessary text for output to the same console.
public class Main {
public static void main(String[] args) {
SomeMulti someMulti = new SomeMulti();
Draw formatter = new Draw();
int input = 20;
someMulti.multiply(input);
}
}
public class SomeMulti {
public void multiply(int input) {
int result = input * 1;
System.out.println(1 + " action: " + result);
for (int i = 1; i < 10; i++) {
result = (result + 1) * i;
System.out.println(i + 1 + " action: " + result);
}
}
}
Answer the question
In order to leave comments, you need to log in
I messed around a little and stuffed everything into a sheet, this is what happened:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
SomeMulti someMulti = new SomeMulti();
Draw formatter = new Draw();
ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<Integer> numbers2 = new ArrayList<Integer>();
numbers = someMulti.multiply();
numbers2 = someMulti.multiply2();
System.out.println(formatter.drawHz(numbers, numbers2));
}
}
import java.util.ArrayList;
public class SomeMulti {
public ArrayList<Integer> multiply() {
ArrayList<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
numbers.add((2 + (i * 2)));
}
return numbers;
}
public ArrayList<Integer> multiply2() {
ArrayList<Integer> numbers2 = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
numbers2.add(i + 1);
}
return numbers2;
}
}
import java.util.ArrayList;
public class Draw {
public StringBuilder drawHz(ArrayList<Integer> numbers, ArrayList<Integer> numbers2) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numbers.size(); i++) {
sb.append("nubmer " + i + " is " + numbers.get(i)).append("\n");
sb.append("nubmer2 " + i + " is " + numbers2.get(i)).append("\n");
}
return sb;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question