Java 如何创建random.txt文件,然后在文件中随机创建500个随机整数,然后使用该文件对其运行常用算法?
下面给出的代码不起作用: 我的任务是,Java 如何创建random.txt文件,然后在文件中随机创建500个随机整数,然后使用该文件对其运行常用算法?,java,arrays,algorithm,random,Java,Arrays,Algorithm,Random,下面给出的代码不起作用: 我的任务是, 编写一个程序,生成500个随机整数并将其存储到数组中。整数值将在1到1000之间 将500个随机整数打印到.txt文件 main方法将控制程序的运行。(菜单等) 使用if/else梯形图或开关盒创建菜单,以控制程序的操作。菜单应该在主菜单中 创建一个名为Common_Algo的“程序员创建的类” 从任务中创建的th.txt文件中读取500个随机整数 2: 使用类构造函数用.txt文件中的随机整数填充数组。500整数数组(arr[500])将是“程序员创建的
.txt文件
.txt文件
中读取500个随机整数
2:(您的菜单将调用这些方法。所有方法将在同一数组上操作。) 输入1。输出所有值
输入2。所有值的输出总和和值的平均值
输入3。输出所有奇数
输入4。输出所有偶数\ 输入5。输出中间值(中间值)
输入6。输出数组中的第一个值
输入7。输出数组中的最后一个值
输入8。输入搜索值
输入9。输出排序数组
输入10。输出最大值及其在数组中的位置。
输入11:输出最小值及其在数组中的位置。
输入12:退出
**以下是我迄今为止实现的,但它没有在命令提示符下编译。**
import java.io.*;
import java.util.*;
public class Common_Algo
{
private int i;
int [] arr;
public Common_Algo()
{
try {
FileReader file = new FileReader("random.txt");
Scanner scanner = new Scanner(file);
arr = new int [500];
i = 0;
while (scanner.hasNextInt())
{
arr[i++] = scanner.nextInt();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
void printAllValues()
{
for (i = 0; i<500; i++)
{
System.out.println(arr[i]);
}
}
void sumAndMean()
{
// calculate the sum
int sum = 0;
float average = 0;
for (int i = 0; i< arr.length; i++){
sum = sum + arr[i];
}
System.out.println("The sum of all the elements in the array " + sum);
// calculate the average
average = sum/arr.length;
System.out.println("Average of all vlaues of an array:" + average);
}
void oddNumber()
{
System.out.println("Odd numbers in array is:");
for(int i = 0; i<arr.length; i++)
{
if (arr[i]%2 ==0)
{
System.out.println(arr[i]);
}
}
}
void evenNumber()
{
System.out.println("Even numbers in array is:");
for(int i=0; i<arr.length; i++)
{
if (arr[i]%2==0)
{
System.out.println(arr[i]);
}
}
}
void middleValue(){
int m = 0;
if (arr.length%2 == 1)
{
m=(arr[(arr.length + 1)/2-1]);
}
else
{
m=(arr[arr.length/2-1]+arr[arr.length/2])/2;
}
System.out.println("Middle Value = "+ m);
}
void firstValue(){
System.out.println("The first value in the array is: " + arr[0]);
}
void lastValue()
{
System.out.println("The last value in the array is: " +arr[arr.length-1]);
}
void searchValue()
{
Scanner scanner = new Scanner (System.in);
int search_Value,flag=0;
System.out.println("Enter search value: ");
search_Value = scanner.nextInt();
//this does linear search for searching the value in array
for (int i = 0; i<arr.length; i++)
{
if (search_Value == arr[i])
{
System.out.println("Value " + search_Value +" is found at location "+ i);
flag = 1;
}
}
if (flag == 0)
{
System.out.println("Value " + search_Value + " is not found inside the array");
}
}
void sortArray()
{
Arrays.sort(arr);
System.out.println("The sort array list: ");
//sort array list
for (i=0; i<arr.length; i++){
System.out.println(arr[i]);
}
}
void highValue()
{
int max = arr [0];
for (int i = 1; i < arr.length; i++){
if (arr[i] > max){
max = arr[i];
System.out.println("The highest value of array is: " + max);
}
}
}
void lowValue()
{
int min = arr[0];
for (int i = 1; i <arr.length; i++)
if (arr[i] < min)
min = arr[i];
System.out.println("The lowest value in the array: " + min);
}
}
class RandomData
{
public static void main(String[] args) {
int i,choice;
Scanner scanner = new Scanner(System.in);
Random rnum = new Random();
try {
PrintWriter fileout = new PrintWriter(new FileWriter ("random.txt"));
for (i = 0; i<500; i++)
{
fileout.println(rnum.nextInt(1000));
}
fileout.close();
}
catch (Exception e)
{
System.out.println(e);
}
Common_Algo object = new Common_Algo();
do
{
System.out.println("Enter 1 for: Output of all values ");
System.out.println("Enter 2 for: Ouput of Sum and mean Average values");
System.out.println("Enter 3 for: Output of all odd numbers");
System.out.println("Enter 4 for: Output of all even numbers" );
System.out.println("Enter 5 for: Output of middle values");
System.out.println("Enter 6 for: Output of first value in the array");
System.out.println("Enter 7 for: Output of last vlaue in the array");
System.out.println("Enter 8 for: Output of the entered search value");
System.out.println("Enter 9 for: Output of the Sorted Array");
System.out.println("Enter 10 for: Output of the highest value and its location in the array");
System.out.println("Enter 11 for: Output of lowest value and its location in the array");
System.out.println("Enter 12 for: Exit");
System.out.println("Please enter your choice");
choice = scanner.nextInt();
switch(choice)
{
case 1:
object.printAllValues();
break;
case 2:
object.sumAndMean();
break;
case 3:
object.oddNumber();
break;
case 4:
object.evenNumber();
break;
case 5:
object.middleValue();
break;
case 6:
object.firstValue();
break;
case 7:
object.lastValue();
break;
case 8:
object.searchValue();
break;
case 9:
object.sortArray();
break;
case 10:
object.highValue();
break;
case 11:
object.lowValue();
break;
case 12:
System.exit(0);
}
}
while(choice!=12);
}
}
import java.io.*;
导入java.util.*;
公共类公共算法
{
私人互联网i;
int[]arr;
公共公共算法()
{
试一试{
FileReader文件=新的FileReader(“random.txt”);
扫描仪=新扫描仪(文件);
arr=新整数[500];
i=0;
while(scanner.hasNextInt())
{
arr[i++]=scanner.nextInt();
}
}
捕获(例外e)
{
系统输出打印ln(e);
}
}
void printAllValues()
{
对于(i=0;i不要试图在一次大跳跃中一次完成所有事情。一步一步地做。你的导师已经将问题分解为多个步骤。分别为每个步骤编写代码。测试代码以确保其工作正常,修复发现的任何错误。是的,这意味着阅读和理解发现的任何编译错误。因为如果你一次只写一篇文章,那么每一篇文章中不应该有太多的错误
当你有了一段经过测试和工作的代码后,将它与你目前为止所写的其他代码集成,并测试新的组合代码。同样,修复所有错误
这种循序渐进的方法意味着您永远不会有一大堆代码需要修复,而只需要一些更容易处理的小块代码。您可以一块一块地构建最终的程序,一路测试和修复。编译器错误和警告会告诉您遇到的确切问题