现在这段代码在一台机器上运行得非常好,但在另一台机器上只是拒绝输出正确的结果。
这是一个简单的缩写输出请求。我对代码没有疑问,但我的问题是为什么它输出数字而不是字母?
当使用 Eclipse(Kepler)从我的笔记本电脑运行代码时没有任何问题,并且我收到了信件。如果我使用可用的桌面之一和相同版本的 Eclipse,我会得到一个整数作为结果。这可能是设置,但我就是不明白为什么。重写代码不会改变输出
import java.util.Scanner;
public class InitialHere {
public static void main(String[] args)
{
// TODO Auto-generated method stub
//Using input keyboard
Scanner kb = new Scanner(System.in);
String firstname,lastname;
//Requesting names
System.out.print("What is your first name?");
firstname = kb.nextLine();
System.out.print("What is your last name?");
lastname = kb.nextLine();
//Calculating the initials
char achar = firstname.charAt(0);
char bchar = lastname.charAt(0);
//Output
System.out.println("Your initials are " + achar + bchar);
最佳答案
试试这个:
System.out.println("Your initials are " + achar + "" + bchar);
我认为这不应该发生,但闻起来像添加了碳。例如:
char a='a';
char b='b';
System.out.println( a+b ); ==> 195
关于java - charAt 产生不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22687028/