|
-
September 29th, 2009, 04:01 AM
#1
[RESOLVED] Java homework help!
1. Write a program that allows a user to input a sequence of consecutive numbers
A. Create a program SequentialNumbers that prompts the user for integer numbers. The program should let the user enter numbers as long as the current number is exactly one greater than the previous number.
I tried using this program:
import java.util.Scanner;
public class Sequence {
public static void main(String[] args) {
//input number which is exactly one greater or lower than the previous one, if they deviate,
// put out an error paragraph stating the difference is more than 2
int number;
int number1;
Scanner sc = new Scanner(System.in);
System.out.println("Input sequence of number differing by 1");
do {
System.out.print("Please input a number : ");
number = sc.nextInt();
System.out.print("Please input a number : ");
number1 = sc.nextInt();
} while ( ((number1 - number) == 1) || ((number1 - number) == -1) );
System.out.println("Your first number is " + number);
System.out.println("Your second number is " + number1);
System.out.println("The difference is " + (number1 - number) + ", which differs by more than 1. Try the program again");
}
}
However, it is flawed because it will not detect the difference if you input the data between 2nd and 3rd line (beginning of a new loop). This is driving me crazy, help me out please!
You are supposed to be able to input any number sequence as long as it only differs by 1. I have not learned arrays yet, I think I am supposed to complete this program using only basic for or do-while loop.
2. Write a program that allows a user to input a sequence of positive integer numbers terminated by any negative value. The program should report the smallest and greatest numbers that were entered.
A. Create a program MinMax that allows the user to input a sequence of positive integer numbers one by one.
o Each number should be compared to a Min and Max numbers which have been initialized to the first number entered by the user.
o If a new number entered is smaller that the min number, assign the value of the new number to the min number.
o If a new number entered is greater that the max number, assign the value of the new number to the max number.
o When the user enters a negative number, display the values of the min number and max number and terminate the program.
For this one, I am not even sure how to start... Using do-while loop, I can prompt for user input. However, the variable storing the user input also changes with every user input, which means, there is no way to compare values without storing it inside an array!!!! Yet, this assignment is given prior to our array class next week... Any solution?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|