gpt4 book ai didi

java - 我被这个计算器程序困住了,我没有得到正确的答案

转载 作者:行者123 更新时间:2023-12-01 19:28:18 25 4
gpt4 key购买 nike

我的编程类(class)希望我有这样的结局:

  • 输入第一个数字!
  • 9
  • 输入第二个数字!
  • 5
  • 9 + 5 = 14
  • 9 - 5 = 4
  • 9 * 5 = 45
  • 9/5 = 1.8 这就是问题所在,我编写的程序只给出 1.0 作为答案。我怎样才能让这个数字变成 1.8 而不是 1.0?

public class Nelilaskin {

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);


System.out.println("Enter the first number!");
int first = Integer.valueOf(reader.nextLine());
System.out.println("Enter the second number!");
int second = Integer.valueOf(reader.nextLine());
int plus = (first + second);
int minus = (first - second);
int multi = (first * second);
double division = (first / second * 1.0);

System.out.println(first + " + " + second + " = " + plus);
System.out.println(first + " - " + second + " = " + minus);
System.out.println(first + " * " + second + " = " + multi);
System.out.println(first + " / " + second + " = " + division);


}

}

最佳答案

考虑将第一个和第二个的数据类型替换为Float。并将结果也存储在浮点变量中,然后输出将是所需的。

float plus = (first + second);
float minus = (first - second);
float multi = (first * second);
float division = first / second;

关于java - 我被这个计算器程序困住了,我没有得到正确的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60705315/

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