Java第6次上机

Java基本的API中,I/O的基本应用,包括:File类,InputStream,OutputStream,FileInputStream类,FileOutputStream类,DateInputStraeam,DataOutputStream,Reader,Writer,InputStreamReader,OutputStreamWriter,BufferedRead,BufferedWriter等。
-----------------------------------------------------------------------------------------------------------------------------------------
package alone;
import java.io.*;
import java.util.Date;
public class Alone13_1 {
public static void main(String[] args) throws IOException {
File f=new File(new BufferedReader(
new InputStreamReader(System.in)).readLine());
File []fs=f.listFiles();
for(int i=0;i<f.length();i++)
{
if(fs[i].isDirectory())
System.out.println("目录:"+fs[i]);
else
{
System.out.print("文件:"+fs[i]);
System.out.print(" 大小:"+fs[i].length()/1024+"Kbyte ");
if(fs[i].isHidden())System.out.print("隐藏属性:是 ");
else System.out.print("隐藏属性:是 ");
System.out.print("日期:"+
new Date(fs[i].lastModified()).toLocaleString()+"\n");
}
}
}
}

----------------------------------------------------------------------------------------------------------------------------------------------
package alone;
import java.io.*;
public class Alone13_2 {
public static void main(String[] args) throws Exception, IOException {
String [][]stud={
{"洪吉通","80","75","65","50"},
{"徐光豪","90","100","100","100"},
{"林顺喜","60","70","55","75"},
{"李之梅","60","80","75","80"},
{"洪顺照","80","70","90","85"},
{"朴顺吉","100","80","90","85"}
};
String fp="D:\\stud.data";
DataOutputStream dos=new DataOutputStream(new FileOutputStream(new File(fp)));
for(int i=0;i<stud.length;i++)
{
dos.writeUTF(stud[i][0]);
//System.out.print(stud[i][0]);
for(int k=1;k<=4;k++)
dos.writeInt(Integer.parseInt(stud[i][k]));
//System.out.print(stud[i][k]);
//System.out.print("\n");

}
dos.close();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
package alone;
import java.io.*;
public class Alone13_3 {
public static void main(String[] args) throws IOException {
Student[] s =new Student[6];
String fp="D:\\stud.data";
DataInputStream dos=new DataInputStream(
new FileInputStream(new File(fp)));
for(int i=0;i<6;i++)
{s[i]=new Student(dos.readUTF(),dos.readInt(),
dos.readInt(),dos.readInt(),dos.readInt());
s[i].get();
}
dos.close();
}
}
class Student{
String name;
int kor,eng,mat,sci;
Student(){}
Student(String name,int kor,int eng,int mat,int sci)
{
this.name=name; this.kor=kor; this.eng=eng;
this.mat=mat; this.sci=sci;
}
public void get()
{System.out.println(name+" "+kor+" "+eng+" "+mat+" "+sci);}

public void set(String name,int kor,int eng,int mat,int sci)
{ this.name=name; this.kor=kor;this.eng=eng;
this.mat=mat;this.sci=sci;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
package alone;
import java.io.*;
public class Alone13_4 {
public static void main(String[] args) throws Exception {
int n1,n2,n3;
System.out.println("第一个整数?");
n1=Integer.parseInt(new BufferedReader(
new InputStreamReader(System.in)).readLine());
System.out.println("第二个整数?");
n2=Integer.parseInt(new BufferedReader(
new InputStreamReader(System.in)).readLine());
System.out.println("第三个整数?");
n3=Integer.parseInt(new BufferedReader(
new InputStreamReader(System.in)).readLine());
System.out.println("结果");
System.out.println("合计:"+(n1+n2+n3));
System.out.println("平均:"+(n1+n2+n3)/3);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是 Java 上机实验六计算多种平均值的代码示例: ```java import java.util.Scanner; public class AverageCalculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入数字个数:"); int n = input.nextInt(); int[] numbers = new int[n]; double sum = 0; for (int i = 0; i < n; i++) { System.out.print("请输入第" + (i + 1) + "个数字:"); numbers[i] = input.nextInt(); sum += numbers[i]; } double arithmeticMean = sum / n; double geometricMean = 1; double harmonicMean = 0; for (int i = 0; i < n; i++) { geometricMean *= numbers[i]; harmonicMean += 1.0 / numbers[i]; } geometricMean = Math.pow(geometricMean, 1.0 / n); harmonicMean = n / harmonicMean; System.out.println("算术平均数为:" + arithmeticMean); System.out.println("几何平均数为:" + geometricMean); System.out.println("调和平均数为:" + harmonicMean); } } ``` 代码解释: 1. 首先,我们使用 `Scanner` 类来读取用户输入的数字个数和每个数字的值。 2. 然后,我们定义一个 `numbers` 数组来存储用户输入的数字,以便计算不同种类的平均数。 3. 接着,我们使用一个 `for` 循环来遍历 `numbers` 数组,计算总和。 4. 计算算术平均数,只需将总和除以数字个数即可。 5. 计算几何平均数,需要将所有数字相乘后开 n 次方。 6. 计算调和平均数,需要将每个数字的倒数相加,然后除以数字个数再取倒数。 输出结果: ``` 请输入数字个数:5 请输入第1个数字:1 请输入第2个数字:2 请输入第3个数字:3 请输入第4个数字:4 请输入第5个数字:5 算术平均数为:3.0 几何平均数为:2.605171084697352 调和平均数为:2.1897810218978103 ``` 希望这个示例能够帮助你理解如何计算多种平均数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值