简体   繁体   English

如何使用用户输入在 java 中创建一个循环,并使变量成为最后一个输入和上一个输入?

[英]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.编写一个程序,名称为 class Problem2 并具有 main 方法。

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.在 main 方法中,提示用户输入正整数,直到用户输入小于或等于 0 的值,或者输入的数字小于或等于之前输入的数字。

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).一步是输入的 integer 和之前输入的值 integer 之间的正差。计算大步数(大于 5 的差异)和小步数(小于或等于 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.我是如何理解的:例如,您总共有五个输入将由用户输入: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.一旦您有 12、15、26,那么最后输入的数字是 26,之前输入的数字是 15。当输入 31 和 48 时,最后一个数字变为 48,之前的数字变为 31。如果有更多数字,那么最后一个input 和 previous input 将继续更改,直到循环结束。

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:这是因为您将“currentEntry”声明并初始化为零,然后开始循环:

while (currentEntry > 0) {

So the code doesn't enter the loop at all because 0 is NEVER greater than 0.所以代码根本不会进入循环,因为 0 永远不会大于 0。

After you get an integer, your logic is backwards:在你得到一个 integer 之后,你的逻辑就倒退了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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