用java编程:产生5000个1~9999之间的随机整数,将其存入本文文件a.txt中
展开全部
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test {
public static void main(String[] args) {
List<Integer> arr = new ArrayList<Integer>(5000);
Random random = new Random();
for (int i = 0; i < 5000; i++) {
Integer r = null;
do
// 由于要求1~9999,所以得排除0的情况
r = random.nextInt(10000);
while (r == 0);
arr.add(random.nextInt(10000));
}
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("C:\\a.txt")));
for (Integer i : arr) {
bw.write(String.valueOf(i));
bw.newLine();
}
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test {
public static void main(String[] args) {
List<Integer> arr = new ArrayList<Integer>(5000);
Random random = new Random();
for (int i = 0; i < 5000; i++) {
Integer r = null;
do
// 由于要求1~9999,所以得排除0的情况
r = random.nextInt(10000);
while (r == 0);
arr.add(random.nextInt(10000));
}
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("C:\\a.txt")));
for (Integer i : arr) {
bw.write(String.valueOf(i));
bw.newLine();
}
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
展开全部
public class Test {
public static void main(String[] args)
{
int value=1;
for (int i= 1; i<=10; i++) {
value+=findn(i);
System.out.println("n值为:"+i+" value值为:"+value);//可注掉,调试时看值。
if(value>9999){
System.out.println("最大n值为:"+(i-1)+" 最大value值为:"+(value-findn(i)));
break;
}
}
}
static long findn(int n)
{
if (n == 1)return 1;
else return n * findn(n - 1);
}
}
n为7 value为5914
public static void main(String[] args)
{
int value=1;
for (int i= 1; i<=10; i++) {
value+=findn(i);
System.out.println("n值为:"+i+" value值为:"+value);//可注掉,调试时看值。
if(value>9999){
System.out.println("最大n值为:"+(i-1)+" 最大value值为:"+(value-findn(i)));
break;
}
}
}
static long findn(int n)
{
if (n == 1)return 1;
else return n * findn(n - 1);
}
}
n为7 value为5914
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Random r = new Random();
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("c://a.txt"))));
for(int i = 0;i< 5000;i++){
Integer a = r.nextInt(9999)+1;
bufw.write(a.toString()+" ");
}
bufw.close();
写的不是很美观对付看吧,就是实现了功能
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("c://a.txt"))));
for(int i = 0;i< 5000;i++){
Integer a = r.nextInt(9999)+1;
bufw.write(a.toString()+" ");
}
bufw.close();
写的不是很美观对付看吧,就是实现了功能
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询