Wednesday, March 22, 2023

java.util.Random Class Example Random Number Example with Fun for IMCA,BCA,MCA and all IT Students


Prepared By : Uday Shah (HOD-IT) 
Contact No : 7600044051
-----------------------------------------------------------------------------------------------------------------------------
 //Random Number Example with Fun

import java.util.*;

public class NextIntAppli {

public static void main(String[] args)

{


int sum = 0, sum1 = 0, count = 0, count1 = 0;

int turn = 0;

String nm1="A", nm2="B",no=""; 

// creating random object

Random ran = new Random();

int flag = 0;


while (true) {

if(turn %2 == 0)

{

System.out.printf("A  Press any key to Continue .... ");

no = System.console().readLine();

}

else

{

System.out.printf("B  Press any key to Continue .... ");

no = System.console().readLine();

}

if (turn % 2 == 0) {


int p1 = ran.nextInt(6);

sum += p1;

}

else {

int p2 = ran.nextInt(6);

sum1 += p2;

}

System.out.printf("\nPlayer Name \t Turn \t Score\n");

System.out.printf("        A  \t%d\t%d\n", turn, sum);

System.out.printf("        B  \t%d\t%d\n", turn, sum1);

if (sum >= 30) {

flag = 1;

break;

}

if (sum1 >= 30) {

flag = 2;

break;

}

turn++;

}

if (flag == 1)

System.out.println("\nPlayer 1 WON!!");

else

System.out.println("\nPlayer 2 WON!!");

}

}