在java中使用扫描选项时无法读取完整字符串

在java中使用扫描选项时无法读取完整字符串,java,Java,当我把输入作为 4. 4 世界这是我用java编写的第一个程序 此代码中使用的scan.nextLine()不读取字符串的值。 我还尝试使用scan.next(),它只能读取到空格(即只有一个单词)如果您必须读取多单词字符串,可以这样做: import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution {

当我把输入作为 4. 4 世界这是我用java编写的第一个程序

此代码中使用的scan.nextLine()不读取字符串的值。
我还尝试使用scan.next(),它只能读取到空格(即只有一个单词)

如果您必须读取多单词字符串,可以这样做:

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();
     }
}
String c=”“;
字符串输入[]=scan.nextLine().split(“”);
a=整数.parseInt(输入[0]);
b=Double.parseDouble(输入[1]);
对于(int j=2;j
这对您很有用

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);
类解决方案{
公共静态void main(字符串[]rat){
int i=4;
双d=4.0;
字符串s=“hello”;
字符串c=“”;
扫描仪scn=新扫描仪(System.in);
字符串输入[]=scn.nextLine().split(“”);
整数a=Integer.parseInt(输入[0]);
Double b=Double.parseDouble(输入[1]);
对于(int j=0;j
我不知道你的意思。对我来说很好。它打印出
8
8.0
你好世界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);  

    }
}