如何在Java中输入句子
我编写的代码只接受一个字符串而不是一个完整的句子作为输入,我希望将一个完整的句子作为输入:如何在Java中输入句子,java,Java,我编写的代码只接受一个字符串而不是一个完整的句子作为输入,我希望将一个完整的句子作为输入: import java.util.Scanner; 公共类解决方案{ 公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in); int i; i=scan.nextInt(); 双d; d=scan.nextDouble(); 字符串s; s=scan.next(); System.out.println(“字符串:+s”); System.out.println(
import java.util.Scanner;
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int i;
i=scan.nextInt();
双d;
d=scan.nextDouble();
字符串s;
s=scan.next();
System.out.println(“字符串:+s”);
System.out.println(“双精度:+d”);
System.out.println(“Int:+i”);
}
}
测试用例是“WelcometoJava”,它只是在输出中显示“Welcome”。其他一切都很好。请帮忙 您可以使用
scan.nextLine()
读取整行。您可以尝试以下方法,它将起作用
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);
}
您应该在上述整数扫描和双扫描之后放置一个
scan.nextLine()
。然后使用String s=scan.nextLine()
。像这样,
int i = scan.nextInt();
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
尝试下面的代码,这是工作代码,我尝试了IntelliJ和在线编译器,比如Hacker Rank
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d=scan.nextDouble();
scan.nextLine();
String s=scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
在代码中使用的是
next()
。这用于输入一个单词
您可以使用nextLine()
在java中输入句子
例如:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s;
s=scan.nextLine();
System.out.println("String: " + s);
}
}
但上面的代码(问题)您接受了三个输入。在nextDouble()
之后使用nextLine()
。
在这种情况下,nextLine()。
所以输出字符串是空的。要避免这种情况,请在nextDouble()
之后使用另一个nextLine()
。此nextLine()
读取双输入的换行符,然后nextLine()
读取句子
所以试试这个:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
谢谢你 使用scan.nextLine()
而不是scan.next()
它现在没有显示任何内容…请参阅Plz帮助。您的问题不包含测试用例。确保始终创建一个完整的,强调完整的。否则,我们帮不了那么多忙,正如您所看到的,一场讨论开始了。它现在显示了这一点:-很好的尝试,但您没有通过这个测试用例。Input(stdin)423.1415欢迎来到HackerRank的Java教程!您的输出(stdout)字符串:Double:3.1415 Int:42预期输出字符串:欢迎来到HackerRank的Java教程!双精度:3.1415英寸:42英寸i;i=scan.nextInt();双d;d=scan.nextDouble();它现在什么也没有显示……单词“Welcome”在前面显示过,但是在做了更改之后,它什么也没有显示——只是一个空格=scan.nextDouble();scan.next();s=scan.nextLine();当第三次读取字符串时,当扫描对象跳过字符串时,这将起作用,所以,scan.next()表示跳过,所以,next scan.nextLine()起作用。请添加几行注释,这将提高答案的可读性,这是一个很好的做法。您的答案格式良好,有意义。阅读此帮助可以获得更好的答案写作练习
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//For input a Integer
int i = scan.nextInt();
//For input a Double
double d = scan.nextDouble();
//For input a String
scan.nextLine(); //For using to get the input string that was skipped of the Scanner object.(Use it when scan a string after scanning different type of variables.)
String s = scan.nextLine();
//For print String, Double and Integer variable
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}