Java 循环一个do/while

Java 循环一个do/while,java,while-loop,Java,While Loop,我是java编程新手,而且我只做了几天,所以我有点被卡住了 我想做的是循环回“存款”do while如果存款中没有钱,或者,如果存款中有钱,循环回“bet”do while 我不知道该怎么做,所以我感谢你的帮助 请参阅以下代码: package LectureLoops; import java.text.NumberFormat; import java.util.Scanner; public class ValidatingInput { static Scanner sc

我是java编程新手,而且我只做了几天,所以我有点被卡住了

我想做的是循环回“存款”
do while
如果存款中没有钱,或者,如果存款中有钱,循环回“bet”
do while

我不知道该怎么做,所以我感谢你的帮助

请参阅以下代码:

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
包操作;
导入java.text.NumberFormat;
导入java.util.Scanner;
公共类验证输入{
静态扫描仪sc=新扫描仪(System.in);
静态NumberFormat cf=NumberFormat.getCurrencyInstance();
公共静态void main(字符串[]args){
国际银行=10000;
int-bet;
int max=9000;
内部存款;
cf=NumberFormat.getCurrencyInstance();
做{
系统输出打印(“请存款:”);
存款=sc.nextInt();
如果((存款>银行))
System.out.println(“资金不足,请输入有效金额”);
}//完事
同时(存款>银行);
System.out.println(“您已成功存款”+cf.format(存款));
银行=银行-存款;
System.out.println(“您的新银行余额为”+cf.format(bank));
做{
系统输出打印(“输入您的赌注:”;
bet=sc.nextInt();
如果((下注>押金))
System.out.println(“资金不足,请输入有效金额或添加更多资金”+”\n您当前的津贴为:“+cf.format(存款));
如果((下注>最大值)| |(下注押金));
System.out.print(“感谢您下注”+cf.format(bet)+“您的钱在这里很好!\n”);
存款=存款-押注;
System.out.println(“您有”+cf.format(存款)+“剩余支出”);
}//psvm端
}//验证输入端

您可以将您的赌注
do while
循环放入另一个
do while
循环,该循环检查您的存款是否用完,然后将整个逻辑放入无限
while
循环

你可能想考虑一个条件来打破这个无限的while循环,否则你的应用程序将永远运行下去

下面是一个快速代码片段:

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);

}
while(true){/*将整个逻辑放在这个循环中*/
/*想一个条件来打破这个循环*/
做{
系统输出打印(“请存款:”);
存款=sc.nextInt();
如果((存款>银行))
System.out.println(“资金不足,请输入有效金额”);
}//完事
同时(存款>银行);
System.out.println(“您已成功存款”+cf.format(存款));
银行=银行-存款;
System.out.println(“您的新银行余额为”+cf.format(bank));
do{/*添加do while以检查存款是否用完*/
做{
系统输出打印(“输入您的赌注:”;
bet=sc.nextInt();
如果((下注>押金))
System.out.println(“资金不足,请输入有效金额或添加更多资金”+”\n您当前的津贴为:“+cf.format(存款));
如果((下注>最大值)| |(下注押金));
System.out.print(“感谢您下注”+cf.format(bet)+“您的钱在这里很好!\n”);
存款=存款-押注;
System.out.println(“您有”+cf.format(存款)+“剩余支出”);
}而(存款>0);
}

您可以将您的赌注
do while
循环放入另一个
do while
循环,该循环检查您的存款是否用完,然后将整个逻辑放入无限
while
循环

你可能想考虑一个条件来打破这个无限的while循环,否则你的应用程序将永远运行下去

下面是一个快速代码片段:

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);

}
while(true){/*将整个逻辑放在这个循环中*/
/*想一个条件来打破这个循环*/
做{
系统输出打印(“请存款:”);
存款=sc.nextInt();
如果((存款>银行))
System.out.println(“资金不足,请输入有效金额”);
}//完事
同时(存款>银行);
System.out.println(“您已成功存款”+cf.format(存款));
银行=银行-存款;
System.out.println(“您的新银行余额为”+cf.format(bank));
do{/*添加do while以检查存款是否用完*/
做{
系统输出打印(“输入您的赌注:”;
bet=sc.nextInt();
如果((下注>押金))
System.out.println(“资金不足,请输入有效金额或添加更多资金”+”\n您当前的津贴为:“+cf.format(存款));
如果((下注>最大值)| |(下注押金));
System.out.print(“感谢您下注”+cf.format(bet)+“您的钱在这里很好!\n”);
存款=存款-押注;
System.out.println(“您有”+cf.format(存款)+“剩余支出”);
}而(存款>0);
}

我建议将您的主要代码主体放在
while(bank>0)
循环中,并创建单独的方法来处理存款和下注。然后您可以

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
}

我建议将您的主代码主体放入
while(bank>0)
循环中,并创建单独的方法来处理存款和下注

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
}

非常感谢您的帮助,现在非常简单,我知道应该怎么做了,我已经与下面的内容创建了一个中断。}while(存款>0);if((银行==0)和&(存款==0)){break;非常感谢你的帮助,现在我知道应该怎么做了,非常简单,我已经与下面的人建立了一个中断。}而(存款>0);如果((银行==0)和(&(存款==0)){break;非常感谢你