繁体   English   中英

使用 Scanner 类打印字符串

[英]print string by using Scanner class

我给出这个输入“欢迎来到 HackerRank 的 Java 教程!” 进入但

通过扫描仪类仅打印“欢迎”字符串。

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();
        String s = scan.nextLine();
        scan.close();

        // Write your code here.

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

如何解决这个问题?

关键是您的字符串在输入行中不包含整数和双精度值。

如果提供12 2.56 Welcome to HackerRank's Java tutorials! ,它将起作用:

Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
scan.close();

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

查看Java 演示

输出:

String:  Welcome to HackerRank's Java tutorials!
Double: 2.56
Int: 12

如果您想确保您的字符串被解析,请使用hasNext方法( hasNextInt()hasNextDouble() )检查下一个标记:

Scanner scan = new Scanner(System.in);
int i = 0;
if (scan.hasNextInt())
    i = scan.nextInt();
double d = 0d;
if (scan.hasNextDouble())
    d = scan.nextDouble();
String s = scan.nextLine();
scan.close();

请参阅此演示

请编写以下代码..它会工作!

Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
scan.close();

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

注意:如果在 nextInt() 或 nextDouble() [读取输入标记并读取整行输入] 方法之后立即使用 nextLine() 方法,请记住 nextInt() 或 nextDouble() 读取整数标记; 因此,该行整数或双精度输入的最后一个换行符仍在输入缓冲区中排队,下一个 nextLine() 将读取整数或双精度行的剩余部分。因此,您需要再次调用下一行()。

import java.util.Scanner;
class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("\nEnter The String= ");
        String s = scan.nextLine();
        System.out.println("\nEnter The int= ");
        int i = scan.nextInt();
        System.out.println("\nEnter The Double= ");
        double d = scan.nextDouble();   
        scan.close();

        // Write your code here.

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

在读取输入标记和读取整行输入之间切换时,您需要再次调用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;
        double d;
        String s;
       
        i = scan.nextInt();
        d = scan.nextDouble();
        scan.nextLine();
        s = scan.nextLine();
        scan.close();
        
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

暂无
暂无

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

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