当前位置: 首页 > 知识库问答 >
问题:

无法得到双倍值来进行数学运算

赫连明诚
2023-03-14

我对Java还是一个相当陌生的人,但我在这方面花了很多时间,最终我找不到一种方法来解决数学问题/转换器。当我输入weight3时,它的转换器会工作,但是weight和weight2永远不会通过。谢谢你的帮助。

import java.util.Scanner;

public class TugTeam {

    public static void main(String[] args) {

        // Create Scanner Object

        Scanner input = new Scanner(System.in);

        // Name of team

        System.out.print("The name of the team is: ");
        String name = input.nextLine();

        // Information for first person

        System.out.print("Enter the name of member 1: ");
        String member = input.nextLine();
        System.out.print("Enter the weight of member 1 in lbs (must be in decimal value): ");
        double weight = input.nextDouble();

        // Information for second person

        String person2 = input.nextLine();

        System.out.print("Enter the name of member 2: ");
        String member2 = input.nextLine();
        System.out.print("Enter the weight of member 2 in lbs (must be in decimal value): ");
        double weight2 = input.nextDouble();

        // Information for third person, truncating the Scanner after an integer
        // Otherwise the program outputs both the nextDouble and the nextLine

        String person3 = input.nextLine();

        System.out.print("Enter the name of member 3: ");
        String member3 = input.nextLine();
        System.out.print("Enter the weight of member 3 in lbs (must be in decimal value): ");
        double weight3 = input.nextDouble();

        // Output of team members names

        System.out.println("Team " + name + " Members: " + member + ", " + member2 + ", and " + member3);

        // Converter from LBS to KG

        double kilo = weight + weight2 + weight3 * 0.454;

        if (kilo > 272.16) {
            System.out.printf("The total weight of the team is %5.2f kgs and is over the maximum", kilo);
        } else {
            System.out.printf("The total weight of the team is %5.2f kgs and is under the maximum", kilo);
        }
    }
}

共有1个答案

严元白
2023-03-14

您使用的是< code>input.nextDouble(),但是您的输入为第一个人提供了像< code>100 lbs这样的整数,为第二个人提供了< code>0的整数,为第三个人提供了< code>0的整数。

确保至少输入浮点值,如 100.00.0

或者改进您的代码以读取任何字符串,然后尝试将其转换为double,并在不工作的情况下打印一个错误。这会对用户更加友好。

 类似资料:
  • 本文向大家介绍Windows Powershell 进行数学运算,包括了Windows Powershell 进行数学运算的使用技巧和注意事项,需要的朋友参考一下 PowerShell支持如下算术运算符: 运算符 描述 例子 结果 + 把两个数值相加 6+2  8 - 把两个数值相减 6-2  4 - 将数值转换为对应的负值 -2+6 4 * 把两个数值相乘  6*2 12 / 把两个数值相除 6

  • 谁能告诉我如何在SWIFT中舍入一个双倍值到小数点位数的x位? 我有: 当我打印时,如何将其舍入到1.543?

  • 我有一个jUnit测试断言两个双重对象与以下: 这很好,然后我决定将其改为使用原语double,结果发现它已被弃用,除非还提供了delta。 所以我想知道的是,在这个assertEquals中使用双对象或原语类型有什么区别?为什么使用没有增量的对象是可以的,但是使用没有增量的原语是不可取的?Java是否在后台做了一些已经考虑了默认增量值的事情? 谢谢

  • 我怎么可能只有前5个数字? 我尝试过不同的方法,比如把数字乘以10和x的幂,但是x似乎只影响小数点,设置为x小数点

  • 在我的应用程序中,我处理的货币需要最多两个小数点位数,我使用了数字格式化程序来显示最多两个小数点位数,但当我想要这个双倍值像0.7到0.70时,我不能这样做,键入double总是给我0.7但我想要0.7到0.70

  • 今天,我正在浏览一些C代码(由其他人编写),并找到了这一部分: 我试图弄清楚这是否有意义。 < code>epsilon()的文档说明: 该函数返回1与大于1的最小值之间的差值,该最小值可[由双精度]表示。 这也适用于 0 吗,即 是大于 0 的最小值?还是有 到 之间的数字可以用表示? 如果不是,那么比较不就等于< code>someValue == 0.0吗?