打印 1-99 之间的斐波那契数列

发布于 2025-01-17 07:56:57 字数 1057 浏览 0 评论 0原文

列出的程序是关于在给定低于 100 的用户输入的情况下编写斐波那契数列。我遇到的问题是弄清楚如果用户列出的数字高于 100,如何让斐波那契不打印。但是,我无法弄清楚该怎么做。这是我的代码,我不想使用 while 循环仅打印数字在 1 到 100 之间的情况。但是,当我打印 101 时,它仍然给出斐波那契数列。

import java.util.Scanner;

class Program_2 { 
  public static void main(String [] args){
    Scanner console = new Scanner(System.in);

    System.out.println("Enter an integer between 1-99: ");
    int num = console.nextInt(); 

    while (num>1) { 
      if(num<100) {
      System.out.println("Fibonacci Series till " + num + " terms:");
        }
      int n = 100, firstTerm = 0, secondTerm = 1;
      for (int i = 1; i <= n; ++i) {
        System.out.print(firstTerm + ", ");
  
        // compute the next term
        int nextTerm = firstTerm + secondTerm;
        firstTerm = secondTerm;
        secondTerm = nextTerm;
      
        }      
    }
   } 
  }

我打印 101 时得到的输出是

在此处输入图像描述

我使用了 if/else 语句,但是由于某种原因,它没有做任何事情。然后我尝试使用 while 循环和 if 语句,但它的打印结果仍然高于 100。我不确定如何解决这个问题。非常感谢任何帮助!

The program listed is about writing the Fibonacci series given user input that is below 100. The problem that I am having is figuring out how to get the Fibonacci to not print if the number listed by the user is above 100. However, I am unable to figure out what to do. This is my code, I am not trying to use while loop to only print if the number is between 1 and 100. however when I print 101, it still gives me the Fibonacci series.

import java.util.Scanner;

class Program_2 { 
  public static void main(String [] args){
    Scanner console = new Scanner(System.in);

    System.out.println("Enter an integer between 1-99: ");
    int num = console.nextInt(); 

    while (num>1) { 
      if(num<100) {
      System.out.println("Fibonacci Series till " + num + " terms:");
        }
      int n = 100, firstTerm = 0, secondTerm = 1;
      for (int i = 1; i <= n; ++i) {
        System.out.print(firstTerm + ", ");
  
        // compute the next term
        int nextTerm = firstTerm + secondTerm;
        firstTerm = secondTerm;
        secondTerm = nextTerm;
      
        }      
    }
   } 
  }

The Output that im getting when i print 101, is

enter image description here

I've used an if/else statement, but for some reason, it didnt do anything. Then I tried using a while loop and an if statement, but its still printing above 100. I am unsure how to go about this. Any help is very much appreciated!

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

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

发布评论

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

评论(2

神魇的王 2025-01-24 07:56:58

要解决此问题,您可以在 while 循环中添加另一个条件。现在,只要 num 大于 1,您就会进入 while 循环。因此,也会考虑 100 以上的数字。要解决此问题,您可以像这样启动 while 循环: while(num>1 && num<=100)。

To fix this issue, you can add another condition into your while loop. Right now, you are going into the while loop as long as num is greater than 1. Thus, numbers above 100 are being considered as well. To fix this, you can make your while loop start like this: while(num>1 && num<=100).

圈圈圆圆圈圈 2025-01-24 07:56:58

你的 while 检查“while(n>1)”将无限运行。您需要在每次迭代后更改 num 的值,如下所示:

num--; //In the loop

num 变得小于 1 时,程序退出循环。

如果你想检查 num 是否小于 100 或大于 1 使用 if 语句(检查输入值是否符合你的范围)

while(num > 1 && num < 100){
    //your code in while loop
}else{
    System.out.println("Your value is too big or too small!");
}

而且我还注意到你的斐波那契序列输出不正确(负值,绑定在 int32 中)。您甚至无法以长值显示,因为第 9 阶斐波那契数超过千万亿。尝试使用 BigInteger 以便在控制台输出中显示正确的值。想要欣赏你的输出吗?在System.out.print()中使用/t%n

Your while check "while(n>1)" will run infinitely. You need to change num's value after every iteration like this:

num--; //In the loop

And when num becomes less than 1, the program exits from the loop.

If you want to check whether num is less than 100 or bigger than 1 use if statement(to check if input value corresponds to your range)

while(num > 1 && num < 100){
    //your code in while loop
}else{
    System.out.println("Your value is too big or too small!");
}

And also I've noticed your output of fibonacci sequence is not right(negative values, bound in int32). You can't even display in long values because fibonacci of 9*th order are over quadrillions. Try to use BigInteger in order to show right values in the console output. Want to fancy your output? Use /t and %n in System.out.print();

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