"程序要求——每次运行程序都会从文件读取上次程序运行的结果,并继续打印数字 例
1个回答
展开全部
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; i<line.length(); i++) {
sum += line.charAt(i) - '0';
}
System.out.println(sum);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
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; i<line.length(); i++) {
sum += line.charAt(i) - '0';
}
System.out.println(sum);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询