简体   繁体   English

为什么如果我使用 int 会打印 ASCII 字符,而如果我使用变量则不会?

[英]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是一个Stringi是一个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;如果您尝试打印AZ范围内的字符值来代替 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.还不够——这实际上不会将数字转换为您想要的范围,因为从AZ的字符的数值实际上不是 0 到 25。

You need to write char randN = 'A' + rand;你需要写char randN = 'A' + rand; to effect the desired conversion.以实现所需的转换。

tl;dr tl;博士

Character.toString
( 
    "A".codePointAt( 0 ) 
    +
    ThreadLocalRandom.current().nextInt( 0 , 27 )
)

Or:或者:

Character.toString
( 
    ThreadLocalRandom.current().nextInt( 65 , ( 65 + 27 ) )
)

Start with AA开始

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。

Avoid 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是一个坏习惯,也是一种不必要的习惯。

Code points代码点

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.

相关问题 在java中将int转换为char-我必须使用ASCII吗? - Casting int to char in java- must I use ASCII? 我想打印这个,(+空白,并使用字符) - I want to print this! (+Blank, and use character) 为什么此代码显示ASCII值而不是字符 - Why does this code print the ASCII value instead of the character 为什么我要使用类变量(例如:static private int n),然后为它使用 getter 和 setter? - Why would I use a class variable (ex: static private int n) and then use a getter and a setter for it? 为什么我需要为Integer变量转换一个字符而不是为int转换? - Why do I need to cast a character for an Integer variable but not for an int? 我想在变量中存储0到100之间的数字-我应该使用INT还是BYTE,为什么? - I want to store a number between 0 and 100 in a variable - should I use an INT or a BYTE and why? 为什么FileReader和FileWriter使用INT变量来读写? - Why does FileReader and FileWriter use an INT variable to read and write from? 为什么ByteArrayOutputStream使用int? - Why does ByteArrayOutputStream use int? 为什么我不允许在Java源代码文件中使用字符as作为变量名? - Why am I not allowed to use the character 𝄞 in a Java source code file as a variable name? 如何在不使用BOM且以非ASCII字符开头的情况下识别针对文件的不同编码? - How can I identify different encodings against files without the use of a BOM and beginning with non-ASCII character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM