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

Kon*_*ken 2 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);

}

}
Run Code Online (Sandbox Code Playgroud)

Lou*_*man 5

float quarts = cups * 0.25;
Run Code Online (Sandbox Code Playgroud)

这里0.25被解释为a double,强制cups * 0.25表示为a double,其精度高于cups.你有几个选择:

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

另外,请注意您编写的cups * 0*5;代替cups * 0.5,将设置cups0.