Hi I'm new to this forum and to java, I have some work to do which requires that I have to write a program that reads in a list of whole numbers and that only even numbers
in the range 1-100 are accepted and both 'N' and 'n' terminate the program.
Here is 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, Sum = 0;
		char Choice;
		   
		     
		do
		{
			System.out.println("Enter the list of whole numbers, terminated by -999");
			Number = sc.nextInt();

			while(( Number != -999)&& (Number <=100))
			{
				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');
	}
}

Please can anyone help me out?
Rach

Recommended Answers

All 8 Replies

Sure. What's your question? (I think I can guess, but I'd rather you state the problem explicitly)

Sure. What's your question? (I think I can guess, but I'd rather you state the problem explicitly)

Thank you! I need to amend the program I have so that it only accepts even numbers, I'm not really sure how to do that, do I need to use %? And I'm not sure how to get it to accept both uppercase and lowercase letter to terminate it, so far only 'N' terminates the program.
Thanks Rach

Okay. First problem: definition of an even number is one that divides evenly by two. You're right, the modulo operator (%) is the way to get this. That'll get you the remainder from an integer division - if the remainder is zero, the number divides evenly.

Second problem: you want to construct an expression that evaluates to true for any input except 'N' or 'n'. There are several equivalent statements that will do this. You're going to have to use the logical and (&&) or logical or (||) to compound two simple equality tests.

Try some of these if you're not feeling comfortable with boolean logic:
True && True=> true
True || False => true
True && False => ???
!True || False => ???
!True && !True => ???
(True) || (any statement) => ????
(any statement) || False => ????

Those might limber you up to address this problem :)

That's brilliant thanks!

do u think u can display the correct code for this problem i am stuck as u are and do not understand programming or coding thanks

Farrukh:
We help people with the programs that they write themselves. Please make an attempt to solve the problem yourself. And if you are still having problems, please post a new question on this site, with the code you have written, and we would be glad to help you.

in while bracket type: Choice.equalsIgnoreCase('n').
By this Loop will terminate on 'N' or 'n'.

vivekH: this is an ancient (read: dead) thread. There's little point in commenting on it.

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.