Java 两个输入之间的斐波那契数

Java 两个输入之间的斐波那契数,java,fibonacci,Java,Fibonacci,我的作业有困难。。。“编写一个程序,向用户询问两个数字:下限和上限。您的程序应该打印从下限到上限范围内的所有斐波纳契数,以及斐波纳契数列中所有偶数的总和。”我不知道如何获得两个输入之间的数字。现在它只给出从零到 以下是我到目前为止的情况: public static void main(String[] args) { Scanner scr = new Scanner(System.in); System.out.println ("Enter lower bound:");

我的作业有困难。。。“编写一个程序,向用户询问两个数字:下限和上限。您的程序应该打印从下限到上限范围内的所有斐波纳契数,以及斐波纳契数列中所有偶数的总和。”我不知道如何获得两个输入之间的数字。现在它只给出从零到

以下是我到目前为止的情况:

public static void main(String[] args) 
{
    Scanner scr = new Scanner(System.in);
    System.out.println ("Enter lower bound:");
    int lower = Integer.parseInt(scr.nextLine());
    System.out.println ("Enter upper bound:");
    int upper = Integer.parseInt(scr.nextLine());

    int fiboCounter = 1;
    int first = 0;
    int second = 1;
    int fibo = 0;
    int oddTotal = 1;
    System.out.println("The fibonacci numbers between ");
    while(fiboCounter < upper)
    {
        fibo= first + second;
        first = second;
        second = fibo;
        if(fibo % 2 == 0) 
            oddTotal = oddTotal + fibo;

        System.out.print(" "+ fibo+ " ");
        fiboCounter++;
    }
    System.out.println();
    System.out.println("Total of even Fibos: "+ oddTotal);
}
publicstaticvoidmain(字符串[]args)
{
扫描仪scr=新扫描仪(System.in);
System.out.println(“输入下限:”);
int lower=Integer.parseInt(scr.nextLine());
System.out.println(“输入上限:”);
int upper=Integer.parseInt(scr.nextLine());
int fiboCounter=1;
int first=0;
int秒=1;
int-fibo=0;
int-oddtall=1;
System.out.println(“两者之间的斐波那契数”);
而(光纤计数器<上)
{
fibo=第一+第二;
第一=第二;
第二个=fibo;
如果(fibo%2==0)
oddTotal=oddTotal+fibo;
系统输出打印(“+fibo+”);
fiboCounter++;
}
System.out.println();
System.out.println(“偶数Fibos总数:+oddTotal”);
}

您只需检查计算的数字是否足够大:

public static void main(String[] args) {
    Scanner scr = new Scanner(System.in);
    System.out.println ("Enter lower bound:");
    int lower = Integer.parseInt(scr.nextLine());
    System.out.println ("Enter upper bound:");
    int upper = Integer.parseInt(scr.nextLine());

    // This is how you can initialize multiple variables of the same type with the same value.
    int fiboCounter, second, oddTotal = 1;
    int first, fibo = 0;

    System.out.println("The fibonacci numbers between ");
    while( fiboCounter < upper ) {
        fibo= first + second;
        first= second;
        second=fibo;
        if(fibo%2==0) oddTotal=oddTotal+fibo;

        // Just check if its high enough
        if(fibo > lower ) {
            System.out.print(" "+ fibo + " ");
        }
        fiboCounter++;
    }

    System.out.println("\nTotal of even Fibos: "+ oddTotal);
    // The \n is just the same as System.out.println()

    // You probably want to close the scanner afterwards
    scanner.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪scr=新扫描仪(System.in);
System.out.println(“输入下限:”);
int lower=Integer.parseInt(scr.nextLine());
System.out.println(“输入上限:”);
int upper=Integer.parseInt(scr.nextLine());
//这就是如何用相同的值初始化相同类型的多个变量。
int fiboCounter,秒,oddTotal=1;
int优先,fibo=0;
System.out.println(“两者之间的斐波那契数”);
而(光纤计数器<上){
fibo=第一+第二;
第一=第二;
第二个=fibo;
如果(fibo%2==0)ODDTOAL=ODDTOAL+fibo;
//看看它是否足够高
如果(fibo>较低){
系统输出打印(“+fibo+”);
}
fiboCounter++;
}
System.out.println(“\n偶数Fibos总和:“+oddtottal”);
//\n与System.out.println()相同
//您可能希望稍后关闭扫描仪
scanner.close();
}

我对代码做了一些修改,使其更具可读性。

您只需检查计算的数字是否足够大:

public static void main(String[] args) {
    Scanner scr = new Scanner(System.in);
    System.out.println ("Enter lower bound:");
    int lower = Integer.parseInt(scr.nextLine());
    System.out.println ("Enter upper bound:");
    int upper = Integer.parseInt(scr.nextLine());

    // This is how you can initialize multiple variables of the same type with the same value.
    int fiboCounter, second, oddTotal = 1;
    int first, fibo = 0;

    System.out.println("The fibonacci numbers between ");
    while( fiboCounter < upper ) {
        fibo= first + second;
        first= second;
        second=fibo;
        if(fibo%2==0) oddTotal=oddTotal+fibo;

        // Just check if its high enough
        if(fibo > lower ) {
            System.out.print(" "+ fibo + " ");
        }
        fiboCounter++;
    }

    System.out.println("\nTotal of even Fibos: "+ oddTotal);
    // The \n is just the same as System.out.println()

    // You probably want to close the scanner afterwards
    scanner.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪scr=新扫描仪(System.in);
System.out.println(“输入下限:”);
int lower=Integer.parseInt(scr.nextLine());
System.out.println(“输入上限:”);
int upper=Integer.parseInt(scr.nextLine());
//这就是如何用相同的值初始化相同类型的多个变量。
int fiboCounter,秒,oddTotal=1;
int优先,fibo=0;
System.out.println(“两者之间的斐波那契数”);
而(光纤计数器<上){
fibo=第一+第二;
第一=第二;
第二个=fibo;
如果(fibo%2==0)ODDTOAL=ODDTOAL+fibo;
//看看它是否足够高
如果(fibo>较低){
系统输出打印(“+fibo+”);
}
fiboCounter++;
}
System.out.println(“\n偶数Fibos总和:“+oddtottal”);
//\n与System.out.println()相同
//您可能希望稍后关闭扫描仪
scanner.close();
}

我对代码做了一些修改,使其更具可读性。

首先,像往常一样计算斐波那契数,超过上限时停止(使用循环)。在循环内部,除了计算斐波那契数,只有当它大于下限时才打印出来。首先,像往常一样计算斐波那契数,超过上限时停止(使用循环)。在循环内部,除了计算斐波那契数外,只有当它大于下限时才打印出来。