简体   繁体   English

循环执行

[英]Looping a do/while

I am new to java programming, and I have only been doing it for a few days, so I'm a little stuck. 我是Java编程的新手,而且我只做了几天,所以有点卡住了。

What I would like to do is loop back to the "deposit" do-while if there is no money in the deposit, or, if there is money in the deposit, loop back to the "bet" do-while . 我想做的是,如果存款中没有钱,则循环回到“存款” do-while ,或者,如果存款中有钱,则循环回到“ bet” do-while

I have no idea how to do this, so I appreciate any help. 我不知道如何执行此操作,因此感谢您的帮助。

Please see the below code: 请参阅以下代码:

package LectureLoops;

import java.text.NumberFormat;
import java.util.Scanner;

public class ValidatingInput {

    static Scanner sc = new Scanner(System.in);
    static NumberFormat cf = NumberFormat.getCurrencyInstance();

    public static void main(String[] args) {

        int bank = 10000;
        int bet;
        int max = 9000;
        int deposit;
        cf = NumberFormat.getCurrencyInstance();


        do {
            System.out.print("Please make a deposit: ");
            deposit = sc.nextInt();
            if ((deposit > bank))
                System.out.println("Insufficient funds, please enter a valid amount");
        }// end of do
        while (deposit > bank);
        System.out.println("You have successfully made a deposit of " + cf.format(deposit));
        bank = bank - deposit;
        System.out.println("Your new bank balance is " + cf.format(bank));

        do {
            System.out.print("Enter your bet: ");
            bet = sc.nextInt();
            if ((bet > deposit))
                System.out.println("Insufficient funds please enter a valid amount or add more funds" + "\nYour current allowance is: " + cf.format(deposit));
            if ((bet > max) || (bet <= 0))
                System.out.println("Maximum bet is " + cf.format(max) + " you entered " + cf.format(bet) + "\nPlease enter a valid amount");
        }// end of do
        while ((bet <= 0) || (bet > max || bet > deposit));
        System.out.print("Thank you for your bet of " + cf.format(bet) + " Your money is good here!\n");
        deposit = deposit - bet;
        System.out.println("You have " + cf.format(deposit) + " left to spend");
    }// psvm end

}// validatinginput end

You can put your bet do-while loop in another do-while which checks if your deposit is exhausted or not and then put the whole logic in infinite while loop. 您可以将下注do-while循环放入另一个do-while ,以检查您的存款是否用尽,然后将整个逻辑放入无限while循环中。

You might want to think of a condition to break this infinite while loop later or else your app will keep on going forever. 您可能想考虑一个条件,以便在稍后循环时打破此无限循环,否则您的应用程序将永远运行下去。

Here is a quick code snippet: 这是一个快速的代码片段:

while(true) { /* Put whole logic inside this loop */

        /* Think of a condition to break this loop */

        do {
            System.out.print("Please make a deposit: ");
            deposit = sc.nextInt();
            if ((deposit > bank))
                System.out.println("Insufficient funds, please enter a valid amount");
        }// end of do
        while (deposit > bank);
        System.out.println("You have successfully made a deposit of " + cf.format(deposit));
        bank = bank - deposit;
        System.out.println("Your new bank balance is " + cf.format(bank));

    do { /* Add a do-while to check if deposit is exhausted or not */
        do {
            System.out.print("Enter your bet: ");
            bet = sc.nextInt();
            if ((bet > deposit))
                System.out.println("Insufficient funds please enter a valid amount or add more funds" + "\nYour current allowance is: " + cf.format(deposit));
            if ((bet > max) || (bet <= 0))
                System.out.println("Maximum bet is " + cf.format(max) + " you entered " + cf.format(bet) + "\nPlease enter a valid amount");
        }// end of do
        while ((bet <= 0) || (bet > max || bet > deposit));
        System.out.print("Thank you for your bet of " + cf.format(bet) + " Your money is good here!\n");
        deposit = deposit - bet;
        System.out.println("You have " + cf.format(deposit) + " left to spend");
    } while(deposit > 0);

}

I would suggest putting your main code body in a while(bank>0) loop and create separate methods to handle deposit and bet. 我建议将您的主要代码主体放在while(bank>0)循环中,并创建单独的方法来处理存款和下注。 Then you would have something like 那你会有类似

public static void main(String[] args) {
    int bank = 10000;
    int bet;
    int max = 9000;
    int deposit; cf = NumberFormat.getCurrencyInstance();
    while(bank>0){
        if(deposit>0){
            bet();
        }
        else {
            deposit();
        }
    }
}

public void deposit(){
    //put deposit code here
}

public void bet(){
    //put bet code here
}

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

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