JavaSE系列代码01:while语句的应用

While is a basic cycle mode of computer. When the conditions are met, enter the cycle, when the conditions are not met, jump out of the cycle. The general expression for a while statement is: while (expression) {loop body}.

import java.io.*;
public class Javase_01
{
  public static void main(String[] args) throws IOException
  {
    int a=0,b=1,n,num;
    String str;
    BufferedReader buf;
    buf=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("请输入一个正整数:");
    str=buf.readLine();         //从键盘上读入字符串赋给变量str
    num= Integer.parseInt (str);   //将str转换成int类型后赋给num
    while (b<num)
    {
      n=a+b;  
      a=b;
      b=n;
    }
    if (num==b)  System.out.println(num+"是Fibonacci数");
    else System.out.println(num+"不是Fibonacci数");
  }
}

发布了13 篇原创文章 · 获赞 160 · 访问量 8548

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105376137