我写的代码只是输入一个字符串,而不是整个句子,我想把整个句子作为输入:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i;
i= scan.nextInt();
double d;
d=scan.nextDouble();
String s;
s=scan.next();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Run Code Online (Sandbox Code Playgroud)
测试用例是"欢迎使用Java",它只是在输出中显示"欢迎".其他一切都很好.请帮忙.
小智 7
您可以尝试以下方法,它会起作用。
public static void main(String args[]) {
// Create a new scanner object
Scanner scan = new Scanner(System.in);
// Scan the integer which is in the first line of the input
int i = scan.nextInt();
// Scan the double which is on the second line
double d = scan.nextDouble();
/*
* At this point, the scanner is still on the second line at the end
* of the double, so we need to move the scanner to the next line
* scans to the end of the previous line which contains the double.
*/
scan.nextLine();
// reads the complete next line which contains the string sentence
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5960 次 |
| 最近记录: |