Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的循环中断 import java.util.Scanner; 导入java.util.Random; 公营铸币 { 静态枚举状态{HEADS,TAILS}; 静态硬币; 静态int-toss; 公共静态void main(字符串[]args) { 扫描仪输入=新扫描仪(System.in); 随机=新随机(); cointosing调用=新cointosing(); int头=0; int-tails=0; 智力选择; int-tostimes=1; int循环=1; System.out.printf(“掷硬币。\n1.YES\n2.NO\n”); choice=input.nextInt(); while(选项!=2) { System.out.printf(“投掷次数:%d”,投掷次数); ++托斯泰姆斯; 调用。翻转(随机); 如果(硬币==状态头) { ++头部; } 如果(硬币==状态.TAILS) { ++尾巴; } System.out.printf(“头:%d\n”,头); System.out.printf(“尾部:%d\n”,尾部); System.out.printf(“\n穿过硬币。\n1.是\n2.否”); choice=input.nextInt(); if(选项!=1&&choice!=2) { 对于(loop=1;loop_Java - Fatal编程技术网

java中的循环中断 import java.util.Scanner; 导入java.util.Random; 公营铸币 { 静态枚举状态{HEADS,TAILS}; 静态硬币; 静态int-toss; 公共静态void main(字符串[]args) { 扫描仪输入=新扫描仪(System.in); 随机=新随机(); cointosing调用=新cointosing(); int头=0; int-tails=0; 智力选择; int-tostimes=1; int循环=1; System.out.printf(“掷硬币。\n1.YES\n2.NO\n”); choice=input.nextInt(); while(选项!=2) { System.out.printf(“投掷次数:%d”,投掷次数); ++托斯泰姆斯; 调用。翻转(随机); 如果(硬币==状态头) { ++头部; } 如果(硬币==状态.TAILS) { ++尾巴; } System.out.printf(“头:%d\n”,头); System.out.printf(“尾部:%d\n”,尾部); System.out.printf(“\n穿过硬币。\n1.是\n2.否”); choice=input.nextInt(); if(选项!=1&&choice!=2) { 对于(loop=1;loop

java中的循环中断 import java.util.Scanner; 导入java.util.Random; 公营铸币 { 静态枚举状态{HEADS,TAILS}; 静态硬币; 静态int-toss; 公共静态void main(字符串[]args) { 扫描仪输入=新扫描仪(System.in); 随机=新随机(); cointosing调用=新cointosing(); int头=0; int-tails=0; 智力选择; int-tostimes=1; int循环=1; System.out.printf(“掷硬币。\n1.YES\n2.NO\n”); choice=input.nextInt(); while(选项!=2) { System.out.printf(“投掷次数:%d”,投掷次数); ++托斯泰姆斯; 调用。翻转(随机); 如果(硬币==状态头) { ++头部; } 如果(硬币==状态.TAILS) { ++尾巴; } System.out.printf(“头:%d\n”,头); System.out.printf(“尾部:%d\n”,尾部); System.out.printf(“\n穿过硬币。\n1.是\n2.否”); choice=input.nextInt(); if(选项!=1&&choice!=2) { 对于(loop=1;loop,java,Java,断开for循环。在for循环之前使用标签,然后使用断开标签;以下断开循环的条件需要包含在for循环中: if(loop==3) { System.out.printf("Due to wrong Input Program has terminated.\n"); break; } for(loop=1;loop错误为: for(loop=1;loop<


断开for循环。在for循环之前使用
标签
,然后使用
断开标签;

以下断开循环的条件需要包含在for循环中:

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

             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
或者,您可以将条件更改为

 if(loop==3)//you will never enter this if, as loop is 4
现在开始工作了
Java代码---
导入java.util.Scanner;
导入java.util.Random;
公营铸币{
静态枚举状态{
正面、反面
};
静态硬币;
静态int-toss;
静态int counterrors=0;
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
随机=新随机();
cointosing调用=新cointosing();
int头=0;
int-tails=0;
智力选择;
int-tostimes=1;
int循环=1;
System.out.printf(“掷硬币。\n1.YES\n2.NO\n”);
choice=input.nextInt();
while(选项!=2){
如果(选项!=1){
//表示输入错误
统计错误++;

如果(counterrors将其附加到while循环中

 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;

}

如果(循环==3)将
更改为

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

有时,将代码拆分为多个具有描述性名称的方法或类是一个好主意。这将为您的算法提供更好、更抽象的视图,以便更容易发现问题。标签?Rly?它是什么?Pascal?不!标签不是一个好选项。事实上,标签根本不是一个选项。请找到更简单的解决方案!
System.exit()
?OP声明“如果输入错误3次,程序将终止”这将终止程序。for循环后有代码,可能他想打印一些东西
 if(loop==3)//you will never enter this if, as loop is 4
 if(loop == 4)
 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;

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