允许用户使用多线程输入斐波那契数列和阶乘计算的线程数
我想使用扫描仪来获取用户输入的要运行的线程数。到目前为止,该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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论