为什么我输入男和女也一直循环
时间: 2024-03-09 15:45:41 浏览: 17
如果你输入"男"或"女"也一直循环,那么可能是因为输入的字符串中包含了空格或其他非法字符,导致strcmp函数比较时无法匹配到"男"或"女"。你可以尝试使用scanf函数的"%[^\n]"格式化字符串,这样输入的字符串就可以包含空格了。例如:
```
printf("插入学生的性别:");
scanf("%[^\n]", new_stu.gender);
while (strcmp(new_stu.gender,"男") && strcmp(new_stu.gender,"女")) {
printf("输入错误,请输入男或女:");
scanf("%[^\n]", new_stu.gender);
}
```
这段代码可以接受包含空格的字符串,并且可以去掉输入字符串中的换行符。
相关问题
用java编写一个程序录入心仪的男(女)朋友。 条件:年龄和身高,超出此范围重新录入,一直到正确为止。
以下是一个可能的解决方案:
```
import java.util.Scanner;
public class Boyfriend {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int age = 0;
double height = 0.0;
while (true) {
System.out.print("请输入男(女)朋友的年龄:");
age = input.nextInt();
if (age < 18 || age > 35) {
System.out.println("年龄不符合要求,请重新输入!");
} else {
break;
}
}
while (true) {
System.out.print("请输入男(女)朋友的身高(单位:米):");
height = input.nextDouble();
if (height < 1.5 || height > 1.9) {
System.out.println("身高不符合要求,请重新输入!");
} else {
break;
}
}
System.out.println("您录入的男(女)朋友信息如下:");
System.out.println("年龄:" + age + "岁");
System.out.println("身高:" + height + "米");
input.close();
}
}
```
程序使用了Scanner类来实现用户输入的功能,使用while循环来确保输入的年龄和身高在指定范围内。如果输入的值不符合要求,则程序会打印出相应的提示信息,要求用户重新输入。最后,程序会输出录入的男(女)朋友的信息。
编写一个程序来交换两个变量的值使用指针实现使用for循环和while循环编写一个程序来打印1~10之间的所有偶数从键盘输入若干学生的信息(姓名、学号、性别、年龄等),输出年龄最大同学的信息,统计一下男女生比例。
好的,我可以回答你的问题。首先回答第一个问题:
编写一个程序来交换两个变量的值,使用指针实现
下面是一个简单的 C 代码示例,可以交换两个变量的值,使用指针实现:
```c
#include <stdio.h>
void swap(int *a, int *b);
int main() {
int x = 10, y = 20;
printf("交换前:x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("交换后:x = %d, y = %d\n", x, y);
return 0;
}
// 定义一个函数来交换两个变量的值
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
```
在这个示例中,我们定义了一个 `swap` 函数来交换两个变量的值,函数使用指针作为参数,这样可以通过指针来修改变量的值。
接下来回答第二个问题:
使用 for 循环和 while 循环编写一个程序来打印 1~10 之间的所有偶数
下面是一个简单的 C 代码示例,使用 for 循环和 while 循环来打印 1~10 之间的所有偶数:
```c
#include <stdio.h>
int main() {
int i;
printf("使用 for 循环打印 1~10 之间的所有偶数:\n");
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
printf("\n使用 while 循环打印 1~10 之间的所有偶数:\n");
i = 1;
while (i <= 10) {
if (i % 2 == 0) {
printf("%d ", i);
}
i++;
}
printf("\n");
return 0;
}
```
在这个示例中,我们使用了两个循环来打印 1~10 之间的所有偶数,循环从 1 开始,一直到 10。如果当前的数字是偶数,就打印它。
最后回答第三个问题:
从键盘输入若干学生的信息(姓名、学号、性别、年龄等),输出年龄最大同学的信息,统计一下男女生比例
下面是一个简单的 C 代码示例,从键盘输入若干学生的信息,输出年龄最大同学的信息,统计一下男女生比例:
```c
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int id;
char gender;
int age;
};
int main() {
struct Student students[100];
int n, i, max_age = 0, max_index = 0;
int male_count = 0, female_count = 0;
printf("请输入学生人数:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("请输入第 %d 个学生的信息:\n", i + 1);
printf("姓名:");
scanf("%s", students[i].name);
printf("学号:");
scanf("%d", &students[i].id);
printf("性别(M/F):");
scanf(" %c", &students[i].gender);
printf("年龄:");
scanf("%d", &students[i].age);
if (students[i].age > max_age) {
max_age = students[i].age;
max_index = i;
}
if (students[i].gender == 'M') {
male_count++;
} else if (students[i].gender == 'F') {
female_count++;
}
}
printf("\n年龄最大的学生信息:\n");
printf("姓名:%s\n", students[max_index].name);
printf("学号:%d\n", students[max_index].id);
printf("性别:%c\n", students[max_index].gender);
printf("年龄:%d\n", students[max_index].age);
printf("\n男女生比例为:%d:%d\n", male_count, female_count);
return 0;
}
```
在这个示例中,我们使用了一个结构体 `Student` 来表示学生的信息,然后定义了一个数组 `students` 来存储输入的学生信息。我们使用循环来输入每个学生的信息,并且在输入的过程中统计男女生的人数。最后,我们遍历数组,找到年龄最大的学生,并输出他的信息。
希望这些示例代码能够帮助你理解如何在 C 语言中进行编程。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)