请帮忙编写一个数据转换的java程序。。。

2024-12-04 22:28:38
推荐回答(2个)
回答1:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MyTest {
public static List readFile(String fileName) {
List list = new ArrayList();
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = "";
while ((line=br.readLine()) != null)
list.add(line);
br.close();
fr.close();
} catch (Exception e) {e.printStackTrace();}
return list;
}
public static void main(String[] args){
String fileName = "d:/test.txt";
List list = readFile(fileName);
for(Iterator it=list.iterator();it.hasNext();){
String temp = (String)it.next();
String[] temps = temp.split(",");
double val = Double.parseDouble(temps[0].trim());
if(val>10) temps[0] = "0";
else temps[0]="1";
if("given".equals(temps[1].trim())) temps[1] = "0";
else temps[1] = "1";
System.out.println(temps[0]+","+temps[1]);
}
}
}

回答2:

public class Convert{
int givern;
int not_givern;
Convert (int i,int j) {
givern=i;
not_givern=j;
}
void numberConvert (double m,int n) {
if ((m>=0)&&(m<=10)) {
m=1;
}

else {
m=0;

}

System.out.println((int)m+","+n);
}
public static void main (String[] args) {
Convert con = new Convert(0,1);
con.numberConvert(20.8,con.givern);
con.numberConvert(30.4,con.givern);
con.numberConvert(1.24,con.not_givern);
con.numberConvert(11.3,con.givern);
con.numberConvert(5.00,con.givern);
con.numberConvert(7.33,con.not_givern);
}
}