首页 > 解决方案 > Java中的二项式在获取用户输入后将2个变量保存在一个变量中

问题描述

我尝试在迭代后保存两个用户输入,但我不知道该怎么做。我总是得到

java:19: error: variable x might not have been initialized
long resalt = bio(x, y);

源代码:

import java.util.Scanner;

public class Aufgabe11 {

    public static void main (String [] args) {

        Scanner pit = new Scanner(System.in);
        System.out.println("Enter fac number");
        long a = pit.nextLong();
        long result = recFac(a);
        System.out.println("The factorial of" + " "+ a + " " + "is"+ " " + result);
        Scanner pat = new Scanner(System.in);
        long[] vars = new long [2];
        for(int i = 0; i < vars.length; i++){
            System.out.println("Enter bio var:");
            vars [i] = pat.nextLong();
        }
        long x,y = pat.nextLong();
        long resalt = bio(x, y);
        System.out.println("The bio of" + " " + x + "over" + y + "is" + " " + resalt);
    }
    public static long recFac (long a) {
        if (a <= 1) {
            return 1;
        }
        else {
            return a * recFac (a-1);
        }
    }
    public static long bio (long x, long y) {
        if ((x == y) || (y == 0))
        return 1;
        else 
        return bio (x-1, y) + bio (x-1, y-1);

    }
 }

标签: javauser-input

解决方案


推荐阅读