Test Foro de elhacker.net SMF 2.1

Programación => Programación General => Java => Mensaje iniciado por: Didy en 12 Mayo 2013, 21:36 PM

Título: Tablas de multiplicar con Hilos.
Publicado por: Didy en 12 Mayo 2013, 21:36 PM
Hola buenas, resulta que tengo que realizar un programa en Java por consola,  lance un hilo que imprima la tabla de multiplicar de ese número.

Alguna idea? Gracias!
Título: Re: Tablas de multiplicar con Hilos.
Publicado por: Nasty35 en 12 Mayo 2013, 23:00 PM
Si bien entendí. sería algo así:
Código (java) [Seleccionar]
package test;

import java.util.Scanner;

public class Test extends Thread {

public static void main(String[] args) {
System.out.print("Numero: ");
int num = new Scanner(System.in).nextInt(); // recibimos el numero por input
new Thread(new Test(num)).start(); // creamos e iniciamos el hilo
}

private int num;

public Test(int num) {
this.num = num;
}

@Override
public void run() {
// sacamos toda la tabla de multiplicar del numero
System.out.println("1 * " + this.num + " = " + 1 * this.num);
System.out.println("2 * " + this.num + " = " + 2 * this.num);
System.out.println("3 * " + this.num + " = " + 3 * this.num);
System.out.println("4 * " + this.num + " = " + 4 * this.num);
System.out.println("5 * " + this.num + " = " + 5 * this.num);
System.out.println("6 * " + this.num + " = " + 6 * this.num);
System.out.println("7 * " + this.num + " = " + 7 * this.num);
System.out.println("8 * " + this.num + " = " + 8 * this.num);
System.out.println("9 * " + this.num + " = " + 9 * this.num);
System.out.println("10 * " + this.num + " = " + 10 * this.num);
}

}

Ejemplo de Output con el número 5:
Numero: 5
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
Título: Re: Tablas de multiplicar con Hilos.
Publicado por: Didy en 12 Mayo 2013, 23:14 PM
Si, exacto !! Es justamente eso! Muchas gracias, me has servido de mucha ayuda! No lograba entender cómo hacerlo.