= 1 n switch(n) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf(" />

1到7的整数怎么编程

时间:2025-01-27 05:11:40 网络游戏

C语言

使用`switch`语句

```c

include

int main() {

int n;

while(scanf("%d", &n) == 1) {

switch(n) {

case 1: printf("Monday\n"); break;

case 2: printf("Tuesday\n"); break;

case 3: printf("Wednesday\n"); break;

case 4: printf("Thursday\n"); break;

case 5: printf("Friday\n"); break;

case 6: printf("Saturday\n"); break;

case 7: printf("Sunday\n"); break;

default: printf("Invalid input\n");

}

}

return 0;

}

```

使用`if-else`语句

```c

include

int main() {

int n;

while(scanf("%d", &n) == 1) {

if (n >= 1 && n <= 7) {

switch(n) {

case 1: printf("Monday\n"); break;

case 2: printf("Tuesday\n"); break;

case 3: printf("Wednesday\n"); break;

case 4: printf("Thursday\n"); break;

case 5: printf("Friday\n"); break;

case 6: printf("Saturday\n"); break;

case 7: printf("Sunday\n"); break;

}

} else {

printf("Invalid input\n");

}

}

return 0;

}

```

Python

使用`input`函数和`if`语句

```python

while True:

number = int(input("请输入1到7之间的数字: "))

if 1 <= number <= 7:

if number == 1:

print("星期一")

elif number == 2:

print("星期二")

elif number == 3:

print("星期三")

elif number == 4:

print("星期四")

elif number == 5:

print("星期五")

elif number == 6:

print("星期六")

elif number == 7:

print("星期日")

else:

print("输入错误,请输入1到7之间的数字")

```

Java

使用`Scanner`类和`if`语句

```java

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (scanner.hasNextInt()) {

int number = scanner.nextInt();

if (number >= 1 && number <= 7) {

switch (number) {

case 1:

System.out.println("星期一");

break;

case 2:

System.out.println("星期二");

break;

case 3:

System.out.println("星期三");

break;

case 4:

System.out.println("星期四");

break;

case 5:

System.out.println("星期五");

break;

case 6:

System.out.println("星期六");

break;

case 7:

System.out.println("星期日");

break;

}

} else {

System.out.println("输入错误,请输入1到7之间的数字");

}

}

scanner.close();

}

}

```

这些示例展示了如何在不同的编程语言中处理1到7的整数输入,并根据输入输出相应的星期几。你可以根据自己的需求和编程环境选择合适的方法。