Java 如何使用同一扫描变量读取字符串的int、double和句子

Java 如何使用同一扫描变量读取字符串的int、double和句子,java,java.util.scanner,Java,Java.util.scanner,它只扫描了一个词,请给出任何建议 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=new String(); int x=sc.nextInt(); double y=s

它只扫描了一个词,请给出任何建议

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String s=new String();
            int x=sc.nextInt();
            double y=sc.nextDouble();
            s = sc.next();

            System.out.println("String:"+s);
            System.out.println("Double: "+y); 
            System.out.println("Int: "+x);     
     }       
}
它只扫描了一个词,请给出任何建议

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String s=new String();
            int x=sc.nextInt();
            double y=sc.nextDouble();
            s = sc.next();

            System.out.println("String:"+s);
            System.out.println("Double: "+y); 
            System.out.println("Int: "+x);     
     }       
}
这就是
next()是用来做的。如果你想读多个单词,最简单的方法就是读一行文字。e、 g

s = sc.next();
如果你需要阅读多行文字,你需要制定一个这样做的策略,比如阅读循环中的所有内容,直到你有一个空行

注意:虽然答案与之类似,但区别在于您不丢弃行的其余部分,而是使用
nextLine()返回的字符串

如果您希望输入是

String s = sc.nextLine();
然后你想使用上面的建议,但是如果输入是在多行上,像这样

 1 2.0 Hello World

正如上面的链接所建议的那样,您需要使用一个额外的类将行的其余部分丢弃到
nextLine()

我建议您尝试这段代码

 1 2.0
 Hello World
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        double y = sc.nextDouble();
        sc.nextLine();
        String s = sc.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + y);
        System.out.println("Int: " + x);
    }
}
我希望这不会在双精度或整数输入后跳过读取字符串输入(或仅读取跳过行其余部分的单个单词)

在fb/ayur.sharma上跟随我

如果在nextInt()方法之后立即使用nextLine()方法,请记住nextInt()读取整数标记;因此,该整数输入行的最后一个换行符仍在输入缓冲区中排队,下一个nextLine()将读取整数行的其余部分(为空)

在读取输入标记和读取整行输入之间切换时,需要再次调用nextLine(),因为Scanner对象将读取其先前读取停止的行的其余部分

如果行上没有任何内容,它只会使用换行符并移动到下一行的开头


在双重声明之后,您必须编写:scan.nextLine()

您可以尝试以下代码:

import java.util.Scanner;

public class Solution 
{
    public static void main(String[] args) 
    {
     Scanner scan = new Scanner(System.in);
     int i = Integer.parseInt(scan.nextLine());
     double d = Double.parseDouble(scan.nextLine());
     scan.nextLine();
     String s = scan.nextLine();
     scan.close();

     System.out.println("String: " + s);
     System.out.println("Double: " + d);
     System.out.println("Int: "+ i);
     }
}
因为Scanner对象将读取其先前读取停止的行的其余部分

如果行上没有任何内容,它只会使用换行符并移动到下一行的开头


在双重声明之后,您必须编写:
scan.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.Scanner;

public class Solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    double d = scan.nextDouble();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    scan.nextLine();
    String s = scan.nextLine();

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
}

hasNextInt
hasNextDouble()
探索这些方法。@keppil它真的是重复的吗?@FastSnail和keppil我不认为它是重复的操作没有使用
nextLine()
anywhere@singhakash投票赞成reopen@PeterLawrey你能重新打开这个问题吗,因为它不是给定线程的复制品,由于您是删除重复注释的人之一,您可以在答案中添加注释,以避免出现以下问题:)。如果改为使用
nextLine
,那么他的输入将被跳过。@Tom谢谢,我已经添加了一条注释,说明为什么它是相似的,但不是重复的。@Tom你可能是对的,从OP的问题中不清楚字符串是在我假设的同一行上还是在不同的行上。对的,这可能是真的。。。从未想过像“11.0测试blub测试”(在同一行)这样的输入。@Tom worth为每种可能性添加了一条注释。谢谢。请输入int,double和句子。打印与输出相同出于所有合理的原因,它不起作用!您必须检查Arun的答案,以获得有效的解决方案!
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    double d = scan.nextDouble();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    scan.nextLine();
    String s = scan.nextLine();

    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 i = scan.nextInt();         
    double d = scan.nextDouble(); 

    scan.nextLine();
    String s = scan.nextLine();  

    //if you want to skip the unnecessary input
    //scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    System.out.println("String: " + s);       
    System.out.println("Double: " + d);      
    System.out.println("Int: " + i);

   scan.close();
}