• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

java mid-term tomorrow! weee!!

quietRiot

[H]ard|Gawd
Joined
Jul 3, 2003
Messages
1,109
Midterm tomorrow, please check my code here. I am using quizzes to study, so check my Q and A.

Q:Write a WHILE loop to print out a count from 0 to 100 by increments of two with single number number on each line. The first line should output 0, then 2 and so on until 100 is output. You should declare any variables necessary, but you do not need to place the code in a method or class. The code will receive 2 points for the correct loop type, 2 points for the correct starting position, 2 points for the correct ending position, 2 points for proper incrementation, and 2 points for the proper output statement.

A:

int x=0;
While(x<=100){
x=x+2;
System.out.println(x);
}

Q:Write a FOR loop to print out a countdown from 100 to 0 with a single number on each line. You should declare any variables necessary, but you do not need to place the code in a method or class. The code will receive 2 points for the correct loop type, 2 points for the correct starting position, 2 points for the correct ending position, 2 points for proper decrementation, and 2 points for the proper output statement.

A:

for(int x=100; x>=0; x=x-1)
System.out.println(x);


thanky. (cake, right? gotta start somewhere.)
 
It looks good to me, except when declaring x in the for loop you accidentally have a subtract instead of equals. I wish the CS classes I'm in now were still this easy.
 
i'll trade you my take home mid-term essay thingy for my literature class for this....

there's a problem in your first solution regarding when you increment x. it will print out 2, 4, 6, 8, etc. but it's supposed to print 0, 2, 4, 6, etc.
 
for the first one, I'd probably be a jerk:

Code:
int x = -2;
while ((x += 2) <= 100)
   System.out.println(x);

or

Code:
int x = 0;
do
   System.out.println(x);
while ((x += 2) <= 100)

although this is a do loop

or

Code:
int x = 0;
while (x <= 100)
   System.out.println((x += 2));
 
Minishark said:
It looks good to me, except when declaring x in the for loop you accidentally have a subtract instead of equals. I wish the CS classes I'm in now were still this easy.


gotcha, yea, i see that. haha, yea, i knew this would be cake.
 
tim_m said:
i'll trade you my take home mid-term essay thingy for my literature class for this....

there's a problem in your first solution regarding when you increment x. it will print out 2, 4, 6, 8, etc. but it's supposed to print 0, 2, 4, 6, etc.


i should then start with x=-2 i guess, then. thanks a lot.

ahh i see, because it will run the loop first before the first printout, so it will increment before it prints the first x.
 
For the while loop you just need to move the println statement to be above the x+=2 line.

Code:
int x = 0
while(x <= 100){
   System.out.println(x);
   x = x + 2;
}
 
Back
Top