[英]Why does it print an ASCII character if I use an int, but not if I use a variable?
The following code prints A
to the console:以下代码A
打印到控制台:
char ch = (char)65.25;
System.out.println(ch);
However I don't understand why the next piece of code doesn't print the ASCII character?但是我不明白为什么下一段代码不打印ASCII字符?
int rand = (int)currentSeconds % 26;
System.out.println(" the number is " + rand);
char randN = (char)rand;
System.out.println(randN);
Below follows the full code:下面是完整的代码:
System.out.println(System.currentTimeMillis());
System.out.println(" using current second to generate random Uppercase letter");
long totalsecs = System.currentTimeMillis()/1000;
long currentSeconds = totalsecs % 60;
long totalMinute = totalsecs/60;
long currentMinute = totalMinute%60;
long totalHours = totalMinute/60;
long currentHours = totalHours%24;
System.out.println("current time is " +currentHours +":"+ currentMinute +":"+currentSeconds);
int rand = (int)currentSeconds % 26;
System.out.println(" the number is " + rand);
char randN = (char)rand;
System.out.println(randN);
There are many versions of the println
method, with different parameter types. println
方法有许多版本,具有不同的参数类型。 In other words, if s
is a String
, i
is an int
and c
is a char
, then System.out.println(i)
is actually calling a different method from System.out.println(c)
, which is calling a different method from System.out.println(s)
.换句话说,如果s
是一个String
, i
是一个int
并且c
是一个char
,那么System.out.println(i)
实际上调用的是与System.out.println(c)
不同的方法,后者调用的是不同的方法来自System.out.println(s)
方法。 It's the compiler's job to figure out which method you're trying to call, and it takes into account the type of the expression that you pass in. So找出您要调用的方法是编译器的工作,它会考虑您传入的表达式的类型。所以
System.out.println(rand)
calls the int
version of the method, which prints the number. System.out.println(rand)
调用打印数字的方法的int
版本。System.out.println(randN)
calls the char
version of the method, which prints the character, rather than its numeric equivalent. System.out.println(randN)
调用该方法的char
版本,它打印字符,而不是它的等效数字。System.out.println("the number is " + rand)
calls the String
version of the method, which prints a String
, and in this case, the String
is made by concatenating the number to the end of a String
literal. System.out.println("the number is " + rand)
调用该方法的String
版本,它打印一个String
,在这种情况下, String
是通过将数字连接到String
文字的末尾而构成的。 If you're trying to print a character value in the range A
to Z
in place of a number from 0 to 25, then writing char randN = (char) rand;
如果您尝试打印A
到Z
范围内的字符值来代替 0 到 25 之间的数字,则编写char randN = (char) rand;
isn't enough - that won't actually convert the number to the range you want, because the numeric values of the characters from A
to Z
are not actually 0 to 25.还不够——这实际上不会将数字转换为您想要的范围,因为从A
到Z
的字符的数值实际上不是 0 到 25。
You need to write char randN = 'A' + rand;
你需要写char randN = 'A' + rand;
to effect the desired conversion.以实现所需的转换。
Character.toString
(
"A".codePointAt( 0 )
+
ThreadLocalRandom.current().nextInt( 0 , 27 )
)
Or:或者:
Character.toString
(
ThreadLocalRandom.current().nextInt( 65 , ( 65 + 27 ) )
)
A
从A
开始As others pointed out, to get a random character from AZ, you need to add 0-25 to a starting number, that number being the code for A
which is 65.正如其他人指出的那样,要从 AZ 中获取随机字符,您需要将 0-25 添加到起始数字,该数字是A
的代码,即 65。
char
避免char
Another problem with your code is its use of the char
.您的代码的另一个问题是它对char
的使用。 The char
type in Java is legacy, essentially broken. Java 中的char
类型是遗留的,基本上已损坏。 As a 16-bit value, that type cannot represent most characters.作为 16 位值,该类型不能表示大多数字符。
While your particular case of AZ works with char
, using char
is a bad habit, and an unnecessary habit.虽然您的 AZ 特定情况与char
一起使用,但使用char
是一个坏习惯,也是一种不必要的习惯。
Instead learn to use Unicode code point integer numbers.相反,学习使用 Unicode代码点integer 数字。 Unicode is a superset of US-ASCII, so code points 0-127 represent the same characters in both. Unicode 是 US-ASCII 的超集,因此代码点 0-127 在两者中表示相同的字符。
int codePointForA = "A".codePointAt( 0 ) ; // 65
int addend = ThreadLocalRandom.current().nextInt( 0 , 27 ) ; // ( inclusive , exclusive )
int randomCodePoint = ( codePointForA + addend ) ;
String randomCharacter = Character.toString( randomCodePoint ) ;
See code run live at IdeOne.com .查看在 IdeOne.com 实时运行的代码。
Z Z
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.