简体   繁体   中英

How can I make a loop in java with user input and have variables that are the last input and previous input?

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);
    }

}

// currently it is not working at all and I can't enter any numbers

This is because you declared and initialized "currentEntry" to zero, and then start your loop with:

while (currentEntry > 0) {

So the code doesn't enter the loop at all because 0 is NEVER greater than 0.

After you get an integer, your logic is backwards:

    if (currentEntry <= 0 || currentEntry <= previousEntry) {
        if ((currentEntry - previousEntry) > 5) // difference of more than 5
            numberOfBigSteps++;
    }

You're saying that if the current number is negative or less than or equal to the previous number you should calculate the step value when in fact that is an ending condition where you'd simply stop the loop and display the outputs.

To store the previous number, which you're not currently doing, you'd simply put this line at the bottom of the loop:

previousEntry = currentEntry;

You were actually pretty close to a correct answer.

Here's my version:

// create variables
int numberOfBigSteps = 0;
int numberOfSmallSteps = 0;
int currentEntry = -1;
int previousEntry = -1;
boolean keepGettingNumbers = true;    
Scanner scan = new Scanner(System.in);

while (keepGettingNumbers) {
  System.out.print("Enter a positive integer: ");
  currentEntry = scan.nextInt();

  keepGettingNumbers = (currentEntry>0 && currentEntry>previousEntry);
  if (keepGettingNumbers) {
    if (previousEntry != -1) {
      int step = currentEntry - previousEntry;
      if (step>5) {
        numberOfBigSteps++;
      }
      else {
        numberOfSmallSteps++;  
      }
    }
    previousEntry = currentEntry;
  }        
}

// output
System.out.println("Big steps: " + numberOfBigSteps);
System.out.println("Small steps: " + numberOfSmallSteps);
System.out.println("Ending value: " + currentEntry);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM