Hello, im Ant. Forum looks really good. Im currently in the very early stages of learning Java, i have a few books and am doing online tutorials etc so looks like i will be using this place and searching the forums loads in the near future.

At the moment im trying doing small projects in a book i have but im stuck with Selection and loops. I have a small program here and im trying to get it so it only accepts even numbers in the range of 1 to 100 and capital and small "n" terminates the program. Can anyone please help me out?
Thank you!!

import java.util.*;
public class RepeatList
{
public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		int Number = 0, Sum = 0; //variables
		char Choice;
		
		//if (Number %2 ==0)
		
		do
		{
			System.out.println("Enter the list of whole numbers, terminated by -999");
			Number = sc.nextInt();

			while( Number != -999)
			{
				Sum = Sum + Number;
				Number = sc.nextInt(); //scanner class var
			}
			System.out.println("Sum is "+ Sum);
			System.out.print("Do you want to repeat the ");
			System.out.println("  Program ['Y' or 'N']");
			Choice = sc.next().charAt(0);

		} while( Choice != 'N'); //while choice is not equal to N
	}
}

Recommended Answers

All 12 Replies

1. Number is a class name. If you want to declare a 'Number' variable, you have to do it like this:

//Make sure you use a valid constructor
Number numberName = new Number();

//An alternative way
Number anotherNum = null;
//Somewhere later in the code, before you use the variable
anotherNum = new Number();

2. The do while loop was a good idea, however, you do not need the inner while loop whatsoever.

3. In order to test whether a number is even, consider the modulus operator. The modulus operator gives you, as an answer, the remainder of division. So 10 % 3 will be 1, since 10/3 is 3 Remainder 1. Another example: 15 % 4 = 3 because 15/4 = 3 remainder 3.

Also, I noticed that you named your char variable 'Choice'. By convention, variable names are supposed to be lowercase. Here are acceptable variables named:

someVariableName;
another_variable;
myVariable;

Here are some unacceptable names:
VariableName;
Current;
myvariable;

Of course, those are not descriptive variable names (and variable names should be descriptive), but those are the conventions for syntax.

there are different ways to do this, but since you say you're in the early stage of learning, don't try to do it all at once..

for instance, you could do it by a simple loop without the mathematical stuff, for instance.

int total = 0;
int number = 2;

while ( number < 100 ){
total += number;
number += 2;
}

this just let's you test the looping structures. later on, you can always adjust the code to make a more decent check for a number being even or not.

thats extremly helpful info there! I was creating more if statements and incorporating the do while loop inside of that. Totally the long way around!
Thank you very much for your help

I forgot to post what the program is actually supposed to do
Its supposed to take a series of numbers input from the user pressing return to after entering each one. Once all have been entered the user is then supposed to press either N or n to return calculation of the numbers on screen.


Im now having problems allowing the user to type in N or n to return the calculation for me. Can anyone please help a little further? heres what i have so far.

import java.util.*;
public class RepeatList
{
public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		int number = 0, Sum = 0; //variables

		char letter;
		char choice;
		
		//if (Number %2 ==0)
		
		do
		{
			System.out.println("Enter the list of whole numbers, pressing N or n will Calculate your numbers");
			number = sc.nextInt();
			letter = sc.next().charAt(0);

			while(letter != 'N' && letter != 'n' )
			{
				Sum = Sum + number;
				number = sc.nextInt(); 
			}
			System.out.println("Sum is "+ Sum);
			System.out.print("Do you want to repeat the ");
			System.out.println("  Program ['Y' or 'N']");
			choice = sc.next().charAt(0);

			

		} while( choice != 'N'); //while choice is not equal to N
	}
}

Thanks

I forgot to post what the program is actually supposed to do
Its supposed to take a series of numbers input from the user pressing return to after entering each one. Once all have been entered the user is then supposed to press either N or n to return calculation of the numbers on screen.


Im now having problems allowing the user to type in N or n to return the calculation for me. Can anyone please help a little further? heres what i have so far.

import java.util.*;
public class RepeatList
{
public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		int number = 0, Sum = 0; //variables

		char letter;
		char choice;
		
		//if (Number %2 ==0)
		
		do
		{
			System.out.println("Enter the list of whole numbers, pressing N or n will Calculate your numbers");
			number = sc.nextInt();
			letter = sc.next().charAt(0);

			while(letter != 'N' && letter != 'n' )
			{
				Sum = Sum + number;
				number = sc.nextInt(); 
			}
			System.out.println("Sum is "+ Sum);
			System.out.print("Do you want to repeat the ");
			System.out.println("  Program ['Y' or 'N']");
			choice = sc.next().charAt(0);

			

		} while( choice != 'N'); //while choice is not equal to N
	}
}

Thanks

I already told you to get rid of that inner while loop. You only need the do while loop, you do not need two loops. If you don't get that, that's fine. But please tell me why you don't understand so that I can help you better.

dear friend (everyone),

hello, seems that i was a new student of programming fundamentals with java, the problem i had now is using the modulus as a formula to write an algorithm to add up all odd numbers between 0 and 100.

so any idea of this kind of modulus formula?, let say
input num 1 = x //read x as 0<100
input num 2= y // read y as odd numbers
so, y=x%2=1 //modulus by 2 gets 1 (as i know any odd numbers modulus by 2 is 1.
can any one explain to me more details or any other opinion? this is what i understand. thank you, your cooperation is very much appreciated.

as in:

int total;
for (int i = 0; i<100; i++) {
if (i%2==1) total+=i;
}

?

I think that would server your intended function, but i would do it without modulus and change the for loop to look like:

for(int i = 1; i<100; i+2) {
total+=i;
}

TJ

thank you for replying. its not help me a lot but i got idea a bit. what i want is the explanation on how do modulus used, not the java syntax

thank you for replying. its not help me a lot but i got idea a bit. what i want is the explanation on how do modulus used, not the java syntax

no ... what you want is us to give you prefab code that you can copy paste into yours

the way modulus is used is that it divides the number on the left by the number on the right and gives you the remainder. using this function you can easily test for odd/even number along with many more functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.