如果我运行这个简单的代码,我得到的输出是一个整数值,有人知道为什么吗? 我正在尝试打印名字的第一个字符和姓氏的前五个字符连接在一起。
Scanner scan = new Scanner (System.in);
String firstName, lastName;
System.out.print ("Enter First Name: ");
firstName = scan.nextLine();
System.out.print ("\n");
System.out.print ("Enter Last Name: ");
lastName = scan.nextLine();
System.out.println (firstName.charAt(0)+lastName.charAt(0));
最佳答案
当你打电话时
System.out.println (firstName.charAt(0)+lastName.charAt(0));
你最终调用了System.out.println(int)由于添加字符值本身会产生一个整数值。
I'm trying to print first character of the first name and the first five character of the last name concatenated together.
你可以使用:
System.out.println(firstName.charAt(0) + lastName.substring(0, 5));
关于java - 在 System.out.println 中使用 charAt 显示整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13735550/