Log in

View Full Version : Java Application


nairuz
Nov 13, 2013, 08:05 AM
I created small application where I choose five numbers randomly if they are divisible by 2 I won the game if not I lose . My question I want to tweak it .If I get three number divisible by 2 I won the game and if not I lose it .

This is code :

import java .util.Random ;
public class Boolean {

public static void main (String [] a) {

Random r = new Random ();

int [] num = new int [6];

boolean Result = true;

for ( int k = 0; k < num.length ; k++) {
num [k] = r.nextInt(100) +1 ;


if (num [k] %2 == 0 ) {

System.out.println("TRUE -"+ num[k] + "\tis divisble by 2 ." );

}else {

System.out.println("False -"+ num[k] + "\tis divisble by 2 ." );
Result = false;
}
}


if (Result ) {

System.out.println(" you won the have ");
}else {

System.out.println("you have lost the game ");
}
}
}

Scleros
Nov 30, 2013, 05:07 AM
One option is to increment a counter whenever the number is divisible by 2 and then check the counter.

shanaka1992
Jan 14, 2014, 08:41 AM
<strong>Check this out<br><br></strong><div>import java .util.Random ;</div><div>public class Boolean {</div><div>public static int count=0;</div><div>public static void main (String [] a) {</div><div><br></div><div>Random r = new Random ();</div><div><br></div><div>int [] num = new int [6];</div><div><br></div><div>boolean Result = true;</div><div><br></div><div>for ( int k = 0; k &lt; num.length ; k++) {<span class="Apple-tab-span" style="white-space:pre"> </span></div><div>num [k] = r.nextInt(100) +1 ;</div><div><br></div><div><br></div><div>if (num [k] %2 == 0 ) {</div><div>count++;</div><div>System.out.println("TRUE -"+ num[k] + "\tis divisble by 2 ." );</div><div><br></div><div><br></div><div>}else {</div><div><br></div><div>System.out.println("False -"+ num[k] + "\tis not divisble by 2 ." );</div><div>Result = false;</div><div>}</div><div>}</div><div><br></div><div>&nbsp; &nbsp; System.out.println("Count---"+count);</div><div>if (count&gt;=3 ) {</div><div><br></div><div>System.out.println(" you have won the game ");</div><div>}else {</div><div><br></div><div>System.out.println("you have lost the game ");</div><div>}</div><div>}</div><div>}</div>

shanaka1992
Jan 14, 2014, 08:42 AM
Check this out

import java .util.Random ;
public class Boolean {
public static int count=0;
public static void main (String [] a) {


Random r = new Random ();


int [] num = new int [6];


boolean Result = true;


for ( int k = 0; k < num.length ; k++) {
num [k] = r.nextInt(100) +1 ;




if (num [k] %2 == 0 ) {
count++;
System.out.println("TRUE -"+ num[k] + "\tis divisble by 2 ." );




}else {


System.out.println("False -"+ num[k] + "\tis not divisble by 2 ." );
Result = false;
}
}


System.out.println("Count---"+count);
if (count>=3 ) {


System.out.println(" you have won the game ");
}else {


System.out.println("you have lost the game ");
}
}
}