0% found this document useful (0 votes)
8 views1 page

7.1 Bulls and Cows Source Code

The document contains a Java program for the Bulls and Cows game, where players guess a hidden two-digit number. The game provides feedback on the guesses using 'B' for correct digits in the right place and 'C' for correct digits in the wrong place. The program continues to prompt the player for guesses until they correctly identify the hidden number.

Uploaded by

alarcon17722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

7.1 Bulls and Cows Source Code

The document contains a Java program for the Bulls and Cows game, where players guess a hidden two-digit number. The game provides feedback on the guesses using 'B' for correct digits in the right place and 'C' for correct digits in the wrong place. The program continues to prompt the player for guesses until they correctly identify the hidden number.

Uploaded by

alarcon17722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Random;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Bulls and Cows game!");
System.out.println("This is a logical mind games" +
"\nHere B means one of your guessing numbers is inside the hidden
number and in the right place" +
"\nHere C means one of your guessing numbers is inside the hidden
number but in the wrong place");
Random random = new Random();
int intRandom = random.nextInt(99);//66---100

while (intRandom < 11) {


intRandom++;
}
String s = String.valueOf(intRandom);
char charRandom1 = s.charAt(0);
char charRandom2 = s.charAt(1);
while (true) {

if (charRandom1 == charRandom2) {
charRandom1++;
}
System.out.println("Enter your guess");
String strRandom = scanner.nextLine();

int input = Integer.parseInt(strRandom);


if (input < 10) {
System.out.println("You entered one level number,please enter two
level number");
continue;
}
char charInput = strRandom.charAt(0);
char charInput2 = strRandom.charAt(1);

if (input == intRandom) {
System.out.println("Congratulations");
return;
} else if (charRandom1 == charInput || charRandom2 == charInput2) {
System.out.println("1B,OC");
} else if (charRandom1 == charInput2 && charRandom2 == charInput) {

System.out.println("0B,2C");
} else if (charRandom1 == charInput2 || charRandom2 == charInput) {
System.out.println("0B,1C");
} else {
System.out.println("0B,0C");
}
}
}
}

You might also like