设计一个名为IntSrquence的接口表示整数数列,该接口包含boolean hasNext()和int next()两个方法。定义一个名为RandomIntSequence的类实现IntSequence接口,其中包含一个private整形变量n。在hasNext()方法中随机生成一个两位整数,存储到变量n中,然后返回ture。在next()方法中返回n的值。
时间: 2024-02-25 13:58:28 浏览: 24
好的,我理解了你的问题。下面是针对你的问题的回答:
首先,我们需要定义一个名为IntSequence的接口,如下所示:
```java
public interface IntSequence {
boolean hasNext();
int next();
}
```
接口中包含了两个方法,一个是判断是否还有下一个元素,一个是返回下一个元素。
然后,我们需要定义一个名为RandomIntSequence的类来实现IntSequence接口,如下所示:
```java
import java.util.Random;
public class RandomIntSequence implements IntSequence {
private int n;
public boolean hasNext() {
return true;
}
public int next() {
Random rand = new Random();
n = rand.nextInt(90) + 10;
return n;
}
}
```
在RandomIntSequence中,我们实现了IntSequence接口,并且生成了一个两位整数存储在n中。在hasNext()方法中,我们返回了true,表示该数列中一直有下一个元素。在next()方法中,我们返回了n的值。
这样,我们就实现了一个名为RandomIntSequence的类来表示整数数列,并且可以随机生成两位整数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)