break 语句——如何完全停止程序直到满足要求

Break statement - how to completely stop the program until requirements met

我目前正在为uni做一个小组项目,我想知道如何引入一个break语句来完全停止程序直到满足要求?在这种情况下,initialBudget < (noOfBTC[i] * price[i]))。例如,100000 < 5BTC * 36270 的初始预算。我的代码中断了,但继续 运行 之后的代码。

static Scanner new_scan = new Scanner(System.in);

public static void main(String[] args) {
    System.out.println("What is your initial budget? ");
    double initialBudget = new_scan.nextDouble();
    double[] totalBudget = new double[8];

    totalBudget[0] = initialBudget;
    double[] uninvestedBudget = new double[8];
    uninvestedBudget[0] = initialBudget;
    double[] noOfBTC = new double[8];

    noOfBTC[0] = 0;
    double[] price = new double[8];
    price[0] = 36270;

    double dailyRateChange = ThreadLocalRandom.current().nextDouble(-0.06, 0.06);

   for (int i=0;i<20;i=i+1) {
        double r1=Math.random();
        //System.out.println(dailyRateChange);
    }

    for (int i=1;i<=7;i++) {
        price[i] = price[i-1] * (1 + dailyRateChange);
        //System.out.println("Price: " + price[i]);
    }
    System.out.println("How much BTC would you like to buy or sell? ");
    double BTC = new_scan.nextDouble();

    for (int i=0;i<=7;i++) {
        noOfBTC[i] = noOfBTC[i] + BTC;
        if (initialBudget < (noOfBTC[i] * price[i])) {
            System.out.println("Too many BTC");
            break;

        }else{}

        System.out.println("Number of BTC is " + noOfBTC[i]);
    }

    for (int i=0;i<=7;i++) {
        totalBudget[i] = uninvestedBudget[i] - (price[i] * noOfBTC[i] * dailyRateChange);
        System.out.println("Total budget: " + totalBudget[i]);
    }

break语句只会让你跳出你当前的循环,跳出那个循环后它会遇到你在下面写的下一个for循环并执行它。

如果您不想执行该循环,只需将其放入 if 语句中,这样它就可以仅在满足特定条件时执行。

boolean calcBudget = true;
for (int i=0;i<=7;i++) {
    noOfBTC[i] = noOfBTC[i] + BTC;
    if (initialBudget < (noOfBTC[i] * price[i])) {
        System.out.println("Too many BTC");
        calcBudget = false;
        break;
     }else{}

    System.out.println("Number of BTC is " + noOfBTC[i]);
}
if(calcBudget){
 for (int i=0;i<=7;i++) {
    totalBudget[i] = uninvestedBudget[i] - (price[i] * noOfBTC[i] * dailyRateChange);
    System.out.println("Total budget: " + totalBudget[i]);
 }
}

Java 中的 break 不会停止您的程序。相反,它“打破”了一个循环。一旦你到达那个中断,程序执行将立即转到行 for (int i=0;i<=7;i++) { 并开始 运行 第二个 for 循环。

我想你想要的是设置一个断点。断点允许程序员在特定语句处暂停执行以进行调试。如何设置断点取决于您使用的 IDE(或者,如果您没有使用 IDE,您可以使用 command-line 中的 JDB)。我建议在网上搜索“如何在 [您的 IDE 的名称] 中设置断点”。

我想你想要的是一个 while 循环。此循环将继续,直到满足条件。

一些伪代码。 (不确定你的逻辑)

boolean done = false;
while(!done) {
    System.out.println("How much BTC would you like to buy or sell? ");
    double BTC = new_scan.nextDouble();

    for (int i=0;i<=7;i++) {
        noOfBTC[i] = noOfBTC[i] + BTC;
        if (initialBudget < (noOfBTC[i] * price[i])) {
            System.out.println("Too many BTC");
            break;

        }else{
            System.out.println("Number of BTC is " + noOfBTC[i]);
            done = true;
        }

    }
}