文本文件中随机调用不同命令的百分比 [英] Percentage of Random Calls to Different Command in Text File

查看:52
本文介绍了文本文件中随机调用不同命令的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的配置文件(Test.txt)

This is my Config File(Test.txt)

CommandA   75%
CommandB   15%
CommandC   10%

我编写了一个多线程程序,在该程序中我正在逐行读取文件,但不知道我应该如何解决上述问题,其中这么多百分比(75%)的随机调用转到 CommandA,而这么多百分比(15%) 的随机调用转到 CommandB,与 CommandC 相同.

I wrote a multithreading program in which I am reading the files line by line but not sure how should I do the above question in which this much percentage(75%) of random calls go to CommandA, and this much percentage(15%) of random calls go to CommandB and same with CommandC.

public static void main(String[] args) {

            for (int i = 1; i <= threadSize; i++) {
                new Thread(new ThreadTask(i)).start();
            }
        }

class ThreadTask implements Runnable {

        public synchronized void run() {
            BufferedReader br = null;

            try {
                String line;

                br = new BufferedReader(new FileReader("C:\\Test.txt"));

                while ((line = br.readLine()) != null) {
                    String[] s = line.split("\\s+");
                    for (String split : s) {
                    System.out.println(split);
                }
            }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

        }
    }

推荐答案

获取一个随机数 1-100.如果数字是1-75做命令A,76-90做命令B,91-100做命令C.

Get a random number 1-100. If the number is 1-75 do command A, 76-90 do command B, 91-100 do command C.

编辑评论:

我会考虑两种方法来做到这一点.如果你只有三个命令(A、B、C)那么你可以做一个简单的:

There are two ways I would consider doing this. If you only have the three commands (A, B, C) then you can do a simple:

    int[] commandPercentages = {75, 15, 10};        
    int randomNumber = 90;

    if((randomNumber -= commandPercentages[0]) < 0) {
        //Execute Command A
    }
    else if((randomNumber -= commandPercentages[1]) < 0) {
        //Execute Command B
    }
    else {
        //Execute Command C
    }

如果你有很多复杂的命令,你可以像这样设置命令:

if you have a lot of complicated commands you can set up the commands like so:

private abstract class Command {
    int m_percentage;       
    Command(int percentage) {
        m_percentage = percentage;
    }       
    int getPercentage() {
        return m_percentage;
    }
    abstract void executeCommand();
};

private class CommandA extends Command {        
    CommandA(int percentage) {
        super(percentage);
    }
    @Override
    public void executeCommand() {
        //Execute Command A
    }       
}

private class CommandB extends Command {        
    CommandB(int percentage) {
        super(percentage);
    }
    @Override
    public void executeCommand() {
        //Execute Command B
    }

}

然后像这样选择命令:

    Command[] commands = null;  
    int randomNumber = 90;

    commands[0] = new CommandA(75);
    commands[1] = new CommandB(25);

    for(Command c: commands) {
        randomNumber -= c.getPercentage();
        if(randomNumber < 0) {
            c.executeCommand();
        }
    }

这篇关于文本文件中随机调用不同命令的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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