Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

subtract itself minus 1 continuously

807600Nov 7 2007 — edited Nov 8 2007
Hi there, i'm trying to prepare for a lab exam i have tomorrow and i'm not quite sure how i would something like this:

Create an Eclipse project called ex14. In this project, create a class called Exam14 that is an application that queries the user for a positive integer. If the user enters a String that is not a valid positive integer, the program should keep querying until a valid positive integer is entered. The numbers 0, 1 and 2, all negative integers, and any non integers are not considered valid positive integers. Once a valid positive integer is entered, the program should print out all of the even positive integers from 2 up to but not including the integer that was entered, with the header line shown in the sample below. All of these integers should be printed on the same line with a single blank between them. Note that if all of the integers won't fit on a single line, the terminal will automatically wrap the integers that won't fit onto succeeding lines. The program should then print out the following integers, each on its own line:

the integer entered
the integer entered minus 1
the integer entered minus 1 minus 2
the integer entered minus 1 minus 2 minus 3
etc.

This is my code so far:
public class Exam14 {
	
	public static void main(String[] args) {
		
		Integer userInput;
		int subtractItself = 0;
		
		System.out.print("Please enter an Integer>");
		userInput = Keyboard.in.readInteger();
		
		while (userInput == null || userInput.intValue() <= 2){
			System.out.print("Please enter an Integer>");
			userInput = Keyboard.in.readInteger();
		}
		
		if (userInput != null && userInput.intValue() > 2){
			System.out.println("The even integers up to but not including " + userInput.intValue() + " are:");
			//int evenIntegers = 2;
			for (int evenIntegers = 2; evenIntegers < userInput.intValue(); evenIntegers = evenIntegers + 2)
				System.out.print(evenIntegers + " ");
			System.out.println();
			
			System.out.println("The interesting integers up to " + userInput.intValue() + " are:");
			//System.out.println(userInput.intValue());
			int n = 1;
			while ((userInput.intValue() - n) > 0){
			System.out.println(userInput.intValue() - subtractItself*n); //This is where my error is
				//while ()
			//n = n + n++;
			n++;
			subtractItself++;
			}
		}

	}

}
How would i create a loop that subtracts itself minus n, than subtracts itselft minus the previous subtraction minus n+1.... etc?


This problem also came up in a seperate practice where i needed to make a code that would count the factorial of a number.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 6 2007
Added on Nov 7 2007
5 comments
719 views