允许用户使用多线程输入斐波那契数列和阶乘计算的线程数

发布于 2025-01-11 18:07:37 字数 2326 浏览 0 评论 0原文

我想使用扫描仪来获取用户输入的要运行的线程数。到目前为止,该java代码能够运行斐波那契数列的数量以及基于用户输入的阶乘计算。如何允许用户输入线程数?:

import java.io.IOException; 导入java.util.Scanner;

斐波那契类扩展了线程 {

@Override
public void run() {
    
        
    int n1=0, n2=0, n3=1;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the amount of Fibonacci: ");
    int number = sc.nextInt();
    for(int i=1; i<=number; i++) {
        n1 = n2;                                
        n2 = n3;
        n3 = n1 + n2;
        System.out.println(n1 + "  ");
    }
        

        
        }
    }

阶乘类扩展了线程 {

@Override
public void run() {
    try {
        
            long fact = 1;
            int i = 1, n;
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a number for the calculation of Factorial:    ");
            n = input.nextInt();
            input.close();
            
            while(i<=n)
            {
                fact = fact * i;
                i++;
            }
            System.out.println("Factorial of "+n+" is: "+fact);
            
                
        
            
    } catch (NumberFormatException ex) {

    }
}

}

公共类Assignment02 {

public static void main(String args[]) throws IOException {
    Scanner input = new Scanner(System.in);
    System.out.println("1.Fibonacci");
    System.out.println("2.Factorial");
    System.out.println("Choose your Option:");
    String line = input.nextLine();
    int option = Integer.parseInt(line);
    
    switch (option) {
        case 1:
            System.out.println("Fibonacci Sequence SELECTED");
            try {

                Fibonacci fib = new Fibonacci();
                fib.start();
                fib.join();
                

            } catch (Exception e) {
            }

            break;

        case 2:
            System.out.println("Factorial SELECTED");
            try {
                
                Factorial fact = new Factorial();
                fact.start();
                fact.join();
                
            } catch (Exception e) {
            }
            break;

        default:
            System.out.println("Choose a valid option");
            break;
    }
    

} }

I want to use scanner for user input for the number of threads to run. So far, this java code is able to run both the number of the Fibonacci sequences and factorial calculation based on user input. How do I do allow user input for the number of threads?:

import java.io.IOException;
import java.util.Scanner;

class Fibonacci extends Thread {

@Override
public void run() {
    
        
    int n1=0, n2=0, n3=1;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the amount of Fibonacci: ");
    int number = sc.nextInt();
    for(int i=1; i<=number; i++) {
        n1 = n2;                                
        n2 = n3;
        n3 = n1 + n2;
        System.out.println(n1 + "  ");
    }
        

        
        }
    }

class Factorial extends Thread {

@Override
public void run() {
    try {
        
            long fact = 1;
            int i = 1, n;
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a number for the calculation of Factorial:    ");
            n = input.nextInt();
            input.close();
            
            while(i<=n)
            {
                fact = fact * i;
                i++;
            }
            System.out.println("Factorial of "+n+" is: "+fact);
            
                
        
            
    } catch (NumberFormatException ex) {

    }
}

}

public class Assignment02 {

public static void main(String args[]) throws IOException {
    Scanner input = new Scanner(System.in);
    System.out.println("1.Fibonacci");
    System.out.println("2.Factorial");
    System.out.println("Choose your Option:");
    String line = input.nextLine();
    int option = Integer.parseInt(line);
    
    switch (option) {
        case 1:
            System.out.println("Fibonacci Sequence SELECTED");
            try {

                Fibonacci fib = new Fibonacci();
                fib.start();
                fib.join();
                

            } catch (Exception e) {
            }

            break;

        case 2:
            System.out.println("Factorial SELECTED");
            try {
                
                Factorial fact = new Factorial();
                fact.start();
                fact.join();
                
            } catch (Exception e) {
            }
            break;

        default:
            System.out.println("Choose a valid option");
            break;
    }
    

}
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文