gpt4 book ai didi

java - 如何使用相同的扫描器变量读取字符串的 int、double 和句子

转载 作者:搜寻专家 更新时间:2023-11-01 03:58:53 25 4
gpt4 key购买 nike

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

因为 Scanner 对象将读取其先前读取停止的行的其余部分。

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

双重声明后,你必须写:scan.nextLine();

关于java - 如何使用相同的扫描器变量读取字符串的 int、double 和句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32948425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com