I'm quite new to programming and I've just started Java, I'm not sure about why my program is stuck in a loop -
import java.util.Scanner;
public class Tplayer {
static int numplayer;
static int startvalue;
public static int dice1;
public static int dice2;
public static int bet = 20;
//Advance control
static boolean preset;
static String [] playerarray;
static int [][] scorearray;
public static void addplayer(){
//Preset
if (preset == false){
startvalue = 500;
}
//Create and preset Player data
playerarray = new String[numplayer];
scorearray = new int[numplayer][3];
for (int i = 1; i<=numplayer;i++){
System.out.print("Please enter player "+ i +" name: ");
Scanner temp = new Scanner(System.in);
String name = temp.nextLine();
playerarray[i-1]= name;
scorearray[i-1][0]= startvalue;
}
}
public void calldice(int roll){
roll = Tdice.getdice();
System.out.print(roll);
}
public static void game(){
dice1 = Tdice.getdice();
dice2 = Tdice.getdice();
int smallest = 99;
//testing only System.out.println(dice1);
//testing only System.out.println(dice2);
int total = dice1 + dice2;
//User guessing
boolean check = false;
for (int i = 1; i<=numplayer;i++){
while (check == false){
System.out.print(playerarray[i-1] + ", what number do you think it would be :");
Scanner num = new Scanner(System.in);
String checkifint = num.next();
try {
int guess = Integer.parseInt(checkifint);
scorearray[i-1][1]= guess;
check = true;
}
catch(NumberFormatException nFE) {
System.out.println("Please enter a correct value!");
}
continue;
}
}
//Calculating the difference
for (int i = 1; i<=numplayer;i++){
int diff = 99;
if (scorearray[i-1][1]>total){
diff = scorearray[i-1][1] - total;
if (diff<smallest){
smallest = diff;
}
scorearray[i-1][2] = diff;
}
if (scorearray[i-1][1] == 0){
scorearray[i-1][2] = 0;
if (diff<smallest){
smallest = diff;
}
}
if (scorearray[i-1][1]<total){
diff = total - scorearray[i-1][1];
if (diff<smallest){
smallest = diff;
}
scorearray[i-1][2] = diff;
}
}
//Identify the winner
int wincount = 0;
for (int i = 1; i<=numplayer;i++){
if (scorearray[i-1][2] == smallest){
wincount=wincount+1;
}
}
int dollar = bet*numplayer / wincount;
int ndollar = (bet*numplayer) / (numplayer-wincount);
//Add subtract money
for (int i = 1; i<=numplayer;i++){
if (scorearray[i-1][2] == smallest){
scorearray[i-1][0] = scorearray[i-1][0] + dollar;
}
if (scorearray[i-1][2] != smallest){
scorearray[i-1][0] = scorearray[i-1][0] - ndollar;
}
}
//Show winner
System.out.println("---------------------------------------------");
System.out.println("The rolled dice is " + total + ".");
if (wincount<=1){
System.out.println("There is "+ wincount +" winner.");
}
if (wincount>1){
System.out.println("There is "+ wincount +" winners.");
}
//Preview money
System.out.println("=============================================");
System.out.println("Each Winners win "+ dollar + "!");
for (int i = 1; i<=numplayer;i++){
System.out.println(playerarray[i-1] + " - $" + scorearray[i-1][0]);
}
}
public static void main(String[] args){
//Get number of players
boolean check = false;
while (check == false){
System.out.print("Please enter the number of players in the game: ");
Scanner num = new Scanner(System.in);
String checkifint = num.next();
try {
numplayer = Integer.parseInt(checkifint);
check = true;
}
catch(NumberFormatException nFE) {
System.out.println("Please enter a correct value!");
}
}
//Do advance preset
preset = false;
/*System.out.println("Advance Control?");
Scanner temp = new Scanner(System.in);
preset = temp.nextBoolean();
System.out.println(preset);*/
if (preset == true){
System.out.print("Please enter the starting bet: ");
Scanner val = new Scanner(System.in);
startvalue = val.nextInt();
}
addplayer(); //Create users
game();
}
}
Here is my Tdice file -
import java.util.Random;
public class Tdice {
public static int getdice(){
int roll;
Random generator = new Random();
roll = generator.nextInt(6) + 1;
return roll;
}
}
For the number of player, if I type in 3, it only ask me for the first player, WHY? Please help.
Sorry if my code is too messy...