Results 1 to 1 of 1
- 12-18-2009, 01:03 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Sometimes get the right results sometimes dont
Hi my code is running fine and everything but for somereasons my results seemt to be wrong about 50% of the time.
This is what my results look like
For simon it is correct since the number infront is the score but for John the score is something random. It might be something really simple that I'm unable to spot. Please if you have any time see if you can find the problemJava Code:Name of the team: Team1 There is 2 number of players for team Team1 please enter name of player 1: John please enter name of player 2: Pete Name of the team: Team2 There is 2 number of players for team Team2 please enter name of player 1: Leo please enter name of player 2: Simon TEAM Team1 51 John 17 Pete [Ljava.lang.String;@21abab88 TEAM Team2 56 Leo 68 Simon [Ljava.lang.String;@d8a7efd 51 John was picked 30 is the score 30 68 Simon was picked 68 is the score 68
Below is my program
Java Code:public class Driver { public static void main (String [] a) { //declarations Team nr1 = new Team(); Team nr2 = new Team(); int Team1score; int Team2score; //instructions System.out.println(nr1.ref()); System.out.println(nr2.ref()); Team1score = nr1.randomscore(); System.out.println(Team1score); Team2score = nr2.randomscore(); System.out.println(Team2score); } }Java Code:import java.util.Scanner; public class Team { //attributes private String[] players; private int[] score; static int nrplayers = (int)Math.floor(Math.random()*(9))+2; public Team () { Scanner in = new Scanner (System.in); System.out.println("Name of the team: "); String teamname = in.next(); players = new String [nrplayers]; System.out.printf("There is %d number of players for team %s \n", (nrplayers-1), teamname); players[0] = teamname; for (int i=1; i<players.length; i++){ score = new int [nrplayers-1]; for (int s=0; s<nrplayers-1; s++){ score[s] = (int)Math.floor(Math.random()*(100)); } System.out.printf("please enter name of player %d: ", (i)); String pname = in.next(); String space = " "; pname = score[i-1]+space+pname; players[i] = pname; } } public int randomscore () { int randomplayer = (int)Math.floor(Math.random()*(players.length-1))+1; System.out.println(players[randomplayer] + " was picked"); int randomPLSC = score[randomplayer-1]; System.out.println(randomPLSC + " is the score"); return randomPLSC; } public String [] ref () { System.out.println("\nTEAM " + players[0]); for (int i=1; i<players.length; i++){ System.out.println(players[i]); } return players; } }
Similar Threads
-
Hello! and I need help. I dont know were to start
By Fall0ut in forum New To JavaReplies: 10Last Post: 05-19-2010, 06:26 PM -
Dont use this program, Im warning you!
By Addez in forum JCreatorReplies: 3Last Post: 11-28-2009, 07:18 PM -
Dont understand Return Statement.
By ocean in forum New To JavaReplies: 6Last Post: 10-22-2009, 12:06 PM -
FileOutputStream, I dont know what Im doing wrong...
By Addez in forum New To JavaReplies: 6Last Post: 09-14-2009, 10:46 PM -
dont let me create simple class
By itaipee in forum New To JavaReplies: 5Last Post: 01-11-2009, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks