문제

Now this code works absolutely fine in one machine but in another just simply refuses to output the correct result.

It is a simple Initials output request. I have no queries with the code but my question is why is it outputting numbers instead of letters?

When running the code from my laptop using Eclipse(Kepler) no issues and I get letters. If i use one of the desktops available to me and the same version of Eclipse I get an integer as a result. It may be settings but I just cant figure out why. And rewriting the code makes no change to the output

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);
도움이 되었습니까?

해결책

Try this:

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

I think this shouldn't happen but it smells like char addition. For example:

char a='a';
char b='b';
System.out.println( a+b ); ==> 195
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top