I am trying to make a loop that takes user input until a number that is less than or equal to zero is entered or a number equal to the previous number is entered. I am having trouble figuring out how to code the user's previous input as a variable.
Directions
Write a program that has the class name Problem2 and that has the main method.
In the main method, prompt the user to enter positive integers until the user enters either a value that is less than or equal to 0 or when the number entered is less than or equal to the number entered previously.
A step is a positive difference between an entered integer and the value entered prior to that integer. Calculate the number of big steps (differences of more than 5) and the number of small steps (differences of less than or equal to 5). Print out the big steps and the small steps and the number that caused the program to end.
Examples:
Enter a positive integer: -10
Big steps: 0
Small steps: 0
Ending value: -10
Enter a positive integer: 1
Enter a positive integer: -3
Big steps: 0
Small steps: 0
Ending value: -3
Enter a positive integer: 4
Enter a positive integer: 12
Enter a positive integer: 13
Enter a positive integer: 15
Enter a positive integer: 21
Enter a positive integer: 26
Enter a positive integer: 21
Big steps: 2
Small steps: 3
Ending value: 21
Enter a positive integer: 71
Enter a positive integer: 72
Enter a positive integer: 72
Big steps: 0
Small steps: 1
Ending value: 72
Enter a positive integer: 45
Enter a positive integer: 62
Enter a positive integer: 0
Big steps: 1
Small steps: 0
Ending value: 0
How can I make a variable that is equal to the previous number is if it keeps changing?
How I understand it: You have five inputs total for example that is going to be entered by the user: 12, 15, 26, 31, 48.
Once you have 12, 15, 26 then the last number entered is 26 and the previous number entered is 15. When 31 and 48 are entered then the last number becomes 48 and the previous number becomes 31. If there are more numbers then the last input and previous input would continue to change until the loop ends.
How do I make a variable that would be equal to the number that is previously entered by the user if the numbers keep changing until the loop ends?
Am I understanding this correctly or am I going about it wrong?
My code so far:
public static void main(String args[]) {
// create variables
int numberOfBigSteps = 0;
int numberOfSmallSteps = 0;
int currentEntry = 0;
int previousEntry = 0;
Scanner scan = new Scanner(System.in);
while (currentEntry > 0) {
// loop user input until the user enters a number that is either
// a) less than or equal to 0
// b) less than the previous number
// currently it is not working at all and I can't enter any numbers
System.out.print("Enter a positive integer: ");
currentEntry = scan.nextInt();
if (currentEntry <= 0 || currentEntry <= previousEntry) {
if ((currentEntry - previousEntry) > 5)
// difference of more than 5
numberOfBigSteps++;
}
if ((currentEntry - previousEntry) <= 5) {
numberOfSmallSteps++;
if (currentEntry == previousEntry) {
numberOfSmallSteps++;
}
}
// output
System.out.println("Big steps: " + numberOfBigSteps);
System.out.println("Small steps: " + numberOfSmallSteps);
System.out.println("Ending value: " + currentEntry);
}
}
while (currentEntry > 0)followsint currentEntry = 0;so it is never executed