![](/img/trans.png)
[英]Generate 10 Random Integers storing them in an Array and then calling a Method to display the Array
[英]Generate random integers with a range and place them into this array
我在一个问题上工作了5个小时,在书上和网上搜索过,但我仍然无法解决这个问题,所以请帮我检查一下程序出了什么问题。 而图片是这个程序的要求。
//imports
import java.util.Scanner;
import java.util.Random;
public class Lab09 // Class Defintion
{
public static void main(String[] arugs)// Begin Main Method
{
// Local variables
final int SIZE = 20; // Size of the array
int integers[] = new int[SIZE]; // Reserve memory locations to store
// integers
int RanNum;
Random generator = new Random();
final char FLAG = 'N';
char prompt;
prompt = 'Y';
Scanner scan = new Scanner(System.in);
// while (prompt != FLAG);
// {
// Get letters from User
for (int index = 0; index < SIZE; index++) // For loop to store letters
{
System.out.print("Please enter the number #" + (index + 1) + ": ");
integers[index] = RanNum(1, 10);
}
// call the printStars method to print out the stars
// printArray(int intergers, SIZE);
} // End Main method
/***** Method 1 Section ******/
public static int RanNum(int index, int SIZE);
{
RanNum = generator.nextInt(10) + 1;
return RanNum;
} // End RanNum
/***** Method 2 Section ******/
public static void printArray(int integers, int SIZE) {
// Print the result
for (int index = SIZE - 1; index >= 0; index--) {
System.out.print(integers[index] + " ");
}
} // End print integers
} // End Lab09
正如蒂姆·比格莱森和卡亚曼所说,你应该把一切都放在问题中,而不仅仅是外在形象。
您的代码中有很多错误。 下面的代码将编译并运行,但我建议您看一看并了解它已完成的操作。
错误:
如果您要声明一个方法,请确保在声明的末尾使用 {。 你有:
public static int RanNum(int index, int SIZE);
应该:
public static int RanNum(int index, int SIZE){
// Code here
}
您还应该在 main 方法之外声明,以便可以在整个程序中访问它们。
如果您将数组作为参数传递,则在您的方法中,参数也应该是数组类型。
你有:
public static void printArray(int integers, int SIZE) {
// Code her
}
应该
public static void printArray(int[] integers, int SIZE) {
// Code her
}
这是完整的代码:
package test;
import java.util.Random;
import he java.util.Scanner;
public class Test {
//Local variables
public static final int SIZE = 20; //Size of the array
static int integers[] = new int[SIZE]; //Reserve memory locations to store integers
static int randomNumber;
static Random generator = new Random();
static String prompt;
static final String p = "yes";
static boolean repeat = true;
static Scanner input = new Scanner(System.in);
Test() {
}
/***** Method 1 Section ******/
public static int RanNum (int low, int high) {
randomNumber = generator.nextInt(high-low) + low;
return randomNumber;
} //End RanNum
/***** Method 2 Section ******/
public static void printArray(int[] intArray, int SIZE) {
//Print the result
for (int i = 0; i < SIZE; i++) {
System.out.print (intArray[i] + " ");
}
} //End print integers
public static void main (String [] arugs) {
do {
for (int i = 0; i < SIZE; i++) {
integers[i] = RanNum(1, 10);
}
printArray(integers, SIZE);
System.out.println("Do you want to generate another set of random numbers? Yes/No");
prompt = input.nextLine();
} while(prompt.equals(p));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.