gpt4 book ai didi

java - 打印一些其他字符

转载 作者:行者123 更新时间:2023-12-01 04:18:35 24 4
gpt4 key购买 nike

import java.io.*;
class Test{
public static void main(String args[])throws IOException{
char a[]=new char[4];
DataInputStream in=new DataInputStream(System.in);
for(int i=0;i<4;i++)
a[i]=in.readChar();
System.out.println("Characters are : ");
for(int i=0;i<4;i++)
System.out.println(a[i]);
}
}

输出:

java Test
a
s
d
f
Characters are :




这里我读取了字符 a、s、d 和 f..但它打印了一些其他字符..

最佳答案

输入字符通过以下逻辑进行操作:

(char)((a << 8) | (b & 0xff))

参见readChar文档:

public char readChar() throws IOException

Reads an input char and returns the char value. A Unicode char is made up of two bytes. Let a be the firstbyte read and b be the second byte. The value returned is: (char)((a<< 8) | (b & 0xff))

This method is suitable for reading byteswritten by the writeChar method of interface DataOutput.

关于java - 打印一些其他字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19200279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com