java - Need code that counts variables from a method in a class generated randomly for each time the program is run -
can count each time variable choice 1 or each time variable choice 2 add total times each count generated?
ross gerard is: drinking coffee
ross gerard is: @ work
ross gerard is: eating snack
ross gerard is: @ work
ross gerard is: playing dogs
ross gerard is: primping in mirror
ross gerard is: doing jiu-jitsu
ross gerard is: playing dogs
ross gerard is: eating snack
ross gerard is: walking
ross gerard is: eating snack
ross gerard is: playing nintendo
ross gerard is: playing dogs
ross gerard is: singing
ross gerard is: walking
ross gerard is: kissing girlfriend
ross gerard is: @ work
ross gerard is: swimming
ross gerard is: playing nintendo
ross gerard is: studying java
import java.util.random; class student { public void studentisdoing() { (int counter = 0; counter < 20; counter++) { random r = new random(); int choice = 1 + r.nextint(15); string response = ""; if ( choice == 1 ) response = "playing ball"; else if ( choice == 2 ) response = "studying java"; else if ( choice == 3 ) response = "kissing girlfriend"; else if ( choice == 4 ) response = "swimming"; else if ( choice == 5 ) response = "doing jiu-jitsu"; else if ( choice == 6 ) response = "reading paper"; else if ( choice == 7 ) response = "singing"; else if ( choice == 8 ) response = "primping in mirror"; else if ( choice == 9 ) response = "taking shower"; else if ( choice == 10 ) response = "eating snack"; else if ( choice == 11 ) response = "drinking coffee"; else if ( choice == 12 ) response = "walking"; else if ( choice == 13 ) response = "playing dogs"; else if ( choice == 14 ) response = "at work"; else if ( choice == 15 ) response = "playing nintendo"; else response = "in bathroom"; system.out.println( "ross gerard is: " + response ); } } }
first of all, advise using switch syntax kind of if-else conditions.
for question, should initailize array of size 16 (or 15) @ start of loop, , after getting choice selected add:
arr[choice]++;
at end of loop, array hold in each index amount of times choice chosen.
Comments
Post a Comment