【问题标题】:Increase of 3 variables增加3个变量
【发布时间】:2013-10-13 23:44:13
【问题描述】:

在我的程序中,我有三个变量:当其中一个变量达到 100 时,必须出现“变量首先到达终点线”的字样。

我如何组织第二个和第三个变量的到来,所以它们是这样出来的:

variable1-arrived first
variable2-finished second
variable3 finished third

救命!

public Corsa(String name)
{
    this.name = name;
    System.out.println("Start: " + name); 
    System.out.println("---------------");
}

public void run()
{
    while(finita == false)
    {
        try
        {
            avanza = (int) (Math.random()*20+1);
            percorso = percorso + avanza;
            System.out.println(name + " has path " + percorso + " meters");
            if(percorso < 100)
            {
                System.out.println("---------------");
                sleep = (int) (Math.random()*20+1);
                Thread.sleep(sleep);
            }
            else
            {
                System.out.println("---------------");
                System.out.println("---------------");
                System.out.println(name + " came in first"); 
                finita = true;
            }
        }
        catch(InterruptedException e){}
        Thread.yield();
    }
}

}

【问题讨论】:

  • int counter= 0 是你的朋友 :)
  • 请用英文编写代码,不懂您的语言(意大利语?西班牙语?)的人更容易理解
  • 结合 sᴜʀᴇsʜ ᴀᴛᴛᴀ 和 String[] position = {"first", "second", "third"}; 所说的话

标签: java variables random try-catch yield


【解决方案1】:

我还没有测试过这个(所以它甚至可能无法编译),但是类似下面的东西应该可以工作:

public class myRace
{
    private int distance = 100;
    private float offset = 20;
    public int runners[3];

    public void run()
    {
        // Set all runners to 0
        for ( int i = 0; i < runners.length; i++ )
            runners[i] = 0;

        // Run the race and stop when at least 1 runner has reached the distance...
        boolean finished = false;
        while ( !finished )
        {
            for ( int i = 0; i < runners.length; i++ )
            {
                runners[i] += (int)((Math.random() * offset) + 1);
                if ( runners[i] >= distance ) finished = true;
            }
        }
        // Race finished now sort the runners
        TreeMap<String, int> ranking = new TreeMap<String, int>();
        for ( int i = 0; i < runners.length; i++ )
        {
            // A TreeMap is sorted on its key, not the value!
            // The runners number is tagged on, just in case two runners have finished on the same distance.
            String sortedKey = Integer.toString(runners[i]) + "." + Integer.toString(i);
            ranking.put(sortedKey, i);
        }
        // Print the results
        int pos = 1;
        for ( Map.Entry entry : ranking.entrySet() )
        {
            String key = entry.getKey();
            String distance = key.subString(0, key.indexOf(".")); // chop off the "." + runners number.

            System.out.println("#" + pos +    // position
                "." + entry.getValue() +      // who
                ", Distance = " + distance);  // distance covered

            pos++; // this does take in account whether multiple runners finished on the same distance.
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多