简体   繁体   English

如何从JAVA中的for循环的最后一次迭代中获取字符串?

[英]How to get a string from the last iteration from for-loop in JAVA?

How can i get a string from the last iteration in this loop? 我如何从此循环的上一次迭代中获取字符串?

    public void Flip()
    {
        for (int i = 1; i <= numberOfFlips; i++ )
        {
            System.out.println("Flip " + i + ": " + headOrTail);
            i++;
            System.out.println("Flip " + i + ": " + headOrTail2);
        }

    }

Output for numberOfFlips = 2 is: numberOfFlips = 2的输出是:

Flip 1: Head 翻转1:头

Flip 2: Tail 翻转2:尾巴

and so on. 等等。

My purpose is to get "headOrTail" from the last iteration result and be able to do something with it. 我的目的是从上次迭代结果中获取“ headOrTail”,并能够对其进行处理。 Any hints or solutions how to reach it? 任何提示或解决方案如何达到目的?

Just ask if it is the las loop. 只是问问它是否是las循环。

public void Flip()
    {
        for (int i = 1; i <= numberOfFlips; i++ )
        {
            System.out.println("Flip " + i + ": " + headOrTail);
            i++;
            System.out.println("Flip " + i + ": " + headOrTail2);
            if(i==numberOfFlips){
                <do what you want here>
            }
        }

    }

Your current code is already able to get the result for the last flip. 您当前的代码已经能够获取上一次翻转的结果。 Since you declared variable headOrTail outside the scope of the loop, you just have to update every flip and you can print it after the loop. 由于您在循环范围之外声明了变量headOrTail ,因此只需更新每个翻转,就可以在循环之后进行打印。

for (int i = 1; i <= numberOfFlips; i++ )
{
    headOrTail = newFlip();    //update your new flip
    System.out.println("Flip " + i + ": " + headOrTail);
}

System.out.println("My last flip: " + headOrTail );

Note: Your current code will always print the same flip results since you are not updating headOrTail . 注意:由于您没有更新headOrTail当前代码将始终输出相同的翻转结果。

how to propely create newFlip() method to use it your way? 如何适当地创建newFlip()方法以您的方式使用它?

Depending on the data type of your "flip". 取决于“翻转”的数据类型。 If you use integer to represent a flip (for example, 0 for head, 1 for tail), create a method to return a random number of 0-1. 如果使用整数表示翻转(例如,0表示头部,1表示尾部),则创建一种方法以返回0-1的随机数。

//One of the possibilities..
public static int newFlip(){
    Random rnd = new Random();
    return rnd.nextInt(2);    //you can use Math.random() as well
}

If everything is contained within a class, you can do it as: 如果所有内容都包含在一个类中,则可以按以下方式进行操作:

//Example..
class CoinTosser
{
    private static Random rnd = new Random();
    private int currentFlip;  //this is also your last flip

    public void newFlip(){
        currentFlip = rnd.nextInt(2);  //in this case, no need to return toss result
    }

    public void flipCoin(int times){
        for(int i=0, i<times; i++){
            newFlip();
            System.out.println("Attemp "+ (i+1) + ":" + currentFlip==0?"Head":"Tail");
        }
    }
}

Initialize a String , or a boolean , before the loop, and then overwrite it each loop and the last result will be kept in it. 在循环之前初始化Stringboolean ,然后在每个循环中覆盖它,最后的结果将保留在其中。 Or just save it with a if statement checking if it's the last loop as someone suggested. 或者只是使用if语句保存它,如某人所建议的那样检查它是否是最后一个循环。

Is this what you are looking for? 这是你想要的?

public void Flip()
{
    String lastInteraction = null;
    for (int i = 1; i <= numberOfFlips; i++ )
    {
        System.out.println("Flip " + i + ": " + (lastInteraction = ((i % 2) == 0 ? "Head" : "Tail"));
        if(lastInteraction.equals("Head")) {
            System.out.println("Last flip was head");
        } else {
            System.out.println("Last flip was tail");
        }
    }

}

This will store & print the last result; 这将存储并打印最后的结果; either Head or Tail 头或尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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