Java 为什么在输出过程中字符值会发生变化?

Java 为什么在输出过程中字符值会发生变化?,java,Java,我正在为JavaSE中的学校作业制作掷骰子(如骰子)的程序。用户可以放置一个字符作为标准输入,因此用户选择的字符将代表模具的眼睛。有时当我打印结果时,它会显示一个完全不同的字符 package ThrowADie; import java.util.Scanner; public class ThrowADie { public static void main(String[] args) { //Ask user for the char in which the dices

我正在为JavaSE中的学校作业制作掷骰子(如骰子)的程序。用户可以放置一个字符作为标准输入,因此用户选择的字符将代表模具的眼睛。有时当我打印结果时,它会显示一个完全不同的字符

package ThrowADie;
import java.util.Scanner;

public class ThrowADie {

public static void main(String[] args) {

    //Ask user for the char in which the dices eyes should be printed in.
    System.out.print("Which character should I use for the eye: ");

    //Allow user to place input in the eye variable
    Scanner input = new Scanner(System.in); //Make the stdinput object
    char eye = input.next().charAt(0);

    //Time to throw the die. Place result in dieResult
    int dieResult = throwDie();

    //Reveal of the result
    printDieResult(dieResult, eye);

}


    /*
    * Method name: throwDie()
    * Purpose: Picks a number from 1 to 6 randomly, like a die does
    * Parameters: N/A
    * Returns: Integer number from 1 to 6    
    */
    public static int throwDie(){
        int range = (6 - 1) + 1;     
        return (int)(Math.random() * range) + 1;
    }


    /*
    * Method name: printDieResult()
    * Purpose: Generate result of the die throw in ASCII art
    * Parameters: numberOfEyes, typeOfEyes
    * Returns: N/A
    */
    public static void printDieResult(int numberOfEyes, char typeOfEyes){
        if (numberOfEyes == 1){
            //Print art
            System.out.println(
                    " " + " " + " \n"
                  + " " + typeOfEyes + " \n"
                  + " " + " " + " ");
        } else if (numberOfEyes == 2){
            //Print art
            System.out.println(
                    typeOfEyes + " " + " \n"
                  + " " + " " + " \n"
                  + " " + " " + typeOfEyes);
        } else if (numberOfEyes == 3){
            //Print art
            System.out.println(
                    typeOfEyes + " " + " \n"
                  + " " + typeOfEyes + " \n"
                  + " " + " " + typeOfEyes);
        } else if (numberOfEyes == 4){
            //Print art
            System.out.println(
                    typeOfEyes + " " + typeOfEyes + "\n"
                  + " " + " " + " \n"
                  + typeOfEyes + " " + typeOfEyes);
        } else if (numberOfEyes == 5){
            //Print art
            System.out.println(
                    typeOfEyes + " " + typeOfEyes + "\n"
                  + " " + typeOfEyes + " \n"
                  + typeOfEyes + " " + typeOfEyes);
        } else {
            //Print art
            //Accidentally written down 9 eye representation
            System.out.println(
                    typeOfEyes + typeOfEyes + typeOfEyes + "\n"
                  + typeOfEyes + typeOfEyes + typeOfEyes + "\n"
                  + typeOfEyes + typeOfEyes + typeOfEyes);
        }
    }
}
输出 该程序将产生适当的结果。但偶尔输入的字符(代表模具的眼睛)会转换为数字

在以下情况下,程序应打印9'@'字符。相反,它在第一行打印192。(我知道骰子有6只眼睛,但我在无意中打印了9只眼睛时遇到了这个奇怪的输出)

我找不到原因,有人能看出我做错了什么吗?

字符算术

参考这个<代码>@是字符
64

typeofees+typeofees+typeofees+“\n”

第一行实际上是将字符(64+64+64)=192的值相加,然后用换行符将其相加,因此我们得到
192\n

编译器选择将这些字符相加,而不是创建字符串。解决这个问题的简单方法是在前面加一个空字符串作为序:
“”+typeOfEyes…


基本上,编译器是“哑的”。当我们将整数添加到字符串中时,
“foo”+123
编译器可以将其解释为
foo123
,因为它可以将第一个元素识别为字符串。但是,我们定义了一个
char
,它是一种表示字符的类型。所以编译器用它做数学运算。即使我们不应该这样。添加字符串文字说明我们实际上需要文本。

本质上,这些总和之间缺少一个字符串。这是一个很好的解释。我不得不说:谢谢!
int test = (int) (typeOfEyes + typeOfEyes + typeOfEyes);

        System.out.println("\n" + test + "\n"
                + typeOfEyes + "" + typeOfEyes + "" + typeOfEyes + "\n"
                + typeOfEyes + "" + typeOfEyes + "" + typeOfEyes + "\n"
                + typeOfEyes + "" + typeOfEyes + "" + typeOfEyes);

Which character should I use for the eye: @

192
@@@
@@@
@@@
int test = (int) (typeOfEyes + typeOfEyes + typeOfEyes);

        System.out.println("\n" + test + "\n"
                + typeOfEyes + "" + typeOfEyes + "" + typeOfEyes + "\n"
                + typeOfEyes + "" + typeOfEyes + "" + typeOfEyes + "\n"
                + typeOfEyes + "" + typeOfEyes + "" + typeOfEyes);

Which character should I use for the eye: @

192
@@@
@@@
@@@