2016-11-27 37 views
0

我的代碼是錯誤的字符串味精輸出都走到一起

import java.util.*; 
public class Friends 
{ 
    public static void main(String args[]) 
    { 
     Scanner in=new Scanner(System.in); 
     String f_name[]=new String[5]; 
     String l_name[]=new String[5]; 
     String l_concat[]=new String[5]; 
     int sr[]=new int[5]; 
     int i; 
     System.out.print("\f"); 
     for(i=0;i<3;i++) 
     { 
     System.out.print("Enter your Roll number: "); 
     sr[i]=in.nextInt();   
     System.out.println("Enter your first name: "); 
     f_name[i]=in.nextLine(); 
     System.out.println("Enter your last name: "); 
     l_name[i]=in.nextLine(); 
     l_concat[i]=f_name[i].concat (l_name[i]); 
     } 
     for(i=0;i<3;i++) 
     { 
     System.out.println("sr "+i+" first name: "+l_name[i]+" last name: "+f_name[i]); 
     } 
    } 

} 

兩個名字和姓氏輸出走到一起,請幫我看看怎麼可能由一個先到名味精來了一個然後輸入姓氏信息。

回答

0

改爲使用它。

public static void main(String args[]) { 
    Scanner in = new Scanner(System.in); 
    String f_name[] = new String[5]; 
    String l_name[] = new String[5]; 
    String l_concat[] = new String[5]; 
    int sr[] = new int[5]; 
    int i; 

    for (i = 0; i < 3; i++) { 
     System.out.print("Enter your Roll number: "); 
     sr[i] = in.nextInt(); 

     System.out.print("Enter your first name: "); 
     f_name[i] = in.next(); 

     System.out.print("Enter your last name: "); 
     l_name[i] = in.next(); 

     l_concat[i] = f_name[i].concat(l_name[i]); 
     System.out.print("\n"); 
    } 
    in.close(); 

    for (i = 0; i < 3; i++) { 
     System.out.println("sr " + i + " first name: " + l_name[i] + " last name: " + f_name[i]); 
    } 
} 

有關詳細信息,請檢查該線程。 Using scanner.nextLine()

+0

謝謝Avinash ............... – vijay