在java中循环中断

时间:2014-09-18 06:27:50

标签: java

    import java.util.Scanner;
    import java.util.Random;
    public class CoinTossing 
     {
      static enum Status {HEADS,TAILS};
      static Status Coin;
      static int toss;
      public static void main(String []args)
       {
        Scanner input = new Scanner (System.in);
        Random ranDom = new Random();
        CoinTossing call = new CoinTossing ();
        int heads=0;
        int tails=0;
        int choice;
        int tossTimes=1;
        int loop=1;
        System.out.printf("Toss the Coin.\n1.YES\n2.NO\n");
        choice=input.nextInt();

        while(choice!=2)
         {
           System.out.printf("Toss Times :%d",tossTimes);
           ++tossTimes;
           call.flip(ranDom);
            if(Coin==Status.HEADS)
             {
               ++heads;
             }
            if(Coin==Status.TAILS)
             {
               ++tails;
             }

           System.out.printf("Heads :%d\n",heads);
           System.out.printf("Tails :%d\n",tails);

           System.out.printf("\nToss the Coin.\n1.YES\n2.NO");
           choice=input.nextInt();

           if(choice !=1 && choice!=2)
             {
            for(loop=1;loop<=3;loop++)//give 3 chance for input
             {
                System.out.printf("Wrong Input:%d\n",loop);
                System.out.printf("Please give input btween 1 and 2\n");
                choice=input.nextInt();

                if(choice ==1 | choice ==2)
                {
                    break;
                }
              }
             }

            if(loop==3)
             {
               System.out.printf("Due to wrong Input Program has terminated.\n");
               break;
             }
          }
        System.out.printf("\nTotal Tossed Times:%d\n",tossTimes-1);
        System.out.printf("Total Heads         :%d\n",heads);
        System.out.printf("Total Tails         :%d\n",tails);
       }

       Status flip(Random ranDom)
        {
         toss=ranDom.nextInt(2);
         System.out.printf("\nToss:%d\n",toss);
         if(toss==1)
        {
        Coin=Status.HEADS;
        //return Coin;
        }
       if(toss==0)
        {
        Coin=Status.TAILS;
        //return Coin;
        }
      return Coin;

      }
  }

这是一个投掷硬币的代码,它提示用户第一次输出任何东西2进行折腾。在第一次输入后,用户只有1和2输入。如果其他任何1和2输入,那么它将给用户另一次尝试。它会给3个机会。如果错误输入3次,程序将终止。但问题是它在3次错误输入后没有终止。它将输入选项。

5 个答案:

答案 0 :(得分:1)

错误是:

 for(loop=1;loop<=3;loop++)

应该是

 for(loop=0;loop<3;loop++)

因为,当您退出此循环时,loop等于4,因此以下条件不成立:

 if(loop==3)//you will never enter this if, as loop is 4

或者,您可以将条件更改为

 if(loop == 4)

答案 1 :(得分:0)

这个

if(loop==3)
             {
               System.out.printf("Due to wrong Input Program has terminated.\n");
               break;
             }

打破你的for循环。在for循环之前使用label然后使用break label;

答案 2 :(得分:0)

以下打破循环的条件需要包含在for循环中:

             for(loop=1;loop<=3;loop++)
             {
             ...

            if(loop==3)
             {
               System.out.printf("Due to wrong Input Program has terminated.\n");
               System.exit(0);
             }
             } //For loop end

另外,将break;更改为System.exit(0);

答案 3 :(得分:0)

 Its working now
 Java code---


   import java.util.Scanner;
   import java.util.Random;

  public class CoinTossing {

static enum Status {

    HEADS, TAILS
};
static Status Coin;
static int toss;
static int countMistakes = 0;

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Random ranDom = new Random();
    CoinTossing call = new CoinTossing ();
    int heads = 0;
    int tails = 0;
    int choice;
    int tossTimes = 1;
    int loop = 1;
    System.out.printf("Toss the Coin.\n1.YES\n2.NO\n");
    choice = input.nextInt();
    while (choice != 2) {


        if (choice != 1) {
            // Means wrong input
            countMistakes++;
            if(countMistakes <3){
            System.out.printf("Wrong Input:%d\n", loop);
            System.out.printf("Please give input btween 1 and 0\n");
            choice = input.nextInt();
            }else{
            break;
            }

        } else {
            countMistakes=0;
            System.out.printf("Toss Times :%d", tossTimes);
            ++tossTimes;
            call.flip(ranDom);
            if (Coin == Status.HEADS) {
                ++heads;
            }
            if (Coin == Status.TAILS) {
                ++tails;
            }

            System.out.printf("Heads :%d\n", heads);
            System.out.printf("Tails :%d\n", tails);

            System.out.printf("\nToss the Coin.\n1.YES\n2.NO");
            choice = input.nextInt();
        }

    }
    System.out.printf("\nTotal Tossed Times:%d\n", tossTimes - 1);
    System.out.printf("Total Heads         :%d\n", heads);
    System.out.printf("Total Tails         :%d\n", tails);
}

Status flip(Random ranDom) {
    toss = ranDom.nextInt(2);
    System.out.printf("\nToss:%d\n", toss);
    if (toss == 1) {
        Coin = Status.HEADS;
        //return Coin;
    }
    if (toss == 0) {
        Coin = Status.TAILS;
        //return Coin;
    }
    return Coin;

}

}

答案 4 :(得分:0)

将此附加到您的while循环

while(choice!=2 && loop != 4)

OR

if (loop == 3)更改为

 if (loop == 4)