2016-04-10 4 views
0
import java.util.Scanner; 
import java.io.*; 

public class Solution { 

public static void main(String[] args) { 
     Scanner sc=new Scanner(System.in); 
     int x=sc.nextInt(); 

     Double y= sc.nextDouble(); 
     Scanner sc1=new Scanner(System.in); 
     String name = sc1.nextLine(); 


     System.out.println("String: "+name); 
     System.out.println("Double: "+y); 
     System.out.println("Int: "+x); 
} 
} 

入力:( 'ラム' の前に持っている5スペース)Javaでスキャナを使用して文字列の前にスペースを読み取る方法は?

343434343

343.434343

ram sdf

予想される出力:( 'ラム' の前に5のスペースを持っている)

ram sdf

343.434343

343434343

答えて

1

あなたは、2台のスキャナを作成する必要はありません。また、doubleを読んだらnextLine()する必要があります。ここで

は修正されたコードスニペットです:

public static void main (String[] args) throws Exception { 
    Scanner sc = new Scanner(System.in); 
    int x = sc.nextInt(); 
    Double y = sc.nextDouble(); 

    /* Note the change here */ 
    sc.nextLine(); 
    String name = sc.nextLine(); 

    System.out.println("String: " + name); 
    System.out.println("Double: " + y); 
    System.out.println("Int: " + x); 
} 

は出力:

String:  ram sdf 
Double: 343.434343 
Int: 343 
関連する問題