下面是想用random产生两个不同的数,打印出来。。为什么我写的类会产生相同的随机数??
以下是代码:
import java.util.Random;public class studentInfo { /**
* @param args
*/
String _name;
String _age;
double _;
public void set_(){
_=new Random().nextDouble()*100;
}
public double get_(){
return this._;
}
public static void main(String[] args) {
studentInfo student=new studentInfo();
student.set_();
System.out.println(student.get_());
student.set_();
System.out.println(student.get_());
}}
以下是代码:
import java.util.Random;public class studentInfo { /**
* @param args
*/
String _name;
String _age;
double _;
public void set_(){
_=new Random().nextDouble()*100;
}
public double get_(){
return this._;
}
public static void main(String[] args) {
studentInfo student=new studentInfo();
student.set_();
System.out.println(student.get_());
student.set_();
System.out.println(student.get_());
}}
你在声明一个对象试试看,小弟也是刚学习的。
在main中new一个Random,然后作为参数传入set_(),这样应该可以
也就是是用同一个Random对象