使用浮动“可能有损转换”的问题

时间:2016-02-06 23:18:12

标签: java floating-point

这是我为我的一个CS课程编写的代码,但我想我还没有正确理解浮动这个术语。它适用于前3次转换,然后它给出了品脱,夸脱和加仑(小数点开始时)的错误。我已经尝试将它们转换为分数,但程序最终会随后吐出0。产生的错误是不兼容的类型:可能有损转换从double到float

我的代码如下:

import java.lang.*;
import java.util.*;

/* 
Description: This application will be used to convert a user given volume 
in cups to its equivalent number of teaspoons, tablespoons, ounces, pints
quarts, or gallons.

This program will allow us to view what a certain volume of cups would be in
tablespoons, teaspoons etc.

This program will need the number of cups from the user.
Then the program will output the neccessary teaspoons, tablespoons etc.

                    4 cups equals 4 * 48 = 192 teaspoons
                    4 cups equals 4 * 16 =  64 tablespoons
                    4 cups equals 4 * 8   =  32 ounces
                    4 cups equals 4 * 0.5   =  2 pints
                    4 cups equals 4 * 0.25   =  1 quart
                    4 cups equals 4 * 0.0625   =   0.2500 gallon

java.util and java.text will be used

The input and output will be simple text based interactions using 
system.out.Println and scanner

Psuedocode: 
            Output a welcome message
            Output a message that describes what the program will do
            Output a message requesting the number cups the user wishes to 
            convert

            read the input value and store it

            calculate the teaspoons, tablespoons etc and store it.

            output a message that displays this values so the user can see
            it
*/

class cupsconversion
{

public static void main(String[] args)
{
System.out.println("Welcome to Shahrukhs Cup Conversion Program");
System.out.println();
System.out.println("This application will be used to convert a user given volume");
System.out.println("in cups to its equivalent number of teaspoons, tablespoons, ounces, pints");
System.out.println("quarts, or gallons");
System.out.println("\n \n");
System.out.println("Please type in a +ve real value for the number of cups you want converted");
System.out.print(" Number of cups = ");

Scanner input = new Scanner(System.in);

float cups; // We are storing the input the user puts in float.

cups = input.nextFloat();

float teaspoons = cups * 48;

float tablespoons = cups * 16;

float ounces = cups * 8;

float pints = cups * 0*5;

float quarts = cups * 0.25;

float gallons = cups * 0.0625;

System.out.println(" Given " + cups + " cups, the volume in teaspoons are " + teaspoons);
System.out.println(" Given " + cups + " cups, the volume in tablespoons are " + tablespoons);
System.out.println(" Given " + cups + " cups, the volume in ounces are " + ounces); 
System.out.println(" Given " + cups + " cups, the volume in pints are " + pints);   
System.out.println(" Given " + cups + " cups, the volume in quarts are " + quarts);
System.out.println(" Given " + cups + " cups, the volume in gallons are " + gallons);

}

}

4 个答案:

答案 0 :(得分:5)

float quarts = cups * 0.25;

此处0.25被解释为double,强制cups * 0.25被表示为double,其精度高于cups。您有几种选择:

  • cups * 0.25f
  • cups / 4
  • (float) (cups * 0.25)

另请注意,您撰写了cups * 0*5;而不是cups * 0.5,这会将cups设置为0

答案 1 :(得分:1)

import java.lang.*;
import java.util.*;

/* 
 Description: This application will be used to convert a user given volume 
 in cups to its equivalent number of teaspoons, tablespoons, ounces, pints
 quarts, or gallons.

This program will allow us to view what a certain volume of cups would be in
tablespoons, teaspoons etc.

This program will need the number of cups from the user.
Then the program will output the neccessary teaspoons, tablespoons etc.

                4 cups equals 4 * 48 = 192 teaspoons
                4 cups equals 4 * 16 =  64 tablespoons
                4 cups equals 4 * 8   =  32 ounces
                4 cups equals 4 * 0.5   =  2 pints
                4 cups equals 4 * 0.25   =  1 quart
                4 cups equals 4 * 0.0625   =   0.2500 gallon

java.util and java.text will be used

The input and output will be simple text based interactions using 
system.out.Println and scanner

Psuedocode: 
        Output a welcome message
        Output a message that describes what the program will do
        Output a message requesting the number cups the user wishes to 
        convert

        read the input value and store it

        calculate the teaspoons, tablespoons etc and store it.

        output a message that displays this values so the user can see
        it
*/

public class cupsconversion
{

    public static void main(String[] args)
{
   System.out.println("Welcome to Shahrukhs Cup Conversion Program");
   System.out.println();
   System.out.println("This application will be used to convert a user given              volume");
  System.out.println("in cups to its equivalent number of teaspoons,     tablespoons, ounces, pints");
 System.out.println("quarts, or gallons");
 System.out.println("\n \n");
 System.out.println("Please type in a +ve real value for the number of cups you want converted");
 System.out.print(" Number of cups = ");

 Scanner input = new Scanner(System.in);

 float cups; // We are storing the input the user puts in float.

 cups = input.nextFloat();

float teaspoons = cups * 48;

float tablespoons = cups * 16;

float ounces = cups * 8;

float pints = cups * 0*5;

float quarts = cups * 0.25f;

float gallons = cups * 0.0625f;

  System.out.println(" Given " + cups + " cups, the volume in teaspoons are " + teaspoons);
  System.out.println(" Given " + cups + " cups, the volume in tablespoons are " + tablespoons);
  System.out.println(" Given " + cups + " cups, the volume in ounces are " + ounces); 
  System.out.println(" Given " + cups + " cups, the volume in pints are " + pints);   
  System.out.println(" Given " + cups + " cups, the volume in quarts are " + quarts);
  System.out.println(" Given " + cups + " cups, the volume in gallons are " +     gallons);

 }

     } //fractions needs to be followed with "f"
      //float quarts = cups * 0.25f;
     //float gallons = cups * 0.0625f;
    // or you will need to cast to convert from double to float
   //float quarts = (float) (cups * 0.25);
  //float gallons = (float) (cups * 0.0625);

答案 2 :(得分:0)

在Java中,当您输入一个十进制数字为0.25时,它被解释为double。由于float不如double精确,因此无法隐式执行转换。

如果你想创建一个浮点数,你应该用f(即:0.25f)结束你的数字。

有关更多说明,请参阅Java教程的原始数据类型定义。

答案 3 :(得分:0)

这是一个警告,而不是错误。 Double通常为64位,通常为32位浮点数,因此您在该转换中丢失了最后32位(由于所涉及的格式,它稍微复杂一点,但这是一个合理的近似值)。真的不是问题,除非你处理那些不能表现为浮点数的数字,即真正巨大的,非常小的,或只有一小部分的大数字。

你很少想使用float,除非你遇到内存使用问题,所以只说" double" - "双精度浮点" - 应该没事。

当你说" fractions"时,你还没有解释你的意思,但我认为你使用的是3/4这样的符号。如果你提供两个整数,就像你在这里一样,你会得到一个整数结果,因为这对于某些编程目的来说实际上是一件很有用的事情。整数的浮点表达式的表示法是添加" .0"到最后,例如。 3.0 / 4.0。