【问题标题】:How to create random.txt file then randomly 500 random integers in the file then use that file to run common algorithms on them?如何创建 random.txt 文件,然后在文件中随机 500 个随机整数,然后使用该文件在它们上运行常用算法?
【发布时间】:2021-06-18 05:51:31
【问题描述】:

下面给出的代码不起作用:

我的任务是,

  1. 编写一个程序,生成 500 个随机整数并将它们存储到一个数组中。整数值将介于 1 - 1000 之间。
  2. 将 500 个随机整数打印到 .txt file
  3. main 方法将控制程序的运行。 (菜单等)
  4. 使用 if/else 梯形图或 switch case 创建一个菜单来控制程序的操作。菜单应该在 main 中。
  5. 创建一个名为 Common_Algo 的“程序员创建的类”
  6. 从你在Task中创建的.txt file读取500个随机整数 2:
  7. 使用类构造函数用 .txt 文件中的随机整数填充数组。 500 整数数组 (arr[500]) 将是“程序员创建的类”中的一个实例变量 - Common_Algo
  8. 以下所有方法都将与 Common_Algo 类中的 500 整数数组一起封装。
  9. Common_Algo 类外部的主要方法将控制 程序的运行。

实施方法以实现下面给出的每个结果。
(您的菜单将调用方法。所有方法都将在同一个数组上运行。)

输入 1. 输出所有值
输入 2. 输出所有值的总和和值的平均值
输入 3. 输出所有奇数
输入 4. 输出所有偶数\ 输入 5. 输出中间值(Values in the Middle)
输入 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); 
            
    }
}

【问题讨论】:

  • 编译器错误和警告告诉你遇到的确切问题

标签: java arrays algorithm random


【解决方案1】:

不要试图一次完成所有事情。一步一步做。您的老师已经将问题分解为多个步骤。分别为每个步骤编写代码。测试该代码以确保其正常工作,修复您发现的任何错误。是的,这确实意味着阅读和理解您发现的任何编译错误。因为你一次只写一篇,所以每篇不应该有太多错误。

当您有一段经过测试且可以工作的代码时,将它与您迄今为止编写的其他部分集成并测试新的组合代码。再次修复所有错误。

这种循序渐进的方法意味着您永远不需要修复一大块代码,只有更容易处理的小块。您逐步构建最终程序,一路测试和修复。

【讨论】:

    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多