2016-12-31 8 views
-1

私はこのハッカーの30日間のコードチャレンジを解決していました。以下のコードは次のとおりです。このコードで30日間のコードハッカーブランクday1

import java.util.*; 

public class jabcexample1 { 
    public static void main(String[] args) { 
     int i = 4; 
     double d = 4.0; 
     String s = "HackerRank "; 

     /* Declare second integer, double, and String variables. */ 
     try (Scanner scan = new Scanner(System.in)) { 
      /* Declare second integer, double, and String variables. */ 
      int i2; 
      double d2; 
      String s2; 

      /* Read and save an integer, double, and String to your variables.*/ 
      i2 = scan.nextInt(); 
      d2 = scan.nextDouble(); 

      scan.nextLine(); // This line 
      s2 = scan.nextLine(); 

      /* Print the sum of both integer variables on a new line. */ 
      System.out.println(i + i2); 

      /* Print the sum of the double variables on a new line. */ 
      System.out.println(d + d2); 

      /* Concatenate and print the String variables on a new line; 
      the 's' variable above should be printed first. */ 
      System.out.println(s.concat(s2)); 
     } 
    } 
} 

それなしでコンパイラがさえs2 = scan.nextLine();ある次の行を気づいていないので、私は余分なラインscan.nextLine();を追加しました。コンパイラがs2=scan.nextLine();に書き込みなしで気づかない理由scan.nextLine();

答えて

3

これは、コンパイラやScannerの動作に関するすべてとは関係ありません。

は、Javaのドキュメントを読めば、あなたがSanner.nextLineを(表示されます)は、現在の行過去

進歩このスキャナを行い、 がスキップされたことが入力を返します。このメソッドは、最後の行区切り文字を除いて、現在の行の残りの部分を返します。 位置は、 の次の行の先頭に設定されます。

今、あなたはそれがキャリッジリターン文字である。この場合、

i2 = scan.nextInt(); 
d2 = scan.nextDouble(); 

と呼ば後に残っているものを疑問に思うかもしれません。 scan.nextLine()を呼び出すと、それらの文字が読み込まれ、その位置が次の行の先頭に設定されます。

+0

ありがとうございました。私はキャリッジリターンキャラクターのコンセプトも見逃していました。 – ashishdhiman2007

関連する問題