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...

It is because you set check to true.

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

you are checking if you can convert it and then you go back to the while loop and then the condition is false, so the loop only executes once.

you want to break out of the while loop and then set check to flase and then it will ask for all the player.

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!");
				}
				break; // break out of while
			}	
			check = false; // set condition to false so it executes for the next players
		}

If your indentation is right the continue on line 59 is the last statement in the while loop 47-60. If that's the case then it has no effect. What did you expect it to do?

@sirlink99:
I think the check logic was OK as it was (loop while !check, set check to true when good data has been received to exit loop). Your break on line 15 will ensure the loop is only executed once, regardless of data correct/incorrect.
However, I think you HAVE got a good point about re-initialising check. After it's set true for the first player it never gets set back to false, so subsequent players never execute the while loop. It should be initialsied INSIDE the players loop, ie swap lines 45/46 in the original code

True. you can leave out the break; in the while loop.

commented: starting to post answers +1 +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.