2014-09-29 58 views
-6

我不知道爲什麼我的姓氏和名字在它們之間沒有空格的情況下打印。請幫忙!如何在名字和姓氏之間加上空格

import java.util.Scanner; 

public class LastNameFirstNameChapter3 
{ 
    public static void main(String[] args) 
    { 
    Scanner stdIn = new Scanner(System.in); 
    String firstName; // Users First Name 
    String lastName; // Users Last Name 
    String age;   // Users Age 

    System.out.print("Enter Your First Name: "); 
    firstName = stdIn.nextLine(); 
    System.out.println("Hello " +""+ firstName + "!"); 
    System.out.print("Please Enter Your Last Name: "); 
    lastName = stdIn.nextLine(); 
    System.out.println("Hello " + firstName +""+ lastName + "!"); 
    System.out.print("Enter Your Age: "); 
    age = stdIn.nextLine(); 
    } // end main 
}  // end class LastNameFirstNameChapter3 
+1

-1是'space'標籤。而且你從來沒有在'firstName'和'lastName'之間放置空格,只是一個空字符串。 – 2014-09-29 04:25:23

+2

你有沒有嘗試在引號之間放置一個空格? – 2014-09-29 04:25:33

+0

謝謝。骨頭錯誤。我是代號爲 – 2014-09-29 04:30:06

回答

2
import java.util.Scanner; 

public class LastNameFirstNameChapter3 
{ 
    public static void main(String[] args) 
    { 
    Scanner stdIn = new Scanner(System.in); 
    String firstName; // Users First Name 
    String lastName; // Users Last Name 
    String age;   // Users Age 

    System.out.print("Enter Your First Name: "); 
    firstName = stdIn.nextLine(); 
    System.out.println("Hello " +" "+ firstName + "!"); 
    System.out.print("Please Enter Your Last Name: "); 
    lastName = stdIn.nextLine(); 
    System.out.println("Hello " + firstName +" "+ lastName + "!"); 
    System.out.print("Enter Your Age: "); 
    age = stdIn.nextLine(); 
    } // end main 
} 
+0

的全新商品謝謝......幾乎只要我發佈這個問題,我就意識到我錯過了「空間」。 – 2014-09-29 04:29:39