为什么char值会在输出期间转换?

时间:2014-09-26 19:40:38

标签: java

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

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只眼睛)

run:
Which character should I use for the eyes: @
192
@@@
@@@

我找不到原因,有谁能看到我在这里做错了什么?

2 个答案:

答案 0 :(得分:9)

字符算术!

咨询此table@是字符64

typeOfEyes + typeOfEyes + typeOfEyes + "\n"

第一行实际上是将字符的值加起来(64 + 64 + 64)= 192,然后用换行符加数,所以得到192\n

编译器选择添加它们而不是创建字符串。解决这个问题的简单方法是在前面加上一个空字符串:"" + typeOfEyes...

基本上,编译器是“愚蠢的”。当我们向字符串添加整数时,"foo" + 123编译器可以将其解释为foo123,因为它将第一个元素识别为String。但是,我们已经定义了char,它是表示字符的numeric类型。所以编译器用它做数学运算。即使我们不应该。添加字符串文字告诉它我们实际上想要文本。

答案 1 :(得分:1)

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
@@@
@@@
@@@