Ask Experts Questions for FREE Help !
Ask
    nairuz's Avatar
    nairuz Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 13, 2013, 08:05 AM
    Java Application
    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's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #2

    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's Avatar
    shanaka1992 Posts: 5, Reputation: 1
    New Member
     
    #3

    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's Avatar
    shanaka1992 Posts: 5, Reputation: 1
    New Member
     
    #4

    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 ");
    }
    }
    }

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java netbeans program- Connection between java and mysql,How to conn [ 0 Answers ]

which I wrote from a book but when I run it, it shows exception please give me the code to connect jlistbox to database when I click on btn1. btn coding should search "name" in database and shows all the names in listbox my coding: here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...

Write a JAVA application in a client/server environment. [ 1 Answers ]

Write a JAVA application that allows users to update their personal information in a database in a client/server environment. Windows: Window 1: Window1 should allow the user to select between two options ADD or UPDATE. It should contain two buttons and based on the user selection the user...

Pending appeal of your old application for benefits, can you file new application? [ 1 Answers ]

Pending appeal of your old application for benefits, can you file new application?


View more questions Search