超仔玩游戏
2020-05-0512:36
游戏开发java中int可以用汉字吗?简单试一下不就知道了,今天用的Eclipse版本是2020-03 (4.15.0),编写代码如下:
public class CalikoTest {
public static void main(String[] args) {
float operand1 = 10;
float operand2 = 10;
int operation = '战';
if (operation == '战') {
// 简单示例、并非真正游戏内战斗算法公式
System.out.println(operand1 + " + " + operand2 + " =");
System.out.println(operand1 + operand2);
} else if (operation == '暴') {
System.out.println(operand1 + " - " + operand2 + " =");
System.out.println(operand1 - operand2);
} else if (operation == '奶') {
System.out.println(operand1 + " * " + operand2 + " =");
System.out.println(operand1 * operand2);
} else {
System.out.println(operand1 + " / " + operand2 + " =");
System.out.println(operand1 / operand2);
}
}
}
程序成功运行,但一个汉字占用2个字节,int只支持一个汉字。其实在以上程序中,string被自动转换为int进行比较,在这里用short、long、char、float、double也可以。
最后,看来在java八大基本类型中,除了一个字节的byte和JVM相关的boolean外,至少是都可以使用一个常见汉字的。
虽然在游戏程序设计里这并不是一种规范的良好习惯,但给我们提供了一种独特的方式,你可以发现程序初学者和大神有些都是很具有个性的 :)