[Informatica] Estrattore casuale di numeri nell'intervallo [1;100], Java, BlueJ

« Older   Newer »
  Share  
qerubin
view post Posted on 16/6/2009, 19:41




Un semplice programmino che estrae numeri casuali da 1 a 100, estremi compresi. Può estrarre più volte lo stesso numero.
CODICE
import javax.swing.*;
import java.io.*;
public class randomnumeri{
   public static void main (String args[]) throws IOException{
       int n=Integer.parseInt(JOptionPane.showInputDialog("Questo programma estrae numeri casuali da 1 a 100.\nQuanti numeri estrarre?"));
       char opt=0;
       String extr,ris;
       System.out.println("I numeri estratti sono:");
       for(int i=0; i<n; i++)
       {
           ris="";
           double rand=Math.random();
           double extr2=rand*100;
           extr=String.valueOf(extr2);
           if(extr.charAt(1)=='.')
               ris=ris+extr.charAt(0);
           else if(extr.charAt(2)=='.')
                   ris=ris+extr.charAt(0)+extr.charAt(1);
               else
                   ris=ris+extr.charAt(0)+extr.charAt(1)+extr.charAt(2);
           int a=Integer.parseInt(ris)+1;
           System.out.println(a);
       }
   }
}
 
Top
pianista
view post Posted on 22/6/2009, 21:19




se restringiamo da 1 a 90 possiamo fare il gioco del lotto o la tombola (:
 
Top
qerubin
view post Posted on 23/6/2009, 07:31




Per restringere basta imporre un if e scartare i numeri superiori a novanta ^^ però è fattibilissimo ^^ Appena finito gli esami ricordamelo ^^
 
Top
2 replies since 16/6/2009, 19:41   663 views
  Share