(1)产生5000个1~9999之间的随机整数,并将其存入文本文件a.txt中。
(2)从文件中读取这五千个整数,并计算其最大值、最小值、平均值。
急——
java编写程序实现以下功能
答案:2 悬赏:50 手机版
解决时间 2021-02-16 10:32
- 提问者网友:了了无期
- 2021-02-15 14:36
最佳答案
- 五星知识达人网友:撞了怀
- 2021-02-15 15:38
public static void main(String[] args)
{
test();
}
public static void test()
{
String tempStr="";
for(int i=0;i<5000;i++)
{
double a=Math.random()*9999;
int aa=(int)a+1;
tempStr+=aa+"\r\n";
}
outputData("c:\\a.txt",tempStr);
test1();
}
public static void outputData(String filePath, String text)
{
try
{
BufferedWriter rw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filePath), "UTF-8"));
rw.write(text);
rw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void test1()
{
String s=new String();
int leng=0;
int test[]=new int[5000];
try
{
BufferedReader input = new BufferedReader(new FileReader("c:\\a.txt"));
while ((s = input.readLine()) != null&&leng<5000)
{
test[leng]=Integer.parseInt(s);
leng++;
}
input.close();
int max=test[0];
int min=test[0];
int sum=0;
for(int i=0;i<5000;i++)
{
if(test[i]
min=test[i];
if(test[i]>max)
max=test[i];
sum+=test[i];
}
System.out.println("max="+max);
System.out.println("min="+min);
System.out.println("avg="+(double)sum/5000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
{
test();
}
public static void test()
{
String tempStr="";
for(int i=0;i<5000;i++)
{
double a=Math.random()*9999;
int aa=(int)a+1;
tempStr+=aa+"\r\n";
}
outputData("c:\\a.txt",tempStr);
test1();
}
public static void outputData(String filePath, String text)
{
try
{
BufferedWriter rw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filePath), "UTF-8"));
rw.write(text);
rw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void test1()
{
String s=new String();
int leng=0;
int test[]=new int[5000];
try
{
BufferedReader input = new BufferedReader(new FileReader("c:\\a.txt"));
while ((s = input.readLine()) != null&&leng<5000)
{
test[leng]=Integer.parseInt(s);
leng++;
}
input.close();
int max=test[0];
int min=test[0];
int sum=0;
for(int i=0;i<5000;i++)
{
if(test[i]
if(test[i]>max)
max=test[i];
sum+=test[i];
}
System.out.println("max="+max);
System.out.println("min="+min);
System.out.println("avg="+(double)sum/5000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
全部回答
- 1楼网友:酒者煙囻
- 2021-02-15 16:42
public static void main(string[] args)
{
test();
}
public static void test()
{
string tempstr="";
for(int i=0;i<5000;i++)
{
double a=math.random()*9999;
int aa=(int)a+1;
tempstr+=aa+"\r\n";
}
outputdata("c:\\a.txt",tempstr);
test1();
}
public static void outputdata(string filepath, string text)
{
try
{
bufferedwriter rw = new bufferedwriter(new outputstreamwriter(
new fileoutputstream(filepath), "utf-8"));
rw.write(text);
rw.close();
}
catch (exception e)
{
e.printstacktrace();
}
}
public static void test1()
{
string s=new string();
int leng=0;
int test[]=new int[5000];
try
{
bufferedreader input = new bufferedreader(new filereader("c:\\a.txt"));
while ((s = input.readline()) != null&&leng<5000)
{
test[leng]=integer.parseint(s);
leng++;
}
input.close();
int max=test[0];
int min=test[0];
int sum=0;
for(int i=0;i<5000;i++)
{
if(test[i]<min)
min=test[i];
if(test[i]>max)
max=test[i];
sum+=test[i];
}
system.out.println("max="+max);
system.out.println("min="+min);
system.out.println("avg="+(double)sum/5000);
}
catch (exception e)
{
e.printstacktrace();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯