[Java] Probleme cu numere aleatorii
:: Diverse :: Programare :: Tutoriale
Pagina 1 din 1
[Java] Probleme cu numere aleatorii
Program 1 - zarul ( random number) o cifra
Program 1.1 -zarul ( random number)10 cifre
Program 2 -n factorial
Program 3 -aflarea numarului de cifre mai mari decat o cifra data
- Cod:
package javaapplication2;
/**
*
* @author vaipan.dragos
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int zar=(int)(1+Math.random()*6);
System.out.println("zar="+zar);
}
}
Program 1.1 -zarul ( random number)10 cifre
- Cod:
package javaapplication2;
import java.util.Random;//java.util*
/**
*
* @author vaipan.dragos
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int zar=(int)(1+Math.random()*6);
int i;
System.out.println("zar="+zar);
for (i=1;i<10;i++)
{
Random r=new Random();
zar =1+r.nextInt(6);
System.out.println("zar="+zar);
}
}
}
Program 2 -n factorial
- Cod:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
import java.util.Random;//java.util*
/**
*
* @author vaipan.dragos
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int i, n_fact;
int n=(int)(1+Math.random()*6);
Random r=new Random();
n =1+r.nextInt(100);
System.out.println("n="+n);
if (n<=13){
n_fact=1;
for(i=1;i<=n;i++){
n_fact=n_fact*i;
}
System.out.println("n factorial="+n_fact);
}
}
}
Program 3 -aflarea numarului de cifre mai mari decat o cifra data
- Cod:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication4;
import java.util.Random;//java.util*
/**
*
* @author vaipan.dragos
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random r=new Random();
int n=r.nextInt(50);
int sir[]=new int[n];
int contor=0;
int nr=r.nextInt(1000);
for (int i=0;i<n;i++){
sir[i]=r.nextInt(1000);
if (sir[i]>nr)
contor++;
}
for (int i=0;i<n;i++){
System.out.print(sir[i]+" ");
}
System.out.println();
System.out.println("Numarul de cifre mai mare decat "+nr+" este "+contor);
}
}
BABILONIA- Membru, skill +10
- Mesaje : 644
Data de inscriere : 29/08/2012
Varsta : 27
Localizare : Bucuresti
:: Diverse :: Programare :: Tutoriale
Pagina 1 din 1
Permisiunile acestui forum:
Nu puteti raspunde la subiectele acestui forum
|
|