用JavaWrite a program that generates 200 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers named counts, to store the counts for the number of 0s, 1s, . . . , 9s.)
时间: 2023-03-19 07:22:20 浏览: 109
下面是一个使用Java编写的程序,生成200个在0到9之间的随机整数,并显示每个数字的计数。
```java
import java.util.Random;
public class CountRandomIntegers {
public static void main(String[] args) {
int[] counts = new int[10];
Random rand = new Random();
for (int i = 0; i < 200; i++) {
int randomNumber = rand.nextInt(10);
counts[randomNumber]++;
}
for (int i = 0; i < counts.length; i++) {
System.out.println("Number of " + i + "s: " + counts[i]);
}
}
}
```
这个程序使用了一个名为`counts`的整型数组来存储数字0到9的计数。我们使用`java.util.Random`类生成随机整数,并将它们加入到计数数组中。
在循环结束后,我们遍历计数数组并显示每个数字的计数。
相关问题
Write a program that generates a vector with 30 random integers between –20 and 20 and then finds the sum of all the elements that are divisible by 3.use matlab
Here's the MATLAB code to generate a vector of 30 random integers between -20 and 20, and then find the sum of all the elements that are divisible by 3:
```
% Generate a vector of 30 random integers between -20 and 20
vec = randi([-20, 20], [1, 30]);
% Find the sum of all the elements that are divisible by 3
divisible_by_3 = mod(vec, 3) == 0;
sum_divisible_by_3 = sum(vec(divisible_by_3));
```
In the first line, we use the `randi` function to generate a row vector of 30 random integers between -20 and 20. In the second line, we create a logical vector `divisible_by_3` that is true for any element of `vec` that is divisible by 3 (i.e., the remainder after division by 3 is 0). Finally, we use the `sum` function to sum up only the elements of `vec` that are divisible by 3. The result is stored in the variable `sum_divisible_by_3`.
2. (2 points) Please find the dates without any placed order between the time range. (Hint: write a recursive query to create date sequence and use the select statement in Q1 to assign user-defined variables so that you can have a neat recursive query.)
Assuming we have a table `orders` with columns `order_id`, `order_date`, and `customer_id`, we can use the following recursive query to generate a sequence of dates between a specified time range and then find the dates without any placed order:
```
WITH RECURSIVE dates AS (
SELECT DATE('2021-01-01') AS date
UNION ALL
SELECT date + INTERVAL 1 DAY
FROM dates
WHERE date < DATE('2021-12-31')
), orders_count AS (
SELECT order_date, COUNT(*) AS num_orders
FROM orders
WHERE order_date BETWEEN DATE('2021-01-01') AND DATE('2021-12-31')
GROUP BY order_date
)
SELECT dates.date
FROM dates
LEFT JOIN orders_count ON dates.date = orders_count.order_date
WHERE orders_count.num_orders IS NULL;
```
In this query, we first define a recursive CTE called `dates` that generates a sequence of dates between `2021-01-01` and `2021-12-31`. Then, we define another CTE called `orders_count` that counts the number of orders for each date between the same time range.
Finally, we join the `dates` CTE with the `orders_count` CTE using a left join and filter for the rows where `num_orders` is null. These are the dates without any placed order.
相关推荐
![](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)