为什么我不能循环这个程序 [英] Why cant I loop in this program

查看:84
本文介绍了为什么我不能循环这个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

int main(void)
{

	int grade;
	unsigned int counter;
	int pass;
	int fail;

	pass = 0;
	counter = 1;


	do{
	while (counter <=10)
	{
	
	printf("Enter student's result (1=pass, 2=fail)\n\n");
	scanf("%d", &grade);

	if (grade == 1) 
	{pass = pass + 1;}		

	counter = counter + 1;

	}
	if (pass > 8)
		printf("Good job instructor!\n");
	else printf("default\n");
	}while(1 != pass, 2 != fail);

	
		system("pause");


		return 0
		;

}





我的尝试:



改进程序以验证其输入。在任何输入上,如果输入的值是

而不是1或2,则保持循环直到用户输入正确的值。



What I have tried:

Improve the program to validate its inputs. On any input, if the value entered is
other than 1 or 2, keep looping until the user enters a correct value.

推荐答案

}while(1 != pass, 2 != fail);



不是有效的声明。将变量放在运算符的左侧更好。


is not a valid statement. And it is much better to put the variable to the left of the operator.


可能你的意思是:

Probably, you mean either:
}while(pass != 1 && fail != 2);

其中&&表示AND:左边和右边的部分必须为true才能使整个条件成立。

Where "&&" means "AND": both the left and right parts must to true for the whole condition to be true.

}while(pass != 1 || fail != 2);

哪里||表示或:如果左侧和右侧中的任何一个为真,则整个条件为真。

逗号打破一个语句,结果将是右侧 - 左侧与返回值无关。

Where "||" means "OR": if either one of the left and right hand sides is true, the whole condition is true.
The comma "breaks" a statement, and the result will be the right hand side - the left hand side is irrelevant in the returned value.


学会正确缩进代码,显示其结构,有助于阅读和理解。

Learn to indent properly your code, it show its structure and it helps reading and understanding.
#include <stdio.h>

int main(void)
{

	int grade;
	unsigned int counter;
	int pass;
	int fail;

	pass = 0;
	counter = 1;


	do{
		while (counter <=10)
		{

			printf("Enter student's result (1=pass, 2=fail)\n\n");
			scanf("%d", &grade);

			if (grade == 1)
			{pass = pass + 1;}

			counter = counter + 1;

		}
		if (pass > 8)
			printf("Good job instructor!\n");
		else printf("default\n");
	}while(1 != pass, 2 != fail);


	system("pause");


	return 0
	;

}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

< a href =https://notepad-plus-plus.org/> Notepad ++ Home [ ^ ]

ultraedit [ ^ ]



建议:编写代码时使用一致的样式,它也有助于阅读。

建议:在使用之前初始化变量。你试图测试失败的价值,但你从来没有给它一个价值。

建议:不要在同一行上包装很多东西,它会让事情变得更加困难而没有任何优势。


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Advice: use consistent style when writing code, it also help reading.
Advice: always initialize a variable before using it. you try to test the value of fail but you never gave it a value.
Advice: di not pack many things on same line, it makes things more difficult to read without any advantage.


这篇关于为什么我不能循环这个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆