我有一個用 Java 撰寫的長度單位轉換器。如何通過 JSON 檔案設定轉換規則來實作擴展支持單位串列的功能?
類轉換計算器:
package convert;
import java.util.Scanner;
public class ConversionCalculator
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Convert from:");
String fromUnit = in.nextLine();
System.out.println("Convert to: ");
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
System.out.println("Value:");
double val = in.nextDouble();
double meters = from.toMeters(val);
double converted = to.fromMeters(meters);
System.out.println(val " " fromUnit " = " converted " " toUnit);
}
}
公共類 UnitConverter:
public class UnitConverter {
static double INCHES = 0.0254001;
static double FEET = 0.3048;
static double MILES = 1609.35;
static double MILLIMETERS = 0.001;
static double CENTIMETERS = 0.01;
static double METERS = 1;
static double KILOMETERS = 1000;
private double val, meters, converted;
String afromUnit;
public UnitConverter(String fromUnit) {
afromUnit = fromUnit;
}
public double toMeters(double val) {
if (afromUnit.equals("in")) {
meters = (val * INCHES);
} else if (afromUnit.equals("ft")) {
meters = (val * FEET);
} else if (afromUnit.equals("mi")) {
meters = (val * MILES);
} else if (afromUnit.equals("mm")) {
meters = (val * MILLIMETERS);
} else if (afromUnit.equals("cm")) {
meters = (val * CENTIMETERS);
} else if (afromUnit.equals("m")) {
meters = (val * METERS);
} else {
meters = (val * KILOMETERS);
}
return meters;
}
public double fromMeters(double meters) {
if (afromUnit.equals("in")) {
converted = Math.round(meters * 39.369923740457715);
} else if (afromUnit.equals("ft")) {
converted = Math.round(meters * 3.280839895013123);
} else if (afromUnit.equals("mi")) {
converted = Math.round(meters * 0.0006213688756330196);
} else if (afromUnit.equals("mm")) {
converted = Math.round(meters * 1000);
} else if (afromUnit.equals("cm")) {
converted = Math.round(meters * 100);
} else if (afromUnit.equals("m")) {
converted = Math.round(meters * 1);
;
} else {
converted = Math.round(meters * 0.001);
}
return converted;
}
}
如果我理解正確,那么必須將系數的值添加到 JSON 檔案中。之后,將 JSON 檔案轉換為 JAVA。對?第一次使用 JSON 檔案
uj5u.com熱心網友回復:
您的 JSON 可能如下所示:
[{
"name": "inches",
"unit": "in",
"conversion": "0.0254001",
}, {
"name": "meter",
"unit": "m",
"conversion: "1",
}]
在 JAVA 中,您可以執行以下操作來讀取它:
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(file));
JSONArray jsonArray = new JSONArray(obj.toString());
for (int i = 0; i < jsonArray.length(); i ) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.get("name"));
System.out.println(jsonObject.get("unit"));
System.out.println(jsonObject.get("conversion"));
}
uj5u.com熱心網友回復:
解決了這個問題。代碼部分:
public static void main(String[] args) {
String userJson = "[{\"unit\": \"in\", \"value\": \"11\", \"conversionto\": \"cm\"},"
"{\"unit\": \"ft\", \"value\": \"22\" , \"conversionto\": \"cm\"}]";
Gson gson = new Gson();
Type staffListType = new TypeToken<ArrayList<Staff>>(){}.getType();
ArrayList<Staff> staffArray = gson.fromJson(userJson, staffListType);
for(Staff staff : staffArray) {
System.out.println(staff);
String fromUnit = staff.unit;
String toUnit = staff.conversionto;
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
double val = staff.value;
double meters = from.toMeters(val);
double converted = to.fromMeters(meters);
System.out.println(val " " fromUnit " = " converted " " toUnit);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/473567.html
下一篇:決議ping的輸出
