如何在java中使用用户输入创建循环并具有最后输入和先前输入的变量?

发布于 2025-01-12 00:45:40 字数 2621 浏览 3 评论 0原文

我正在尝试创建一个循环,接受用户输入,直到输入小于或等于零的数字或输入等于前一个数字的数字。我无法弄清楚如何将用户之前的输入编码为变量。

路线

编写一个具有类名 Problem2 且具有 main 的程序 方法。

在main方法中,提示用户输入正整数,直到 用户输入一个小于或等于 0 的值或 当输入的数字小于或等于输入的数字时 以前。

步长是输入的整数与实际值之间的正差 在该整数之前输入的值。计算大数 步数(相差超过5)和小步数 (差异小于或等于5)。打印出大步骤并 小步骤和导致程序结束的数字。

示例:

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 

如果变量不断变化,如何使变量等于前一个数字?

我的理解是:例如,您总共有五个输入,将由用户输入:12、15、26、31、48。

一旦您有 12、15、26,那么最后输入的数字是 26,前一个数字是 26。输入的数字是 15。当输入 31 和 48 时,最后一个数字变为 48,前一个数字变为 31。如果还有更多数字,则最后一个输入和前一个输入将继续改变直到循环结束。

如果数字一直变化直到循环结束,如何创建一个等于用户之前输入的数字的变量?

我的理解是正确的还是错误的?

到目前为止我的代码:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2025-01-19 00:45:40

// 目前它根本不起作用,我无法输入任何数字

这是因为您将“currentEntry”声明并初始化为零,然后用以下内容开始循环:

while (currentEntry > 0) {

所以代码根本不会进入循环,因为 0永远不会大于0。

在你得到一个整数之后,你的逻辑是倒退的:

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

你是说如果当前数字是负数或者小于或等于前一个数字,你应该计算步长值,而实际上这是一个结束您只需停止循环并显示的条件输出。

要存储您当前没有执行的先前数字,您只需将此行放在循环的底部:

previousEntry = currentEntry;

您实际上非常接近正确答案。

这是我的版本:

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

// 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文