How do you start a make up line?

(java help)How do I make the output of this code start a new line?

  • I want every multiple of 20 to display on a new line...(2.,20) (22....,40) public class evenNums { public static void main(String[] args) { int ENUMBS = 0; final int ELIMIT = 100; while (ENUMBS < ELIMIT) { ENUMBS = ENUMBS + 2; System.out.println(ENUMBS);} } }

  • Answer:

    There are a couple of ways to do this...the easiest to understand is to just check, each time, whether or not to print a new line. This will just meaning keeping a count and checking whether or not this is the 21st, 31st, etc. line (i.e. count % 20 = 1 <-- presents a fencepost problem). So just keep a count (ENUMBS isn't a constant, so naming convention, seems to me would be eNumbs <-- although I'm still not sure what that stands for)... final int ELIMIT = 100; // <-- should make this static (probably private, but it doesn't really matter) int eNumbs = 2; int count = 0; // print the first number (handle fencepost) System.out.print(eNumbs); count++; while(eNumbs < ELIMIT){ count++; eNumbs += 2; if(count % 20 == 1){ // print a new line, then the number System.out.println(); }else{ //just print a space System.out.print(" "); } // now print the number, either following a newline or following a space on the same line System.out.print(eNumbs); } ...btw, also, this is really a for loop, you just start with eNumb = 2, then go until it reaches ELIMIT, increment by 2: // still handle fencepost, so start with i = 4 for(eNumbs = 4; eNumbs < ELIMIT + 2; eNumbs += 2){ ...same body as while loop above, except without eNumbs += 2; <-- for loop does this for us } Edit: So I don't confuse you about for loops I kept your naming convention (notice that we STILL need to declare int eNumbs = 2; above the initial print). You could also do it similar to what I did before: for(int eNumbs = 4; eNumbs < ELIMIT + 2; eNumbs += 2){... ...just make sure you don't declare int eNumbs above this (then there is a scope conflict). The reason I had to go until ELIMIT + 2 was because the while loop basically has the meaning that the LAST number you should print is EITHER: eNumbs = ELIMIT - 1 --> eNumbs += 2 = ELIMIT + 1 eNumbs = ELIMIT - 2 --> eNumbs += 2 = ELIMIT So the LAST number I should do is one of those two, Since I chose less than, add one to the largest value to get: ELIMIT + 2...think about it: If you add 2 to EITHER of those last values, it will be either ELIMIT + 2 or ELIMIT + 3 <-- both of which will cause the for loop to terminate (and not print anything else)...so this is why you have to go until ELIMIT + 2.

king_tys... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

add this to your loop if(ENUMBS%20 == 0) {System.out.println();} Also, it is a highly recommended practice to NOT use all caps for variable names. Generally all caps are reserved for static values (such as int PI = 3.14). Non-static variables should either be labeled as: thisIsAVariable OR this_is_a_variable

Nunya

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.