![](/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.