[英]How can I loop all methods and objects in a class? (java)
I need to loop every method inside my class RandomMath
我需要循环我类
RandomMath
每个方法
I'm trying to implement an algorithm which: 我正在尝试实现一种算法:
I've already done steps 1-4 as well as implement a statement which makes the process replace the generated number with new4Digit after the first run of the process. 我已经完成了步骤1-4,并实现了一条语句,该语句使该过程在第一次运行后用new4Digit替换生成的数字。 Now I need to essentially loop the entire class and it will be done.
现在,我基本上需要遍历整个类,并且它将完成。 Anyone know how?
有人知道吗?
my code: 我的代码:
import java.util.Random;
public class RandomMath {
private static int generated = generator();
//first set of values for the bigger, smaller and new 4 digit numbers
final private static int bigger = bigger();
final private static int smaller = smaller();
final private static int new4Digit = subtraction();
public static void main(String[] args) {
System.out.println(bigger);
System.out.println(smaller);
System.out.println(new4Digit);
}
//create method for generating random numbers
public static int generator(){
int n = 0;
//new4Digit is initialised to zero. So after the first loop instead...
//...of generating a new number the value of n is taken as new4Digit
if(new4Digit != 0){
n = new4Digit;
}
else{
Random randomGen = new Random();
//set max int to 10000 as generator works between 0 and n-1
for(int i=0; i<1; i++){
n = randomGen.nextInt(10000);
if((n==1111 || n==2222 || n==3333 || n ==4444 || n==5555)
||(n==6666 || n==7777 || n==8888 || n==9999 || n==0000)){
i--;
}
}
}
return n;
}
//method for denoting the bigger number
public static int bigger(){
int[] times = new int[10];
while (generated != 0) {
int digit = generated % 10;
times[digit]++;
generated /= 10;
}
int big = 0;
for (int i = 9; i >= 0; i--) {
for (int j = 0; j < times[i]; j++) {
big = big * 10 + i;
}
}
//code below accounts for ints with leading zeroes
//if the numer has three leading zeroes (<10) times by 1000
if (big < 10){
big = big * 1000;
}
//if the number has two leading zeroes (<100) times by 100
else if(big < 100){
big = big * 100;
}
//if the number has one leading zero (<1000) times by 10
else if(big < 1000){
big = big * 10;
}
return big;
}
//denoting the smaller number by simply reversing the bigger
public static int smaller(){
int d1 = bigger %10;
int r1 = bigger /10;
int d2 = r1 % 10;
int r2 = r1 / 10;
int d3 = r2 % 10;
int r3 = r2 / 10;
int d4 = r3 % 10;
int small = d1 * 1000 + d2 * 100 + d3 * 10 + d4;
return small;
}
//method for subtracting the smaller number from the bigger
public static int subtraction(){
int newNumber = bigger - smaller;
return newNumber;
}
}
Don't use static methods. 不要使用静态方法。 Create an object/class with constructor, methods, parameters and fields and call methods you need several times on a instance of this class
使用构造函数,方法,参数和字段创建对象/类,并在该类的实例上多次调用所需的方法
If I understand your question, you should use something this (instead of static fields use parameters): 如果我理解您的问题,则应使用以下内容(而不是使用参数的静态字段):
import java.util.Random;
public class RandomMath {
public static void main(String[] args) {
int new4Digit = loop(10);
System.out.println(new4Digit);
}
public static int loop(int times){
int new4Digit = 0;
for(int i=0; i< times; i++) {
new4Digit = generator(new4Digit);
new4Digit = subtraction(new4Digit);
}
return new4Digit;
}
//create method for generating random numbers
public static int generator(int new4Digit){
...
return n;
}
//method for denoting the bigger number
public static int bigger(int generated){
...
return big;
}
//denoting the smaller number by simply reversing the bigger
public static int smaller(int bigger){
...
return small;
}
//method for subtracting the smaller number from the bigger
public static int subtraction(int generated){
int bigger = bigger(generated);
int newNumber = bigger - smaller(bigger);
return newNumber;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.