为了账号安全,请及时绑定邮箱和手机立即绑定

源代码如下所示,无法将java.util.random中的next(int)应用于int?

源代码如下所示,无法将java.util.random中的next(int)应用于int?

犯罪嫌疑人X 2023-04-22 17:13:08
import java.util.*;class Shape{public void draw(){}public void erase(){}}class Circle extends Shape{public void draw(){System.out.println("Circle.draw()");}//public void erase(){System.out.println("Circle.erase()");}}class Square extends Shape{public void draw(){System.out.println("Square.draw()");}public void erase(){System.out.println("Square.erase()");}}class Triangle extends Shape{public void draw(){System.out.println("Triangle.draw()");}public void erase(){System.out.println("Triangle.erase()");}}class RandomShapeGenerator{public Random rand=new Random(47);public Shape next(){switch(rand.nextInt(3)){default:case 0: return new Circle();case 1: return new Square();case 2: return new Triangle();}}}public class Shapes{public static Random gen=new Random();public static void main(String[] args){Shape[] s=new Shape[9];//把图形加入数组中 for(int i=0;i<s.length;i++)s[i]=gen.next();for(Shape shp:s)shp.draw();}}
查看完整描述

1 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

public static Random gen=new Random();
改成
public static RandomShapeGenerator gen=new RandomShapeGenerator();

查看完整回答
反对 回复 2023-04-25
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信