2014-03-27 57 views
2

現在,這個代碼在一臺機器上工作絕對正常,但在另一臺機器中只是簡單地拒絕輸出正確的結果。charAt產生不同的輸出

這是一個簡單的Initials輸出請求。我對代碼沒有任何疑問,但我的問題是爲什麼它會輸出數字而不是字母?

當使用Eclipse(開普勒)從我的筆記本電腦運行代碼沒有問題,我收到了信件。如果我使用其中一個可用的桌面和相同版本的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); 
+0

準確的輸入和輸出是什麼? – Keerthivasan

+0

想象一下,輸入是湯姆沃爾頓,結果應該是TW。但它最終是171. – Snorrarcisco

+0

哦,真的,你有沒有sysout出'firstname'和'lastname'之前呢? – Keerthivasan

回答

3

沒有變化試試這個:

System.out.println("Your initials are " + achar + "" + bchar); 

我覺得這不應該發生,但它聞起來像炭除。例如:

char a='a'; 
char b='b'; 
System.out.println(a+b); ==> 195 
+0

這完美地工作非常感謝你。任何理由爲什麼它在某些機器上工作而不在其他人上?讓我有點困惑 – Snorrarcisco

+0

不是。我認爲這根本不應該發生,如果是這樣的話會是一個編譯器錯誤。 – Scheintod

+0

@Sororcisco我能想到的唯一原因是您的JVM版本可能在不同的機器上有所不同。 – Savv