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
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
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 problem
Below is my program
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);
}
}
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;
}
}