And a correct loop:
Getting yourself into a pattern that helps you remember all the parts...
// comment about what the loop is supposed to be doing
initialize
while (condition){
----------------;
-------------------;
--------------;
---------------;
-----------;
---------------;
update
}// NOTE: default size of a loop body is ONE STATEMENT
// but this one has several statements because of {}'s
| Examples |
|---|
// count 0 to 9 on a single line with spaces between digits
System.out.printf("\n");
int count = 0;
while ( count < 10 ) {
System.out.printf( "%d ",count);
count ++;
}
System.out.printf("\n");
|
Scanner inputScanner = new Scanner(System.in);
// keep asking until user provides a negative integer
int x = 999;
while ( x >= 0 ) {
System.out.printf("\nPlease enter a negative integer: ");
x = inputScanner.nextInt();
}
|
(3 pts each) The following (a thru e) are intended as written work but there is NO reason to get them wrong. Check your answers on the computer when you think you have the correct answer (good practice for exams!). If you have gotten some wrong, be sure you understand why then write up the CORRECT answer to submit as homework. I'm happy to receive all the right stuff, (-: But as hand-written, not not not not code. And do not submit typed work either. Write out every loop by legible beautiful hand.
f) Count from 2 to 18 by 2's
A good answer would be:![]()
A bad answer would be....
2 6 8 10 12 14 16 18
For example, if the user enters
java YourClientName 6 3
the printed output should be
*-*-*-
*-*-*-
*-*-*-
java YourOtherClientName 4 8
the printed output should be
*-*-*-*-
-*-*-*-*
*-*-*-*-
-*-*-*-*