Java 接收无穷大值
我试图做一个单位转换程序,但我一直接受无穷大的值。我不确定我需要修复哪里,因为它不会给我错误。我只测试了oz到ml,以确保我做得正确,但我得到的答案是无穷大 UnitConverter.java:Java 接收无穷大值,java,Java,我试图做一个单位转换程序,但我一直接受无穷大的值。我不确定我需要修复哪里,因为它不会给我错误。我只测试了oz到ml,以确保我做得正确,但我得到的答案是无穷大 UnitConverter.java: public class UnitConverter { final double oz_TO_ml = 29.5735; final double gal_TO_g = 3.78541; final double lb_TO_kg = 0.453592; fina
public class UnitConverter {
final double oz_TO_ml = 29.5735;
final double gal_TO_g = 3.78541;
final double lb_TO_kg = 0.453592;
final double inc_TO_mm = 25.4;//Inc is inches
final double ft_TO_cm = 30.48;
final double mi_TO_km = 1.60934;
double factor;
public UnitConverter(String unit) {
if (unit.equals("oz")) {
factor = oz_TO_ml;
} else if (unit.equals("gal")) {
factor = gal_TO_g;
} else if (unit.equals("lb")) {
factor = lb_TO_kg;
}
}
public double toOz(double amount) {
return (amount * factor);
}
public double fromOz(double amount) {
return (amount / factor);
}
public double toMl(double amount) {
return (amount * factor);
}
public double fromMl(double amount) {
return (amount / factor);
}
}
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
System.out.print("Convert to: ");
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
System.out.print("Value ");
double val = in.nextDouble();
double oz = from.toOz(val);
double converted = to.fromOz(oz);
System.out.println(val + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
Convert from: oz
Convert to: ml
Value 12
12.0 oz = Infinity ml
Calculator.java:
public class UnitConverter {
final double oz_TO_ml = 29.5735;
final double gal_TO_g = 3.78541;
final double lb_TO_kg = 0.453592;
final double inc_TO_mm = 25.4;//Inc is inches
final double ft_TO_cm = 30.48;
final double mi_TO_km = 1.60934;
double factor;
public UnitConverter(String unit) {
if (unit.equals("oz")) {
factor = oz_TO_ml;
} else if (unit.equals("gal")) {
factor = gal_TO_g;
} else if (unit.equals("lb")) {
factor = lb_TO_kg;
}
}
public double toOz(double amount) {
return (amount * factor);
}
public double fromOz(double amount) {
return (amount / factor);
}
public double toMl(double amount) {
return (amount * factor);
}
public double fromMl(double amount) {
return (amount / factor);
}
}
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
System.out.print("Convert to: ");
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
System.out.print("Value ");
double val = in.nextDouble();
double oz = from.toOz(val);
double converted = to.fromOz(oz);
System.out.println(val + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
Convert from: oz
Convert to: ml
Value 12
12.0 oz = Infinity ml
样本输入:
public class UnitConverter {
final double oz_TO_ml = 29.5735;
final double gal_TO_g = 3.78541;
final double lb_TO_kg = 0.453592;
final double inc_TO_mm = 25.4;//Inc is inches
final double ft_TO_cm = 30.48;
final double mi_TO_km = 1.60934;
double factor;
public UnitConverter(String unit) {
if (unit.equals("oz")) {
factor = oz_TO_ml;
} else if (unit.equals("gal")) {
factor = gal_TO_g;
} else if (unit.equals("lb")) {
factor = lb_TO_kg;
}
}
public double toOz(double amount) {
return (amount * factor);
}
public double fromOz(double amount) {
return (amount / factor);
}
public double toMl(double amount) {
return (amount * factor);
}
public double fromMl(double amount) {
return (amount / factor);
}
}
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
System.out.print("Convert to: ");
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
System.out.print("Value ");
double val = in.nextDouble();
double oz = from.toOz(val);
double converted = to.fromOz(oz);
System.out.println(val + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
Convert from: oz
Convert to: ml
Value 12
12.0 oz = Infinity ml
输出:
public class UnitConverter {
final double oz_TO_ml = 29.5735;
final double gal_TO_g = 3.78541;
final double lb_TO_kg = 0.453592;
final double inc_TO_mm = 25.4;//Inc is inches
final double ft_TO_cm = 30.48;
final double mi_TO_km = 1.60934;
double factor;
public UnitConverter(String unit) {
if (unit.equals("oz")) {
factor = oz_TO_ml;
} else if (unit.equals("gal")) {
factor = gal_TO_g;
} else if (unit.equals("lb")) {
factor = lb_TO_kg;
}
}
public double toOz(double amount) {
return (amount * factor);
}
public double fromOz(double amount) {
return (amount / factor);
}
public double toMl(double amount) {
return (amount * factor);
}
public double fromMl(double amount) {
return (amount / factor);
}
}
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
System.out.print("Convert to: ");
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
System.out.print("Value ");
double val = in.nextDouble();
double oz = from.toOz(val);
double converted = to.fromOz(oz);
System.out.println(val + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
Convert from: oz
Convert to: ml
Value 12
12.0 oz = Infinity ml
您的
UnitConverter
类构造函数只知道3个单位:oz
、gal
和lb
。如果使用其中一个进行实例化,它将正确分配因子并能够转换单位,如下所示:
public UnitConverter(String unit) {
if (unit.equals("oz")) {
factor = oz_TO_ml;
} else if (unit.equals("gal")) {
factor = gal_TO_g;
} else if (unit.equals("lb")) {
factor = lb_TO_kg;
}
}
但是,在计算器类中,您有以下行:
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
double converted = to.fromOz(oz);
如果使用示例输入运行程序,则从isoz
到isml
。但是,如果您使用单位ml
实例化UnitConverter
,那么该因子设置为什么?根据您的构造函数,它从未设置,因此它保留默认值0.0
稍后,您可以调用此行:
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
double converted = to.fromOz(oz);
这将运行fromOz
方法
public double fromOz(double amount) {
return (amount / factor);
}
除以系数,即0.0
。这是无限输出的来源
正如另一个答案所说,执行此计算不需要两个UnitConverter
对象。这个系数在盎司和毫升之间的转换是正确的,所以这个计算器代码就足够了
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
System.out.print("Value ");
double val = in.nextDouble();
double result = from.toMl(val);
System.out.println(val + " " + fromUnit + " = " + result + " ml.");
}
}
如果希望保留当前的计算器代码,则需要在UnitConverter
构造函数中为ml(1.0
)的scalefactor添加一个条件。然而,我认为这种方法是有缺陷的,因为当你尝试在盎司和英寸之间转换时会发生什么?这种转换毫无意义,但您的体系结构不会阻止它
public UnitConverter(String unit)
{
if (unit.equals("oz"))
{
factor = oz_TO_ml;
} else if (unit.equals("gal"))
{
factor = gal_TO_g;
} else if (unit.equals("lb"))
{ factor = lb_TO_kg;
}
}
如果通过“ml”,则因子将为零
您的设计目前需要其中两个,但您实际上只需要一个,因为“oz”拥有进行转换所需的一切
忽略行输入代码中的toUnit,只使用fromUnit
编辑:我将向您展示另一种方式,它只支持一个转换来显示粗略的设计。注意,方法调用现在是静态的,因为您只需要它们的一个实例
UnitConverter.java
public class UnitConverter
{
private static final double oz_TO_ml = 29.5735;
public static double convert(String fromType, String toType,double amount) throws IllegalArgumentException
{
if (fromType.equals("oz") && toType.equals("ml"))
{
return (amount * oz_TO_ml);
}
else
{
throw new IllegalArgumentException("The combination of converting " + fromType + " to " + toType + " is not supported");
}
}
}
Calculator.java:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Convert from: ");
String fromUnit = in.nextLine();
System.out.print("Convert to: ");
String toUnit = in.nextLine();
System.out.print("Value ");
double val = in.nextDouble();
System.out.println(val + " " + fromUnit + " = " + UnitConverter.convert(fromUnit,toUnit,val) + " " + toUnit);
}
}用一初始化因子变量。默认值为0的java将原语double
class UnitConvertor {
final double oz_TO_ml = 29.5735;
final double gal_TO_g = 3.78541;
final double lb_TO_kg = 0.453592;
final double inc_TO_mm = 25.4;//Inc is inches
final double ft_TO_cm = 30.48;
final double mi_TO_km = 1.60934;
double factor=1;//initialize with 1
但我仍然不确定,如果用户输入为“ml”,您使用的检查是什么。您在运行时键入了什么?系统输出的结果是什么statement@immibis我运行它并在oz中输入,当它要求convert from:时,然后输入ml表示conver to:和12表示value,您的程序使用什么转换因子表示“ml”?将“oz”转换为“ml”意味着什么?这不取决于物质吗?你说的通过“ml”是什么意思?im现在有点丢失UnitConverter to=新的UnitConverter(toUnit);哦,好的。我怎么修复它呢?我试着忽略了toUnit,它给了我一个语法error@Jay我想他是在建议你退后一步,从一开始就努力去理解这个问题。没有人能在不了解问题是什么的情况下成功地解决问题。一旦你真正理解了这个问题,解决这个问题的正确方法就会更容易找到。请点击我的答案旁边显示为“禁用”的回答标志。我会的,但它不允许我这样做。说我没有必要的15个名声。@jay不用担心快乐的Codingthanks!你帮我省了很多时间试图解决这个问题。@jay这不是一个真正的答案,所以不要太激动,它所做的只是隐藏了你对代码如何工作的中间理解,它涵盖了一些严重的基本设计错误伟大的答案,它很好地描述了问题,并且有更好的解决方案