分配char时,整数出现在打印行中

时间:2017-09-26 23:11:43

标签: java

我正在尝试让打印行阅读"访问代码XXX:"通过使用条件运算符。但是,它只给了我"访问代码888888:"。我不确定如何正确格式化char以显示为" X"在命令行而不是" 88"。

public static final int MAX_GUESSES = 15;
public static void main(String[] args) {

    Random rng = new Random();
    Scanner input = new Scanner(System.in);

    int counter = 0;
    int digit1 = 5;
    int digit2 = 5;
    int digit3 = 5;
    int inputDigit1 = 12;
    int inputDigit2 = 12;
    int inputDigit3 = 12;
    int accessCode;
    char f = 'X';
    int x1;
    int x2;
    int x3;

    while(counter < MAX_GUESSES) {

        counter++;

        x1 = (inputDigit1 == digit1)?(digit1):f;
        x2 = (inputDigit2 == digit2)?(digit2):f;
        x3 = (inputDigit3 == digit3)?(digit3):f;

        System.out.print("Access Code " + x1 + x2 + x3 + ":");
        accessCode = input.nextInt();

        inputDigit1 = accessCode/100;
        inputDigit2 = (accessCode%100)/10;
        inputDigit3 = accessCode%10;

    }

}

0 个答案:

没有答案