Skip to content

Instantly share code, notes, and snippets.

@diegogurgel
Last active January 23, 2018 17:40
Show Gist options
  • Save diegogurgel/3175f3f3f30c517d2b6eafda52878286 to your computer and use it in GitHub Desktop.
Save diegogurgel/3175f3f3f30c517d2b6eafda52878286 to your computer and use it in GitHub Desktop.
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Naya Maia
*/
public class GuessGameT {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int number;
int secretn[] = new int [4];
Random r = new Random();
int[] guess = new int [4];
int tries = 8;
int num = 0;
int position = 0;
for(int i=0; i<secretn.length; i++){
number = r.nextInt(10);
for(int j=0; j<secretn.length; j++){
if(number == secretn[j] && j !=i){
number = r.nextInt(10);
}else{
secretn[i] = number;}
}
} //SHOW THE NUMBER [APAGAR]
for(int i=0; i<secretn.length; i++){
System.out.println(secretn[i] + " ");
}
//STEP 2: Ask the player to input 4 numbers
boolean somethingWrong = true;
while(somethingWrong) {
somethingWrong = false;
Scanner input = new Scanner(System.in);
System.out.println("Guess a 4 digits number? (0-9)");
String userInput = input.next();
for(int i=0; i < guess.length; i++){
guess[i] = Character.getNumericValue(userInput.charAt(i));
}
for(int i = 0; i< secretn.length; i++){
for(int j = 0; j< secretn.length; j++){
if(secretn[i] == guess[j]){
if(i==j){
System.out.println("Number "+ guess[j]+" was found in the correct position "+ j);
}else{
System.out.println("Number "+ guess[j]+" was found in the wrong position ");
somethingWrong = true;
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment