有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中斐波那契序列中的BufferedReader错误

我是Java新手,来自C语言背景。在尝试实现斐波那契序列时,我收到以下错误:

 error: cannot find symbol
    n = Integer.parseInt(br.readline());
                           ^
 symbol:   method readline()
 location: variable br of type BufferedReader

计划如下:

import java.io.*;

public class fibonacci
{
public static void main(String args[])throws IOException
{
    int n;
    InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(read);

    System.out.println("enter the value of n");
    n = Integer.parseInt(br.readline());

int fib[];
    fib[1]=1;
    fib[2]=1;
    for(int i=3; i<=n; i++)
    {
        fib[i] = fib[i-1] + fib[i-2] ;
    }

    System.out.println("the"+n+"th fibonacci term is "+fib[n]);
}
}

请帮忙


共 (2) 个答案

  1. # 1 楼答案

    因为它应该是readLine而不是readline。请记住JAVA是区分大小写的。请使用IDE,它甚至会在编译之前捕捉到这样的错误

  2. # 2 楼答案

    Java区分大小写

    n = Integer.parseInt(br.readline());
                                ^
    

    应该是

    n = Integer.parseInt(br.readLine());