Ausear
Ausear

Reputation: 1

How do I increment variables within an if statement to be used in another statement?

I'm having troubles figuring out how to increment a variable through an if statement, and allow it to be registered if called upon in a different if statement. I'm currently working on an assignment that mimics a menu for inputs. As shown, the user inputs a specific command, and on A, B, or C, they enter 3 scores/values. A separate input could be F, and on this input, it's supposed to show how many failures for each company, A, B, C has.

            System.out.print("\nPlease enter a command:"); //Starts command prompt
            String menuPrompt = scanner.next(); //Takes next input and uses one of the following if statements

            if (menuPrompt.equals("a")) {
                double aNum1;
                double aNum2; //initializes a score inputs
                double aNum3;

                System.out.print("\nEnter number 1:");
                aNum1 = scanner.nextDouble();

                System.out.print("\nEnter number 2:");
                aNum2 = scanner.nextDouble(); //asking for inputs of test values of company a

                System.out.print("\nEnter number 3:");
                aNum3 = scanner.nextDouble();

                if(aNum1 < -6.0 || aNum1 > 12.3) {
                    aFailure = aFailure + 1;
                }
                if(aNum2 < -6.0 || aNum2 > 12.3) {
                    aFailure = aFailure + 1; //checks to see if inputted value is a failure, if so, failure ticks up 1.
                }
                if(aNum3 < -6.0 || aNum3 > 12.3) {
                    aFailure = aFailure + 1;
                }

There are two other similar lines of code alike to this one just for "b" and "c". I initialized aFailure, bFailure, and cFailure at the top of the class and set their values to 0. After the if statements for the a, b, c prompts, I have this:

            } else if (menuPrompt.equals("f")) {

                System.out.print("\nAzuview Failures: " + aFailure);
                System.out.print("\nBublon Failures: " + bFailure); //Lists total failures for each company
                System.out.print("\nCryztal Failures: " + cFailure);

I'm not sure exactly how these variables are supposed to be defined, or if my formatting is even correct, help?

Upvotes: 0

Views: 662

Answers (2)

Anthony Dodier
Anthony Dodier

Reputation: 31

When you initialized a variable in a loop it is reinitialized every time the loop run again. When you initialized it outside of the loop it stays the same variable even in the loop. The concept behind a for loop is base on that. In this example, the count is going to be incremented in every update of the while loop So the count is going to be set to 0 and goes up by 1 until it reaches 9 when it will exit the loop

var count = 0;
while(count <10){
    count++;
}

but in this one every time is going to loop in it. It reinitialized your variable count to 0 and increment it to 1. So every loop a new count is created and set to 1

while(true){
  var count =0;
  count++;
}

My first response on stack hope it helps you understand the concept a little better :)

Upvotes: 1

Ausear
Ausear

Reputation: 1

I'm fairly sure I fixed this, instead of initializing within a while loop, I initialized it outside of the while loop and it seemed to fix this, any input on explaining the concepts behind this would be awesome!

Upvotes: 0

Related Questions