java编写输出1到999中所有不包含5的整数
时间: 2023-04-04 13:03:31 浏览: 173
您好,以下是我对您的问题的回答:
可以使用循环语句和条件语句来实现输出1到999中所有不包含5的整数的功能。具体的代码如下:
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 999; i++)
{
if (i % 10 != 5 && i / 10 % 10 != 5 && i / 100 % 10 != 5)
{
printf("%d ", i);
}
}
return 0;
}
以上是我对您的问题的回答,希望能够帮到您。
相关问题
用JAVA编写封装成一个方法:打印1到100之内的整数,但数字中包含9的要跳过,每行输出5个满足条件的数,之间用空格分隔。
### 回答1:
以下是用JAVA编写的封装成一个方法,实现在1到100之间输出不包含数字9的整数,每行输出5个数字的功能:
```java
public static void printNumbersWithoutNine() {
int count = 0;
for (int i = 1; i <= 100; i++) {
if (i % 10 == 9 || i / 10 == 9) {
continue; // 如果数字中包含9则跳过
}
System.out.print(i + " ");
count++;
if (count % 5 == 0) {
System.out.println(); // 每行输出5个数字
}
}
}
```
调用该方法即可实现题目要求的功能:
```java
printNumbersWithoutNine();
```
输出结果为:
```
1 2 3 4 5
6 7 8 10 11
12 13 14 15 16
17 18 20 21 22
23 24 25 26 27
28 30 31 32 33
34 35 36 37 38
40 41 42 43 44
45 46 47 48 50
51 52 53 54 55
56 57 58 60 61
62 63 64 65 66
67 68 70 71 72
73 74 75 76 77
78 80 81 82 83
84 85 86 87 88
90 91 92 93 94
95 96 97 98 100
```
### 回答2:
下面是一个使用Java编写的满足要求的方法:
```java
public class Main {
public static void main(String[] args) {
printNumbers();
}
public static void printNumbers() {
int count = 0;
for (int i = 1; i <= 100; i++) {
if (hasNine(i)) {
continue; // 如果数字包含9,则跳过
}
System.out.print(i + " ");
count++;
if (count % 5 == 0) {
System.out.println(); // 每行输出5个数
}
}
}
public static boolean hasNine(int num) {
while (num != 0) {
if (num % 10 == 9) {
return true; // 如果数字包含9,则返回true
}
num /= 10;
}
return false; // 数字不包含9
}
}
```
这个方法中,我们使用一个变量`count`来记录已经输出的满足条件的数的数量。遍历从1到100的整数,如果某个数包含9,则使用`continue`语句跳过这个数。如果不包含9,则输出这个数,并将`count`加1。当已经输出了5个数时,换行输出下一行。最终,我们就可以得到满足条件的1到100之间的整数。
### 回答3:
可以使用以下的JAVA代码实现这个需求:
```java
public class PrintNumbers {
public static void main(String[] args) {
printNumbersWithNoNine();
}
public static void printNumbersWithNoNine() {
int count = 0; // 用于计数每行已输出的数字个数
for (int i = 1; i <= 100; i++) {
if (i % 10 == 9 || i / 10 == 9) { // 如果数字中包含9,则跳过当前数字
continue;
}
System.out.print(i + " "); // 输出满足条件的数字
count++;
if (count == 5) { // 每行输出5个数字后换行
System.out.println();
count = 0; // 重置计数器
}
}
}
}
```
这段代码中的`printNumbersWithNoNine()`方法遍历了从1到100的所有整数,并检查每个数字是否包含9。如果数字中包含9,则`continue`语句将跳过当前循环,不执行后续的输出语句。如果数字不包含9,则输出该数字,并将计数器`count`加1。当`count`等于5时,换行并重置计数器,然后继续循环。
用Java编写一个每次随机生成10个0(包括)到100之间的随机正整数,并打印输出。
好的,我可以帮你解决这个问题。你可以尝试以下代码:
```java
import java.util.Random;
public class RandomNumbers {
public static void main(String[] args) {
Random random = new Random();
for (int i = 0; i < 10; i++) {
int randomNumber = random.nextInt(101); // 生成0到100的随机数
System.out.println(randomNumber); // 输出随机数
}
}
}
```
这个程序会使用Java内置的Random类来生成随机数,并且每次运行都会打印出10个随机数。希望能对你有所帮助。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)