【发布时间】:2021-06-18 05:51:31
【问题描述】:
下面给出的代码不起作用:
我的任务是,
- 编写一个程序,生成 500 个随机整数并将它们存储到一个数组中。整数值将介于 1 - 1000 之间。
- 将 500 个随机整数打印到
.txt file - main 方法将控制程序的运行。 (菜单等)
- 使用 if/else 梯形图或 switch case 创建一个菜单来控制程序的操作。菜单应该在 main 中。
- 创建一个名为 Common_Algo 的“程序员创建的类”
- 从你在Task中创建的
.txt file读取500个随机整数 2: - 使用类构造函数用 .txt 文件中的随机整数填充数组。 500 整数数组 (arr[500]) 将是“程序员创建的类”中的一个实例变量 - Common_Algo
- 以下所有方法都将与 Common_Algo 类中的 500 整数数组一起封装。
- 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