random功能:即可随机产生0~x之间的一切整数。而且它是一个函数,不能单独使用。
random举例:
program exercise(input,output)
var i,a:longint
begin
for i := 1 to 10 do
begin
a:=random(100)
write(a,' ')
end
end.
问题1:randomGraph在generategraph方法内未定义,所以提示无法找到。
问题2:二维数组generategraph,只声明了,没有定义(即没有赋值)。
下面是修改后的代码:
import java.util.Scannerimport java.util.Random
public class test1
{
public static int [][] randomGraph
public static void main(String[] args){
int Vertices
Random x = new Random()
System.out.println("Pleas enter the number of vertices which should be larger than 2 and less than 11\n")
Scanner sc=new Scanner(System.in)
Vertices = sc.nextInt()
randomGraph = generategraph(Vertices)
System.out.print("test1")
System.out.println()
test1(Vertices)
}
public static int[][] generategraph(int n){
if(n<3||n>11)
{
System.out.printf("输入的整数超出范围 重新输入\n")
System.exit(0)
}
int[][] Graph= new int[n][n]
Random r=new Random()
for(int i=0 i<n i++){
for(int j=0j<nj++)
{
if(i==j)
Graph[i][j]=0
else
Graph[i][j]=r.nextInt(9)+1
}
}
return Graph
}
public static void test1(int a){
for(int i=0 i<ai++){
for(int j=0 j<aj++){
System.out.print(randomGraph[i][j]+" ")
}
System.out.println()
}
System.out.println()
}
}
} else {yNew = paY[i-1] - yD
}
if (yNew >YSIZE) {
yNew -= yD
} else { if (yNew <0)
yNew += yD
}
paY[i] = yNew
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)