📄 writedata.java
字号:
//输入成绩
import java.io.*;
class WriteData
{
public static void main(String[] args)
{
byte[] byteBuf = new byte[81];//缓冲区
File file = new File("g:\\data.txt");//输出文件
FileOutputStream fos = null;//输出流
try{
fos = new FileOutputStream(file);
}
catch(FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
for(int i=0; i<5; i++)//输入成绩
{
System.out.print("请输入成绩 " + (i+1) + ":");
try{
System.in.read(byteBuf);
String sTemp = new String(byteBuf);
sTemp = sTemp.trim() + " ";
fos.write(sTemp.getBytes());//写入文件
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
try{
fos.close();//关闭流
fos = null;
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
//计算成绩
import java.io.*;
import java.util.*;
class ComputeData
{
public static void main(String[] args)
{
String sTemp;
try{
byte[] byteBuffer = new byte[81];//缓冲区
File file = new File("g:\\data.txt");//输入文件
FileInputStream fis = new FileInputStream(file);//输入流
fis.read(byteBuffer);
fis.close();
sTemp = new String(byteBuffer);
}
catch(IOException ioe)
{
ioe.printStackTrace();
return;
}
System.out.println("原始成绩: " + sTemp);
StringTokenizer st = new StringTokenizer(sTemp, " ");
double[] dScore = new double[5];
int iPosition = 0;
while (st.hasMoreTokens()) {
String s = st.nextToken();
if(s != null)
{
s = s.trim();
}
if(s.length() > 0)
dScore[iPosition++] = Double.parseDouble(s);
}
double dSum = 0.0;
for(int i=0; i<dScore.length; i++)
dSum += dScore[i];
System.out.println("平均成绩: " + (dSum/dScore.length));
bubbleSort(dScore);
System.out.println();
System.out.print("排序后成绩: ");
for(int i=0; i<dScore.length; i++)
{
System.out.print(dScore[i] + " ");
}
System.out.println();
}
//冒泡排序
public static void bubbleSort(double[] dValue)
{
for(int i=0; i<dValue.length; i++)
{
for(int j=i; j<dValue.length; j++)
{
if(dValue[i] > dValue[j])
{
double dTemp = dValue[i];
dValue[i] = dValue[j];
dValue[j] = dTemp;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -