java - 为什么 char 值在输出过程中会发生变化?

标签 java

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

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

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

最佳答案

字符算术!

咨询此table@ 是字符 64

typeOfEyes + typeOfEyes + typeOfEyes + "\n"

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

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

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

关于java - 为什么 char 值在输出过程中会发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26067136/

相关文章:

java - 如何使用Spring在Java中将一个属性使用到另一个属性中?

java - Mockito - 在单元测试中期望模拟时返回 null

java - 如何让消息出现在不同的侧面?

java - 如何在 Java 中使用 Selenium WebDriver 检查弹出窗口是否打开

java - 如何使用 util.regex.Matcher (java) 创建 Tokenizer?

java - 从不同的方法写入同一个文件

java - 如何在 Hibernate 应用程序中处理数据库空值?

java - 如何使用 jedis for Java 连接到 Docker Redis 集群实例?

java - 从Java代码检查CFS中是否存在某个文件

Java API 用于任何一年的印度教日历中假期日期的 Swing