if (x != 1 || x != 2) { printf("输入有误,请重新输入!"); break; } return;
时间: 2023-09-20 16:02:56 浏览: 32
这段代码用于判断变量 x 是否为 1 或 2,如果不是,则输出一条错误信息并跳出当前函数。
然而,这段代码存在逻辑错误。条件表达式 `x != 1 || x != 2` 中的或运算符表示只要 x 不等于 1 或者不等于 2,条件就成立,因此无论 x 是什么值,条件表达式都为真,代码块中的语句都会被执行,这显然不是我们想要的结果。
应该使用与运算符 &&,将条件表达式修改为 `x != 1 && x != 2`,表示只有当 x 不等于 1 且不等于 2 时,条件才成立,代码块中的语句才会被执行。
正确的代码应该是:
```
if (x != 1 && x != 2) {
printf("输入有误,请重新输入!");
break;
}
return;
```
相关问题
printf("plz enter the flag:"); while ( 1 ) { v6 = getch(); v32[v31] = v6; if ( !(_BYTE)v6 || v32[v31] == 13 ) break; if ( v32[v31] == 8 ) { printf("\b\b"); --v31; } else { printf("%c", v32[v31++]); } } v8 = 0; for ( i = 0; i < 17; ++i ) { if ( v32[i] != byte_415768[*(&v9 + i)] ) v8 = 1; } if ( v33 != 49 || v34 != 48 || v35 != 50 || v36 != 52 || v37 != 125 ) v8 = 1; v32[v31] = 0; printf("\r\n"); if ( v8 ) { printf("u r wrong\r\n\r\n"); main(v3, v4, v5); } else { printf("u r right!\r\n"); } system("pause"); HIDWORD(v1) = v0; LODWORD(v1) = 0; return v1; }
这段代码是什么意思?
这段代码是一段C语言代码,包括一些变量的定义和赋值,以及一些判断语句和函数调用。程序提示用户输入一个flag,并在while循环中获取用户输入的字符,并将其存储在数组v32的第v31个位置。如果输入的字符是空字符或回车符,则结束循环。如果输入的字符是退格符,则将光标回退两个位置,并将v31减1。否则,将输入的字符打印出来,并将v31加1。循环将一直执行,直到用户输入了空字符或回车符为止。之后,使用for循环遍历一个长度为17的数组v32,如果数组v32中的某个元素不等于另一个数组byte_415768中对应位置的元素,则变量v8被赋值为1。另外,如果变量v33、v34、v35、v36、v37分别不等于49、48、50、52、125,则变量v8也被赋值为1。最后,数组v32中的第v31个元素被赋值为0,并输出一个换行符。如果变量v8的值为1,则输出"u r wrong",并且调用main函数重新开始程序。否则,输出"u r right!"。然后,使用system函数调用操作系统命令"pause",以便程序在执行完后暂停,等待用户按下任意键继续。最后,变量v1被赋值为0,并返回。
优化这段代码 System.out.printf("请输入年份"); int year = input.nextInt(); if (year>0){ System.out.printf("请输入月份"); int month = input.nextInt(); if (month >= 1 && month <= 12){ System.out.printf("请输入天数"); int day = input.nextInt(); int days =0; if (month == 2){ if (year%4==0 && year%100 !=0 || year%400==0){ days = 29; }else { days = 28; } } else if (month == 4 | month == 6 | month == 9 | month == 11) { days = 30; } else if (month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10| month == 12){ days = 31; }else { System.out.printf("天数输入错误"); } if(day >= 1 && day <= days) { System.out.println(year + "-" + month + "-" + day); }else { System.out.printf("天数输入错误"); } }else { System.out.printf("月份输入错误"); } }else { System.out.printf("年份输入错误"); }
到12){ System.out.printf("请输入日期"); int day = input.nextInt(); if (day >= 1 && day <= 31){ System.out.printf("%d年%d月%d日", year, month, day); }else{ System.out.printf("日期输入有误"); } }else{ System.out.printf("月份输入有误"); } }else{ System.out.printf("年份输入有误"); }
可以将输入年份、月份、日期的部分提取出来,写成一个方法,减少重复代码。同时,可以加入异常处理,避免用户输入非法字符导致程序崩溃。代码如下:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = getYear(input);
int month = getMonth(input);
int day = getDay(input);
System.out.printf("%d年%d月%d日", year, month, day);
}
public static int getYear(Scanner input) {
int year = ;
while (true) {
try {
System.out.printf("请输入年份");
year = input.nextInt();
if (year > ) {
break;
} else {
System.out.printf("年份输入有误");
}
} catch (InputMismatchException e) {
System.out.printf("年份输入有误");
input.next();
}
}
return year;
}
public static int getMonth(Scanner input) {
int month = ;
while (true) {
try {
System.out.printf("请输入月份");
month = input.nextInt();
if (month >= 1 && month <= 12) {
break;
} else {
System.out.printf("月份输入有误");
}
} catch (InputMismatchException e) {
System.out.printf("月份输入有误");
input.next();
}
}
return month;
}
public static int getDay(Scanner input) {
int day = ;
while (true) {
try {
System.out.printf("请输入日期");
day = input.nextInt();
if (day >= 1 && day <= 31) {
break;
} else {
System.out.printf("日期输入有误");
}
} catch (InputMismatchException e) {
System.out.printf("日期输入有误");
input.next();
}
}
return day;
}
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)