当循环在结束时进入continue语句时,它不会从顶部重新启动

时间:2017-04-27 01:20:54

标签: java loops while-loop do-while

好的,所以我创建了以下程序。它尚未完成但是当代码结束时(y或n部分)并且用户决定再次尝试时他们没有获得输入新赌注的选项它只使用从第一次输入的赌注。

(如果您需要有关理解代码的帮助或认为其他人可能难以理解,请发表评论)

import java.util.*;

public class Lab15 {
    static Scanner kin = new Scanner(System.in);

    public static void main(String[] args) {
        double money = 1000;
        System.out.println("Welcom To Craps You Start With 1000$!!!");
        int play = 1; //variable to cancel whole loop
        char yesNo; //if user wants to continue playing or not

        while(money > 0 && play == 1) {
            System.out.print("Please Enter The Amount You Want To Bet: ");
            double bet = kin.nextDouble();
            while((bet <= money || bet > 0)) {
                do { //loop to test if the bet is legit
                    if(money < bet || bet < 0) {
                        System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
                        bet = kin.nextDouble();
                    }
                } while(bet > money || bet < 0);

                money -= bet;
                System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");

                int die1 = (1 + (int) (6 * Math.random()));
                int die2 = (1 + (int) (3 * Math.random()));
                int sum = die1 + die2;
                System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);              

                System.out.print("Do You Want To Play Again(y) Or Not(n)");
                yesNo = kin.next().charAt(0);

                int loopBreak = 0; //to break do while loop bellow

                do {
                    if(yesNo == 'y') { //take above yes no to restart loop or not
                        System.out.println("Restarting...");
                        loopBreak += 1;
                        continue;
                    }
                    else if(yesNo == 'n') {
                        System.out.println("Thank You For Playing");
                        play -= 1; //to cancel whole program
                        break;
                    }
                    else {
                        System.out.println("Please Enter 'y' Or 'n'");
                    }
                } while(loopBreak == 0);

                if(play == 0) {
                    break;
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

所有关于嵌套的while循环都不会在需要时停止。就像这样

 while(money > 0 && play == 1) {
     System.out.print("Please Enter The Amount You Want To Bet: ");
     double bet = kin.nextDouble();
     while((bet <= money || bet > 0)) {

         ...

         do {
             if(yesNo == 'y') { //take above yes no to restart loop or not
                 System.out.println("Restarting...");
                 loopBreak += 1;
                 continue;
             }
             else if(yesNo == 'n') {
                 System.out.println("Thank You For Playing");
                 play -= 1; //to cancel whole program
                 break;
             }
             else {
                 System.out.println("Please Enter 'y' Or 'n'");
             }
         } while(loopBreak == 0);

         if(play == 0) {
             break;
         }
     }
 }

在你得到答案后'y'你不会打破嵌套而程序永远不会得到

 while(money > 0 && play == 1) {
 --> System.out.print("Please Enter The Amount You Want To Bet: ");
 --> double bet = kin.nextDouble();
     //because below while loop continues to loop
     while((bet <= money || bet > 0)) {

     ...

    }
}

一部分。无论如何纠正的代码在这里(我尝试了所有可能性并且它有效)

import java.util.*;

public class Lab15 {
    static Scanner kin = new Scanner(System.in);

    public static void main(String[] args) {
        double money = 1000;
        System.out.println("Welcom To Craps You Start With 1000$!!!");
        int play = 1; //variable to cancel whole loop
        char yesNo; //if user wants to continue playing or not

        while(money > 0 && play == 1) {
            System.out.print("Please Enter The Amount You Want To Bet: ");
            double bet = kin.nextDouble();
            while((bet <= money || bet > 0)) {
                do { //loop to test if the bet is legit
                    if(money < bet || bet < 0) {
                        System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
                        bet = kin.nextDouble();
                    }
                } while(bet > money || bet < 0);

                money -= bet;
                System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");

                int die1 = (1 + (int) (6 * Math.random()));
                int die2 = (1 + (int) (3 * Math.random()));
                int sum = die1 + die2;
                System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);              

                System.out.print("Do You Want To Play Again(y) Or Not(n): ");
                yesNo = kin.next().charAt(0);

                int loopBreak = 0; //to break do while loop bellow

                do {
                    if(yesNo == 'y') { //take above yes no to restart loop or not
                        System.out.println("Restarting...");
                        loopBreak++;
                        continue;
                    }
                    else if(yesNo == 'n') {
                        System.out.println("Thank You For Playing");
                        play--; //to cancel whole program
                        break;
                    }
                    else {
                        System.out.println("Please Enter 'y' Or 'n': ");
                    }
                } while(loopBreak == 0);

                if(play == 0 || loopBreak == 1) {
                    break;
                }
            }
        }
    }
}

祝你有个美好的一天!

答案 1 :(得分:0)

另一种解决方案是使用带标签的中断/继续https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

然而,它应该谨慎使用(或根本不使用)。你可能可以通过其他方式简化它。

package test.test;

import java.util.Scanner;

public class Lab15 {
    static Scanner kin = new Scanner(System.in);

    public static void main(String[] args) {
        double money = 1000;
        System.out.println("Welcom To Craps You Start With 1000$!!!");
        int play = 1; // variable to cancel whole loop
        char yesNo; // if user wants to continue playing or not

        //Labeled <-----------------
        start: while (money > 0 && play == 1) {
            System.out.print("Please Enter The Amount You Want To Bet: ");
            double bet = kin.nextDouble();
            while ((bet <= money || bet > 0)) {
                do { // loop to test if the bet is legit
                    if (money < bet || bet < 0) {
                        System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
                        bet = kin.nextDouble();
                    }
                } while (bet > money || bet < 0);

                money -= bet;
                System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");

                int die1 = (1 + (int) (6 * Math.random()));
                int die2 = (1 + (int) (3 * Math.random()));
                int sum = die1 + die2;
                System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);

                System.out.print("Do You Want To Play Again(y) Or Not(n)");
                yesNo = kin.next().charAt(0);

                int loopBreak = 0; // to break do while loop bellow

                do {
                    if (yesNo == 'y') { // take above yes no to restart loop or not
                        System.out.println("Restarting...");
                        loopBreak += 1;
                        //Labeled continue <-----------------
                        continue start; 
                    } else if (yesNo == 'n') {
                        System.out.println("Thank You For Playing");
                        play -= 1; // to cancel whole program
                        break;
                    } else {
                        System.out.println("Please Enter 'y' Or 'n'");
                    }
                } while (loopBreak == 0);

                if (play == 0) {
                    break;
                }
            }
        }
    }
}