如何用Java输入一个句子

时间:2016-06-25 11:34:01

标签: java

我编写的代码只输入一个字符串,而不是整个句子,我希望将整个句子作为输入:

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);
    }
}

测试用例是"欢迎来到Java"它只是展示了#34;欢迎"在输出中。其他一切都很好。请帮忙。

7 个答案:

答案 0 :(得分:8)

您可以使用scan.nextLine();阅读整行。

答案 1 :(得分:2)

您可以尝试以下操作,它将起作用。

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);
  }

答案 2 :(得分:1)

尝试下面的代码,这是有效的代码,我尝试使用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);
    }
}

答案 3 :(得分:0)

您应该在上述整数扫描和两次扫描之后放置scan.nextLine()。然后使用String s = scan.nextLine()。像这样

int i = scan.nextInt();
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();

答案 4 :(得分:0)

在您的代码中,您正在使用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);
    }
}

但是在上面的代码(问题)中,您需要输入三个信息。您在nextLine()之后使用nextDouble()。 在这种情况下,nextLine()将输入读取为双重输入的换行符。 因此输出String为空。为避免这种情况,请在nextLine()之后使用另一个nextDouble()。此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);
    }
}

谢谢...!

答案 5 :(得分:0)

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);
   }
}

答案 6 :(得分:0)

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);
    }
}