Home > front end >  Break statement - how to completely stop the program until requirements met
Break statement - how to completely stop the program until requirements met

Time:02-15

I am currently working on a group project for uni and I was wondering how to introduce a break statement that completely stops the program until the requirements are met? In this case, initialBudget < (noOfBTC[i] * price[i])). E.g., an initial budget of 100000 < 5BTC * 36270. The code I have breaks the, but continues running the code after.

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

CodePudding user response:

A break in Java doesn't stop your program. Instead, it "breaks" out of a loop. Once you get to that break, program execution will go immediately to the line for (int i=0;i<=7;i ) { and start running the second for loop.

I think what you are wanting is to set a breakpoint. A breakpoint allows a programmer to pause execution at a particular statement for debugging purposes. How you set a breakpoint depends on the IDE you are using (or, if you are not using an IDE, you could use JDB from the command-line). I recommend searching the web for "How to set a breakpoint in [the name of your IDE]."

CodePudding user response:

The break statement will only get you out of your current loop, after coming out of that loop it will encounter the next for loop which you have written below and will execute it.

If you don't want that loop to execute then simply put it in if statement and that will enable it to execute only if certain condition are met.

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

CodePudding user response:

I think what you are wanting is a while loop. This loop will continue until conditions are met.

Some psuedocode. (not sure of your logic)

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

    }
}
  • Related