Help! Simple Java Loop..

MasterSamwise

Senior member
Jan 12, 2003
219
0
0
import cs1.Keyboard;

public class ch3_4
{

public static void main (String[] args)
{

int value;

System.out.print ("Enter a value to begin the loop: ");
value = Keyboard.readInt ();

if (value <2)
System.out.print ("Illegal value, Enter another: ");
value= Keyboard.readInt ();


for (int count = 2; count != value; count *= 2)
System.out.println (count);



}

}

I need to write an app that takes the input and adds all the even numbers between the 2 and the input value.

The code i have written so far is intended to print the even numbers but not to add them yet. This never prints any output! Any suggestions?

MS
 

MasterSamwise

Senior member
Jan 12, 2003
219
0
0
Code:
import cs1.Keyboard;

public class ch3_4
{

public static void main (String[] args)
{

	int value, count = 0;

	System.out.print ("Enter a value to begin the loop: ");
	value = Keyboard.readInt ();

	if (value <2)
		System.out.print ("Illegal value, Enter another: ");
		value= Keyboard.readInt ();


			for (count = 2; count != value; count += 2)
				System.out.println (count);
				

		System.out.println (value);



	}

}

The loop still wont end? Anyone see my mistake? I also need the operation for adding the values of count. Not to good at this loop stuff..

MS
 

Haircut

Platinum Member
Apr 23, 2000
2,248
0
0
if (value <2)
System.out.print ("Illegal value, Enter another: ");
value= Keyboard.readInt ();


This bit of code always executes the third line, without braces only the first line after the if statement is included as part of the if.

Also, you say you want all even numbers, yet using count *= 2 in the for loop will give you powers of 2.
Using count != value will also cause the loop to never terminate unless you happen to pick an exact power of 2.
 

Aso

Senior member
Aug 16, 2000
381
0
76
This should get you close to what you want to do.
If you aren't using a Java IDE, I would recommend using eclipse for writing java code.


import cs1.Keyboard;

public class ch3_4
{
public static void main(String[] args)
{
int value, count = 0;
System.out.print("Enter a value to begin the loop: ");
//read the user input
value = Keyboard.readInt();

//check to make sure they entered a value greater than 2 so the loop will execute
if (value < 2)
{
System.out.print("Illegal value, Enter another: ");
}
// the inputted value was greater than 2 so calculate the sum of all the even numbers
else
{
//create a integer to hold the sum of all evens
int sum = 0;
//loop from 0 to the inputed value
for (count=0; count <= value; i++)
{
if(!count%2)//if the mod of the count is 0 then add it to the sum
{
sum += count;//add the current count value to the sum

//the next line can be used to debug the loop
//System.out.println("Current count is - " + count);
}
}
System.out.println(sum);
}
}
}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |