Count 100 - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19) +---- Thread: Count 100 (/showthread.php?tid=721) |
Count 100 - WitherSlayer - 01-03-2013 This is more of an explanatory program than a practical. It will count to 100 and show you when it it finished. [code2=java]import java.util.Scanner; public class New { static Scanner input = new Scanner(System.in); public static void main(String[]args){ countNumber();} public static void countNumber(){ int i = 0; while (i <= 100){//The while statement says that this will repeat 100 times System.out.println(i); i++; if(i == 101){ System.out.println("I now equals 100"); } } } }[/code2] p.s. Sorry that it does not tell you exactly what is doing Re: Count 100 - brandonio21 - 01-03-2013 Good job, but Code: if(i == 101){ Doesn't really seem like the best idea, because i != 100 at that specific time. Why don't you have it setup to check if i = 100? Also, I know these are probably remnants of an older idea or something, but the scanner that you created is never actually used, so it can definitely be removed! Here's a cleaned up version: [code2=java]public class New { public static void main(String[]args){ countNumber();} public static void countNumber(){ int i = 0; System.out.println(i); while (i < 100){//The while statement says that this will repeat 100 times i++; System.out.println(i); } if(i == 100){ System.out.println("I now equals 100"); } } }[/code2] Re: Count 100 - WitherSlayer - 01-03-2013 Sorry, I'm still a novice. And now when i look at it i see the mistakes i made. But i would have to = 101 because if it's 100 the it writes "I now equals 100" before it says 100. Thanks <!-- s:o --><img src="{SMILIES_PATH}/icon_e_surprised.gif" alt=":o" title="Surprised" /><!-- s:o --> |