gpt4 book ai didi

java - 使用 float "possible lossy conversion"的问题

转载 作者:行者123 更新时间:2023-12-03 07:59:44 24 4
gpt4 key购买 nike

这是我为我的一个 CS 类(class)编写的代码,但我想我对术语 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);

}

}

最佳答案

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 - 使用 float "possible lossy conversion"的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com