0

I don't want my method writing for me, I'm new to Java, if somebody could just point out where I'm getting confused please? I need to write a method that "charges" the "powerLevel" integer from 0-15(maxPowerLevel) then stop. so far I've done:

public void rechargePower()  {
    int powerLevel = 0; 
    for (int i = 1; i <= maxPowerLevel; i++); {
        powerLevel += 1;
        while(powerLevel <= maxPowerLevel) {
            System.out.println ("Charging");
        }
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Spum0n1
  • 11
  • 1
  • 3
  • The semicolon near the end of this line: `for (int i = 1; i <= maxPowerLevel; i++);{` is short-circuiting your for loop completely. It is in effect doing this: `for (int i = 1; i <= maxPowerLevel; i++) { }` – Hovercraft Full Of Eels Feb 22 '20 at 19:34
  • Also, for your own benefit and ours, learn to format your code to meet coding standards (your book and your instructor will tell you what these are). Doing this makes it easier for you and for us to find bugs in your code. – Hovercraft Full Of Eels Feb 22 '20 at 19:35
  • I've taken the effort to format your code for you this time, but again in the future, you will want to be doing this yourself. No sense in making your code harder to read than it has to be. – Hovercraft Full Of Eels Feb 22 '20 at 19:37
  • That inner while loop should also be an if-block, else it will loop *forever* since you do not change the loop condition *within* the loop. – Hovercraft Full Of Eels Feb 22 '20 at 19:37
  • Thanks for your responses, I missed that semi-colon. I'm having an issue where its just constantly printing "Charging" rather than upping the charge number 1 at a time. – Spum0n1 Feb 22 '20 at 19:38
  • See my comment above. Also, please check out [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). It won't solve your direct problem, but it will give you steps that you can follow that should help you solve it yourself, or even if that is not successful, then at least help you to better isolate your problem so that your question can be more focused and easier to answer. – Hovercraft Full Of Eels Feb 22 '20 at 19:38
  • Ah thankyou! That makes sense! – Spum0n1 Feb 22 '20 at 19:39
  • I would suggest getting rid of one of the loops - no reason to have two of them in that case – user85421 Feb 22 '20 at 20:05

0 Answers0