Java基础编程题

题目:第一个人10岁,第二个人比第一个人大2岁,依次递推,请用递归的方式计算出第8个人多大

public class program15 {


    public static void main(String[] args) {

        System.out.println(computeAge(8));
    }

    private static int computeAge(int i) {
        if(i==1){
            return 10;
        }
        return computeAge(i-1)+2;
    }

}

题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 “stud “中。

public class lianxi50 { 
public static void main(String[] args){ 
   Scanner ss = new Scanner(System.in);
   String [][] a = new String[5][6];
   for(int i=1; i<6; i++) {
    System.out.print("请输入第"+i+"个学生的学号:");
    a[i-1][0] = ss.nextLine();
    System.out.print("请输入第"+i+"个学生的姓名:");
    a[i-1][1] = ss.nextLine();
    for(int j=1; j<4; j++) {
       System.out.print("请输入该学生的第"+j+"个成绩:");
       a[i-1][j+1] = ss.nextLine();
       }
System.out.println("\n");
   }
//以下计算平均分
float avg;
int sum;
for(int i=0; i<5; i++) {
sum=0;
   for(int j=2; j<5; j++) {
   sum=sum+ Integer.parseInt(a[i][j]);
      }
   avg= (float)sum/3;
   a[i][5]=String.valueOf(avg);
}
//以下写磁盘文件 
String s1; 
try { 
    File f = new File("C:\\stud"); 
    if(f.exists()){ 
      System.out.println("文件存在"); 
      }else{ 
         System.out.println("文件不存在,正在创建文件"); 
          f.createNewFile();//不存在则创建 
        } 
BufferedWriter output = new BufferedWriter(new FileWriter(f)); 
for(int i=0; i<5; i++) {
for(int j=0; j<6; j++) {
   s1=a[i][j]+"\r\n";
   output.write(s1);    
    }
}
output.close(); 
System.out.println("数据已写入c盘文件stud中!");
   } catch (Exception e) { 
     e.printStackTrace(); 
     } 
}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fighting_Boss_Hao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值