你好我嘗試做一個字母猜謎遊戲有3個隨機字母,當我嘗試使用下面的代碼它總是返回一個int到其他程序波紋管我嘗試接受它作爲一個字母來獲得一個值,但可以」弄明白了。Interger正在通過,但期望的Char值?
import java.util.Random;
public class CodeLetter {
private char letterValue;
int count = 8;
Random rnd = new Random();
public char codeLetter(){
letterValue = (char)(rnd.nextInt(5)+'A');
System.out.println(letterValue);
return letterValue;
}
}
調用使用線
letter1 = codeLetter.codeLetter();
上面的代碼但一旦它被打印到屏幕它仍持有一個int值,而不是字符的代碼。 此程序需要兩個類的質量要求
public class CodeBreaker {
private char letter1;
private char letter2;
private char letter3;
CodeLetter codeLetter = new CodeLetter();
public void CodeBreaker(){
//Welcome Screen
System.out.println("Welcome to CODEBREAKER ");
System.out.println("you have 6 tries to guess the secret 3 letter code.");
System.out.println("The letters Range from A to E");
System.out.println("Goodluck");
System.out.println("The code has no repeat letters");
//end
letter1 = codeLetter.codeLetter();
letter2 = codeLetter.codeLetter();
letter3 = codeLetter.codeLetter();
System.out.println(letter1 + letter2 + letter3);
}
/*public boolean done(){
}
/*private boolean isValid*char){
//- Is the given letter valid?
}*/
public void getGuess(/*int*/){
//- Get guess #
}
public void checkGuess(){
//- Verify the guess
}
public void display(){
//- Display the secret code
}
}