gpt4 book ai didi

java - 在 Java 中没有得到预期的输出

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:07 25 4
gpt4 key购买 nike

输入格式

  • 第一行包含一个整数,。
  • 第二行包含一个 double ,.
  • 第三行包含一个字符串,.

输出格式

在第一行打印两个整数的总和,在第二行打印两个 double 的总和(缩放到小数位),然后在第三行打印两个连接的字符串。这是我的代码

package programs;

import java.util.Scanner;


public class Solution1 {

public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "Programs ";

Scanner scan = new Scanner(System.in);
int i1 = scan.nextInt();
double d1 = scan.nextDouble();
String s1 = scan.next();

int i2 = i + i1;
double d2 = d + d1;
String s2 = s + s1;
System.out.println(i2);
System.out.println(d2);
System.out.println(s2);
scan.close();
}
}

输入(标准输入)

12
4.0
are the best way to learn and practice coding!

你的输出(标准输出)

16
8.0
programs are

预期输出

16
8.0
programs are the best place to learn and practice coding!

最佳答案

Scanner.next()读取下一个 token 。默认情况下,空格用作标记之间的分隔符,因此您只会得到输入的第一个单词。

听起来你想读一整行,所以使用Scanner.nextLine() .不过,根据 this question,您需要调用一次 nextLine() 来消耗 double 之后的换行符.

// Consume the line break after the call to nextDouble()
scan.nextLine();
// Now read the next line
String s1 = scan.nextLine();

关于java - 在 Java 中没有得到预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39631518/

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