我正在从 CSV 文件读取数值。我想将这些值与预先已知的其他值进行比较(下面代码中的 x
、 y
和 z
,其中已知 x < y < z
)。但我无法弄清楚如何将任何给定值与 x
进行比较, y
和z
并根据结果采取行动。
public HashMap<String , Double> ReadCVS() {
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
double x = 7.33;
double y = 12.33;
double z = 20.66;
try {
br = new BufferedReader(new FileReader(this.filePath));
br.readLine();
while ((line = br.readLine()) != null) {
// use comma as separator
String[] tempData = line.split(cvsSplitBy);
Data.put(tempData[0], Double.parseDouble(tempData[1]));
for(int i =0;i<=tempData[1].length();i++){
//S++;
if(((Double.parseDouble(tempData[1]))<x)){
System.out.println("A");
}
如何比较从 Double.parseDouble(tempData[1])
获得的 double 值值为 x
, y
和z
?我需要使用类似 if smaller than x print A, else if between x and y print B e lse print C
的逻辑....
最佳答案
I need to use logic like
if smaller than x print A, else if between x and y print B e lse print C
....
嗯,你几乎可以做到这一点。只需使用一个变量来保存值:
if (v < x) {
System.out.println("A");
}
else if (v < y) {
System.out.println("B");
}
else {
System.out.println("C");
}
根据需要进行调整。
此外,您的 tempData
是一个字符串数组,tempData[1]
是数组中的第二个字符串,tempData[1].length()
是该字符串的长度。我猜您更愿意遍历数组中的所有值,因此您的循环可能看起来更像这样:
for (int i = 0; i < tempData.length; i++) {
double v = Double.parseDouble(tempData[i]);
// put your if statements here
}
关于java - 将输入 double 值与多个已知值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36013943/