繁体   English   中英

"如何在Java中输入一个句子"

[英]How to input a sentence in 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);
    }
}

你可以使用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() nextDouble() 在这种情况下, nextLine()将 input 作为double input的换行符读取。 所以输出字符串为空。 为了避免这种情况,在nextDouble()之后使用另一个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);
    }
}

谢谢...!

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

这将正常工作。

public static void main(String[] args) {
    
        Scanner scan = new Scanner(System.in);
        int c = scan.nextInt();
        Double b = scan.nextDouble();
        scan.nextLine();
        String a = scan.nextLine();
        System.out.println("String: "+a);
        System.out.println("Double: "+b);
        System.out.println("Int: "+c);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM