任何人都可以帮助我.. 如何获得这样的输出
方数
插入数字以获得方形值:2
数字2的平方是4
多维数据集
插入数字以获得方形值:3
3号的平方是27
现在我得到这样的输出
方数
插入数字以获得方形值:方形立方体
插入数字以获取立方体的值:2
数字2的平方是4
3
3号立方体是27
这里是我的编码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
class Thread_Square extends Thread{
private int sumSquare;
public void Thread_Square(){
BufferedReader square = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Square Number");
System.out.print("Insert Number to get Value of Square: ");
String inputSquare = null;
try {
inputSquare = square.readLine();
} catch (IOException ex) {
Logger.getLogger(Thread_Square.class.getName()).log(Level.SEVERE, null, ex);
}
int numSquare = Integer.parseInt(inputSquare);
sumSquare=numSquare*numSquare;
System.out.println("Square of number " +numSquare +" is " +sumSquare);
}
public void run()
{
Thread_Square();
}
}
class Thread_Cube extends Thread{
public void Thread_Cube(){
int sumCube;
BufferedReader Cube = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Square Cube");
System.out.print("Insert Number to get Value of Cube: ");
String inputCube = null;
try {
inputCube = Cube.readLine();
} catch (IOException ex) {
Logger.getLogger(Thread_Square.class.getName()).log(Level.SEVERE, null, ex);
}
int numCube = Integer.parseInt(inputCube);
sumCube=numCube*numCube*numCube;
System.out.println("Cube of number " +numCube +" is " +sumCube);
}
public void run()
{
Thread_Cube();
}
}
class Thread_Question1 {
public static void main(String[] args) throws IOException {
Thread_Square obj1 = new Thread_Square();
obj1.start();
Thread_Cube obj2 = new Thread_Cube();
obj2.start();
}
}
答案 0 :(得分:1)
你有两个同时并行运行的并发线程。对于第一个输出,您需要使用该流程一个线程,或者在完成第一个线程后强制第二个线程启动。看到这个:
Thread_Square obj1 = new Thread_Square();
obj1.start();
obj1.join();
Thread_Cube obj2 = new Thread_Cube();
obj2.start();
答案 1 :(得分:1)
等待使用join()
的第一个主题完成:
public static void main(String[ ] args) throws IOException, InterruptedException {
MultiThread obj1 = new MultiThread();
obj1.start();
obj1.join ();
ThreadCube obj2 = new ThreadCube();
obj2.start();
}