简体   繁体   English

如何在一个类中循环所有方法和对象? (Java)的

[英]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: 我正在尝试实现一种算法:

  1. generates a 4 digit number. 生成一个4位数字。
  2. rearranges the digits into the largest arrangements creating a new value called bigger. 将数字重新排列为最大的排列,从而创建一个称为“更大”的新值。
  3. does the same but into the smallest arrangement creating a new value called smaller. 这样做是一样的,但是在最小的排列中创建了一个称为较小的新值。
  4. subtracts smaller from bigger to make a new 4 digit number called new4Digit 从较大的值减去较小的值,得到一个新的4位数字,称为new4Digit
  5. loop the process from step 2 ten times using new4Digit obtained above instead of generating a new number. 使用上面获得的new4Digit将步骤2的过程循环十次,而不是生成一个新的数字。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM