package chap1;
import java.io.BufferedReader;
import java.io.FileReader;
public class t1111 {
public static void main(String args[])throws Exception{
String str=null;
int total=0;
BufferedReader fr = new BufferedReader(new FileReader("a.txt"));
while((str=fr.readLine()) !=null){
total = total + Integer.parseInt(str); //累加
System.out.println("total " + total);
}
}
}
---
package chap1;
import java.io.BufferedReader;
import java.io.FileReader;
public class t1111 {
public static void main(String args[])throws Exception{
String str=null;
int total=0,i=0;
BufferedReader fr = new BufferedReader(new FileReader("a.txt"));
while(i < 11) {
i=i+1;
total = total + i;
System.out.println("total= " + total + "\ti="+i);
}
}
}
---
package chap1;
import java.io.BufferedReader;
import java.io.FileReader;
public class t1111 {
public static void main(String args[])throws Exception{
String str=null;
int total=0,i=0;
BufferedReader fr = new BufferedReader(new FileReader("a.txt"));
while(i<11) {
total = total + i;
System.out.println("total= " + total + "\t"+"i="+i);
i=i+1;
}
}
}
--- For迴圈
package chap1;
public class t1111 {
public static void main(String args[]){
int total=0;
for (int i=5; i<101; i=i+5){
total = total+i; //累加
}
System.out.println("1+2+3+4+5+6+7+8+9+10="+total);
}
}
---陣列
package chap1;
public class t1111 {
public static void main(String args[]){
int total = 0;
int data[]={75,85,100,60,100};
for (int i=0; i<5; i++){
System.out.println(data[i]);
total=total+data[i];
System.out.println("total="+total);
}
}}
------作業
package chap1;
import java.io.BufferedReader;
import java.io.FileReader;
public class t1113 {
public static void main(String args[]) throws Exception {
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
String ss = "";
int sum = 0;
int i = 0;
int[] data = new int[100];
while ((ss = fr.readLine()) != null) {
data[i] = Integer.parseInt(ss);
System.out.println(ss);
sum = sum + data[i];
i++;
}
System.out.println("\n\n\n" + sum);
}
}
留言列表