Java程序如下:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class HardWork {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("c:\\test.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line;
int sum;
int i;
while((line = br.readLine()) != null) {
sum = 0;
for(i=0; isum += line.charAt(i) - '0';
}
System.out.println(sum);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
运行测试:
5
19
文件内容:
23
496
public class Test{
public static void main(String... args){
File file = new File("d:\\test.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String temp;
while((temp=br.readLine())!=null){
int n = temp.length();
int num = Integer.parseInt(temp);
int sum = 0;
for(int i=0;i
sum+=(int)num/Math.pow(10, i)%10;
System.out.println(sum);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}