2016-09-25 4 views
-4
import java.io.*; 
import java.util.*; 
import java.text.*; 
import java.math.*; 
import java.util.regex.*; 

public class Solution { 

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

    Scanner scan = new Scanner(System.in); 
    int a; 
    double b; 
    String c; 

    /* Read and save an integer, double, and String to your variables.*/ 

    a = scan.nextInt(); 
    b = scan.nextDouble(); 
    c= scan.nextLine(); 

    /* Print the sum of both integer variables on a new line. */ 

    System.out.println(i+a); 
    /* Print the sum of the double variables on a new line. */ 
    System.out.println(b+d); 
    /* Concatenate and print the String variables on a new line; 
     the 's' variable above should be printed first. */ 
    System.out.println(s+c); 

    scan.close(); 
    } 
} 

をスキャンオプションを使用している間、完全な文字列を読み取ることができません!これは私の最初のJavaプログラムです私は 4.0 世界として入力を与えたときにJavaで

このコードで使用されているscan.nextLine()はstringの値を読みません。唯一のスペースまで読み 私も(scan.nextを使用してみました)(すなわち、1つの単語だけ)

+3

あなたは何を意味するのか分かりません。私のためにうまく動作します。 '8'、' 8.0'と 'hello World! 'を表示します。 –

+1

あなたの質問は何ですか?それはちょうどあまりにも曖昧です – codemirel

+0

Robby Cornelissen私は8、8.0とhello Worldとして出力をしたい!これは、Javaで私の最初のプログラムではない8、8.0とこんにちは世界!私の問題は、scan.next()が文字列全体を読み取っていないことと、scan.nextLine()が1つの単語も読み取っていないことです。 –

答えて

0

あなたがマルチワードの文字列を読み取るために持っている場合は、この方法を行うことができます:

String c = ""; 
String inputs[] = scan.nextLine().split(" "); 

a = Integer.parseInt(inputs[0]); 
b = Double.parseDouble(inputs[1]); 
for (int j = 2; j < inputs.length; j++) { 
    c += " " + inputs[j]; 
} 

System.out.println(i + a); 
System.out.println(b + d); 
System.out.println(s + c); 
-1

これがために動作しますあなた

class solution{ 

    public static void main (String []rat){ 

    int i = 4; 
    double d = 4.0; 
    String s = "hello "; 
    String c = ""; 

    Scanner scn = new Scanner(System.in); 
    String inputs[] = scn.nextLine().split(" "); 

    Integer a = Integer.parseInt(inputs[0]); 
    Double b = Double.parseDouble(inputs[1]); 
    for (int j = 0; j < inputs.length; j++)  
    { 
     c += " " + inputs[j]; 
    } 

    System.out.println(i + a); 
    System.out.println(b + d); 
    System.out.println(s + c); 

    } 
} 
+0

読者に両方のリスティングを比較させ、自分自身で変更の重要性を理解しようとするのではなく、提案された改善点について説明します。 – kryger

関連する問題