2017-05-26 47 views
-2

嗨,我們共同的初學者,當我們做java的時候在課堂上迷失了這是我的代碼,但它不會閱讀者名姓氏和位置從標準輸入中讀取一個人的姓氏,他們的名字和他們的家庭位置 - 每個人都在一條獨立的線路上

import java.util.Scanner; 

public class lab1 { 

    public static void main(String[] args) { 

     String firstName,lastName,location; 


     //Create scanner to obtain user input 
     Scanner scanner1 = new Scanner(System.in); 

     //obtain user input 
     System.out.println("Enter your first name: "); 
     firstName = scanner1.nextLine(); 

     System.out.println("Enter your last name: "); 
     lastName = scanner1.nextLine(); 

     System.out.println("Enter your location: "); 
     lastName = scanner1.nextLine(); 

     //output information 
     System.out.print("Hello + firstName + " + "lastName + "location) 
    } 
} 
+1

此代碼不編譯。 ''System.out.print''語句有語法錯誤。這是你的問題嗎? – f1sh

+0

我不認爲這將甚至編譯爲你最後的'sysout'有錯誤 –

+0

對不起,我只是想讓它工作我曾經去過,但以某種方式搞砸了,但它只是讀取你好firtname姓氏位置,而不是我輸入的名稱 – willz

回答

0

它確實有輕微的修改,對於位置你又回到分配給lastName的

System.out.println("Enter your location: "); 
lastName = scanner1.nextLine(); 

稍作修改後,我們可以看到下面的控制檯輸出

public static void main(String[] args) { 

     String firstName,lastName,location; 


     //Create scanner to obtain user input 
     Scanner scanner1 = new Scanner(System.in); 

     //obtain user input 
     System.out.println("Enter your first name: "); 
     firstName = scanner1.nextLine(); 

     System.out.println("Enter your last name: "); 
     lastName = scanner1.nextLine(); 

     System.out.println("Enter your location: "); 
     location = scanner1.nextLine(); 

     System.out.println(firstName + " " + lastName + " " + location); 

    } 

控制檯輸出

Enter your first name: 
Fairoz 
Enter your last name: 
Matte 
Enter your location: 
Bangalore 
Fairoz Matte Bangalore 
+0

你會怎麼說它來自班加羅爾的fairoz啞光? – willz

+0

在CONSOL中運行程序輸入詳細信息。 – Fairoz

相關問題