在java中使用扫描选项时无法读取完整字符串
当我把输入作为 4. 4 世界这是我用java编写的第一个程序 此代码中使用的scan.nextLine()不读取字符串的值。在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 {
我还尝试使用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
和你好世界你的问题是什么?这太模糊了Robby Cornelissen我希望输出为8,8.0和hello World!这是我用java编写的第一个程序,不是8、8.0和hello World!我的问题是scan.next()没有读取整个字符串,scan.nextLine()也没有读取一个单词。请解释建议的改进,而不是让读者比较两个列表,并尝试了解更改本身的意义。
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);
}
}