如何让程序显示随机数?这就是我到目前为止所做的:
import java.util.Scanner;
public class Lab3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int empCount = 0;
System.out.printf("Enter Employee Name: " );
String name =input.next();
Employee e1= new Employee(name);
System.out.printf( "Employee count is " + empCount+ "\n" );
System.out.println("Employee " + empnumber + e1.getName() + " earned:");
}
}
这是我生成随机数的代码,但我似乎无法正常返回。
public static int getNumber(){
Random random = new Random();
int empnumber;
empnumber = 1000 + random.nextInt( 9999 );
return empnumber;
}
答案 0 :(得分:1)
变量empnumber
未在全局范围内声明,它只在getNumber
方法的范围内,因此您无法访问它。
改为使用
System.out.println("Employee " + Lab3.getNumber() + " earned: " + "");
答案 1 :(得分:0)
只需按照以下方法调用main中的静态方法:
int randomNumber = getNumber();
答案 2 :(得分:0)
添加:
int empCount = Lab3.getNumber();