代码之家  ›  专栏  ›  技术社区  ›  Konfu Chicken

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

  •  2
  • Konfu Chicken  · 技术社区  · 10 年前

    这是我为我的一个CS类编写的代码,但我想我还没有正确理解术语float。它适用于前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 回复  |  直到 10 年前
        1
  •  5
  •   Louis Wasserman    10 年前
    float quarts = cups * 0.25;
    

    在这里 0.25 被解释为 double ,强制 cups * 0.25 表示为 双重的 ,其精度高于 cups 。您有几个选项:

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

    另外,请注意您写的 cups * 0*5; 而不是 cups * 0.5 ,将设置 杯子 0 .

        2
  •  1
  •   BratBart    10 年前
    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);
    
        3
  •  0
  •   Avi    10 年前

    在Java中,当您键入一个十进制数字0.25时,它被解释为双精度。由于浮点比双精度精度低,因此无法隐式执行转换。

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

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

        4
  •  0
  •   Jim Driscoll    10 年前

    这是警告,不是错误。Double通常为64位,float通常为32位,因此在转换过程中会丢失最后的32位(由于所涉及的格式,它比这要复杂一点,但这是一个合理的近似值)。真的不是问题,除非你处理的是不能用浮点数表示的数字,比如真的很大,真的很小,或者大的只有很小的分数。

    除非内存使用有问题,否则您很少想使用浮点,所以只需说“double”(双精度浮点的缩写)就可以了。

    当你说“分数”时,你还没有解释你的意思,但我想你使用的是像3/4这样的符号。如果你提供两个整数,就像你在这里看到的那样,你会得到一个整数结果,因为这实际上是一件很有用的事情,可以用于某些编程目的。整数浮点表达式的符号是在末尾加上“.0”,例如3.0/4.0。