将方法随机分配到输入中 [英] Random assign methods into inputs

查看:56
本文介绍了将方法随机分配到输入中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于每个输入(名称、电话号码),我使用随机数生成器将输入分配给方法.我的目标是将所有可能的方法组合用于输入.

For each of the inputs (name, phoneno), I am using Random Number Generator to assign the inputs to a method. My goal is to get all possible combinations of methods to an input.

以下是我的代码.如果我有 1000 个输入和方法怎么办?有没有什么有效的方法来解决这个问题?

Below are my codes. What if I have 1000 of inputs and methods? Is there any efficient way to this this?

public static void main(String[] args) {

  for (int i = 0; i < 9; i++) {

    Random myRand = new Random();
    int randomInteger = myRand.nextInt(10);

    if (randomInteger == 0) {
      name = methodA();
      phoneno = methodA();
    } else if (randomInteger == 1) {
      name = methodA();
      phoneno = methodB();
    } else if (randomInteger == 2) {
      name = methodB();
      phoneno = methodB();
    } else if (randomInteger == 3) {
      name = methodB();
      phoneno = methodA();
    } else if (randomInteger == 4) {
      name = methodC();
      phoneno = methodC();
    } else if (randomInteger == 5) {
      name = methodC();
      phoneno = methodA();
    } else if (randomInteger == 6) {
      name = methodC();
      phoneno = methodB();
    } else if (randomInteger == 7) {
      name = methodA();
      phoneno = methodC();
    } else if (randomInteger == 8) {
      name = methodB();
      phoneno = methodC();
    }
  }
}

public static String methodA() {
  //do something
}

public static String methodB() {
  //do something
}

public static String methodC() {
  //do something
}

任何帮助将不胜感激.谢谢

Any help will be appreciated. Thank You

推荐答案

这里有一个解决方案...

Here is a solution...

import java.util.Random;

public class Main {

    public static final int FUNCTION_NUMBER = 3;

    public static String functionA() {
        return "A";
    }

    public static String functionB() {
        return "B";
    }

    public static String functionC() {
        return "C";
    }

    public static String callFunction(int function_index) {
        switch (function_index) {
            case 0: return functionA();
            case 1: return functionB();
            case 2: return functionC();
            default: throw new IllegalArgumentException("Incorrect value for function_index");
        }
    }

    public static void main(String[] args) {
        Random random = new Random();

        int n = random.nextInt((FUNCTION_NUMBER * FUNCTION_NUMBER) - FUNCTION_NUMBER);
        int name_function_index = n / FUNCTION_NUMBER;
        int phone_no_function_index = n % FUNCTION_NUMBER;

        System.out.print(callFunction(name_function_index));
        System.out.print(callFunction(phone_no_function_index));
        System.out.println("--------------");

    } 

}

如果你想添加其他函数,只需声明它,增加 FUNCTION_NUMBER 常量并在 callFunction 函数中添加一个案例.

If you want to add an other function, just declare it, increment the FUNCTION_NUMBER constant and add a case in the callFunction function.

所有的工作都在这里完成

All the job is done here

int n = random.nextInt(FUNCTION_NUMBER * FUNCTION_NUMBER);
int name_function_index = n / FUNCTION_NUMBER;
int phone_no_function_index = n % FUNCTION_NUMBER;

考虑到您的示例中有 3 个函数,我们有 9 种组合(AA、AB、AC、BA、BB、BC、CA、CB、CC).n 是包含在 0 到 8 之间的随机值.

Considering that you have 3 functions in your example, we have 9 combinations (AA, AB, AC, BA, BB, BC, CA, CB, CC). n is a random value between 0 and 8 included.

int n = random.nextInt(FUNCTION_NUMBER * FUNCTION_NUMBER);

组合的数量可以分成组.一组名称功能.为此,我们使用除法运算符.因此,对于值 0、1 和 2,组编号为 0(函数 A).对于值 3、4 和 5,组编号为 1(功能 B),对于值 6、7 和 8.

The number of combinations can be splitted in groups. One group for one name function. To do that we use the division operator. So, for the values 0, 1 and 2 the group number is 0 (functionA). For the values 3, 4 and 5 the group number is 1 (functionB) and for the value 6, 7 and 8.

int name_function_index = n / FUNCTION_NUMBER;

对于电话号码函数,就像我们为每个名称函数(如前面描述的每个组)迭代函数一样.为此,我们使用模运算符.

For the phone number function, it's like we iterate over the functions for each name function (for each group like described previously). To do that we use the modulo operator.

int phone_no_function_index = n % FUNCTION_NUMBER;

这是每个 n 值的函数索引表.可以看到它代表了所有的功能组合.

Here is a table of the functions indexes for each value of n. You can see that it represents all the functions combinations.

n name_function phoneno_function

n name_function phoneno_function

0 0 0
1 0 1
2 0 2
3 1 0
4 1 1
5 1 2
6 2 0
7 2 1
8 2 2

如果你想丢弃重复,你可以使用这个

If you want to discard the duplications, you can use this

int n = random.nextInt(FUNCTION_NUMBER * FUNCTION_NUMBER) - FUNCTION_NUMBER;
int name_function_index = n % FUNCTION_NUMBER;
int phone_no_function_index = (n + 1 + (n/4)) % FUNCTION_NUMBER;

这篇关于将方法随机分配到输入中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆