显示时数学方程式结果丢失小数 [英] Math equation result loses decimals when displayed

查看:50
本文介绍了显示时数学方程式结果丢失小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用于上课的程序,该程序将允许用户计算等腰梯形的面积.这是我的代码:

  import java.util.Scanner;导入java.lang.Math;公共课程CSCD210Lab2{公共静态void主对象(字符串[] args){扫描仪mathInput =新的Scanner(System.in);//声明变量int topLength,bottomLength,height;//获取用户输入System.out.print(请输入等腰梯形顶部的长度:");topLength = mathInput.nextInt();mathInput.nextLine();System.out.print(请输入等腰梯形底部的长度:");bottomLength = mathInput.nextInt();mathInput.nextLine();System.out.print("请输入等腰梯形的高度:");高度= mathInput.nextInt();mathInput.nextLine();double trapArea =(((topLength + bottomLength)/2 *(height)));System.out.println();System.out.printf(等腰梯形的面积为:" + trapArea);}} 

如果我输入"topLength 2","bottomLength 7"和"height 3",我将得到12.0的答案,而答案应为13.5.有人知道我的代码为什么打印出错误答案而不打印.5吗?

解决方案

问题的根源可以称为整数除法".在Java中,将2个整数相除会产生一个非四舍五入的整数.

以下是解决您遇到的问题的多种方法.我更喜欢第一种方法,因为它允许您将公式与非整数值一起使用.并非三角形的所有长度都是整数:)


使用Scanner#getDouble并将 topLength bottomLength height 放在 double 中,将为您提供所需的输出./p>

您的代码将如下所示:

  public static void main(String [] args){扫描仪mathInput =新的Scanner(System.in);//声明变量double topLength,bottomLength,height;//获取用户输入System.out.print(请输入等腰梯形顶部的长度:");topLength = mathInput.nextDouble();mathInput.nextLine();System.out.print(请输入等腰梯形底部的长度:");bottomLength = mathInput.nextDouble();mathInput.nextLine();System.out.print(请输入等腰梯形的高度:");高度= mathInput.nextDouble();mathInput.nextLine();double trapArea =(((topLength + bottomLength)/2 *(height)));System.out.println();System.out.printf(等腰梯形的面积为:" + trapArea);} 


您还可以将 int s转换为两倍并按如下方式计算 trapArea :

  double trapArea =((((double)topLength +(double)bottomLength)/2 *((double)height)); 


或者甚至是简单的,如果你愿意,将 2 转换成双精度:

  double trapArea =((topLength + bottomLength)/2.0 *(height)); 


所有这些选项都将产生:

等腰梯形的面积为:13.5

I am writing a program for class which will allow the user to calculate the area of an isosceles trapezoid. Here is my code:

import java.util.Scanner;
import java.lang.Math;

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

      //declare variables

      int topLength, bottomLength, height;


      //Get user input
      System.out.print("Please enter length of the top of isosceles trapezoid: ") ;
      topLength = mathInput.nextInt() ;
      mathInput.nextLine() ;

      System.out.print("Please enter length of the bottom of isosceles trapezoid: ") ;
      bottomLength = mathInput.nextInt() ;
      mathInput.nextLine() ;

      System.out.print("Please enter height of Isosceles trapezoid: ") ;
      height = mathInput.nextInt() ;
      mathInput.nextLine() ;

      double trapArea = ((topLength + bottomLength)/2*(height));

      System.out.println();
      System.out.printf("The area of the isosceles trapezoid is: "+trapArea);
   }
}

If I enter say, 2 for topLength, 7 for bottomLength, and 3 for height, I will get an answer of 12.0, when it should result in an answer of 13.5. Does anyone know why my code is printing out wrong answers and not printing the .5?

解决方案

The base of the issue can be known as "Integer Division". In Java, dividing 2 integers will yield a non-rounded integer.

Below are multiple ways to fix the issue you are having. I prefer the first method as it allows you to use your formula with non-integer values. Not all the lengths of a triangle are integers :)


Using the Scanner#getDouble and placing topLength, bottomLength, and height in doubles will give you the desired output.

Your code will then look like this :

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

    // declare variables

    double topLength, bottomLength, height;

    // Get user input
    System.out.print("Please enter length of the top of isosceles trapezoid: ");
    topLength = mathInput.nextDouble();
    mathInput.nextLine();

    System.out.print("Please enter length of the bottom of isosceles trapezoid: ");
    bottomLength = mathInput.nextDouble();
    mathInput.nextLine();

    System.out.print("Please enter height of Isosceles trapezoid: ");
    height = mathInput.nextDouble();
    mathInput.nextLine();

    double trapArea = ((topLength + bottomLength) / 2 * (height));

    System.out.println();
    System.out.printf("The area of the isosceles trapezoid is: " + trapArea);
}


You could also cast your ints to doubles and calculate your trapArea as so :

double trapArea = (((double)topLength + (double)bottomLength) / 2 * ((double)height));


OR even simple, if you want, convert the 2 you are deviding to a double :

double trapArea = ((topLength + bottomLength) / 2.0 * (height));


All of these options will yield :

The area of the isosceles trapezoid is: 13.5

这篇关于显示时数学方程式结果丢失小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆