这是我为我的一个CS类编写的代码,但是我想我对术语float没有正确的理解。它适用于前3次转换,然后给我带来品脱,夸脱和加仑(小数点开始)的错误。我试过将它们转换为分数,但是程序最终将结果吐出0。类型不兼容导致的错误:可能从双精度转换为浮点型

我的代码如下:

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);

}

}

最佳答案

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

关于java - 使用浮点数“可能的有损转换”的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35247705/

10-12 02:15