问题描述
// grade.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
//class grade
//{
//
//public:
//
// void getgrade();
// void print();
//private:
//};
//
//void grade ::getgrade()
//{
//
//
//}
int main()
{
int m , n ;
cout << "how many students do you have ? " << endl ;
cin >> m ;
cout << "plz enter the grades :" << endl ;
for( int i = 0 ; i< m ; i++ )
cin >> n;
for( int i = 0 ; i< m ; i++ )
if(n>=17 && n<=20)
{
cout << ''A'' << endl ;
}
if(n>=13 && n<=16)
{
cout << ''B'' << endl;
}
if(n>=10 && n<=12)
{
cout << ''C'' <<endl;
}
if(n>=6 && n<=9)
{
cout << ''D'' <<endl;
}
if(n>=0 && n<=5)
{
cout << ''E''<<endl;
}
/*switch(n)
{
case 1:
cout << ''A'' << endl ;
break;
case 2:
cout << ''B'' << endl;
break;
case 3:
cout << ''C'' <<endl;
break;
case 17:
cout << ''D'' <<endl;
break;
}*/
return 0;
}
</iostream>
推荐答案
for( int i = 0 ; i< m ; i++ )
if(n>=17 && n<=20)
{
cout << ''A'' << endl ;
}
if(n>=13 && n<=16)
{
cout << ''B'' << endl;
}
这是在一个循环中执行一个"if",还是两个?
Does this execute one "if" in a loop, or two?
for( int i = 0 ; i< m ; i++ )
{
if(n>=17 && n<=20)
{
cout << ''A'' << endl ;
}
if(n>=13 && n<=16)
{
cout << ''B'' << endl;
}
}
更清晰:它在循环内执行两个"if".
尝试一下:它可能会解决您的问题.如果没有,请尝试告诉我们您的问题是什么!
例如,在我的程序中,如果我写20,16,程序就会给我(B& B),但它不正确,它一定是(A& B)".
好的.这就是我在上面描述的内容,但是稍微严重些.
您的代码:
Is a lot clearer: it executes both "if" inside the loop.
Try it: it may get rid of your problem. If it doesn''t, try telling us what your problem is!
"for example in my program if i write 20 , 16 the program give me (B & B)but it is not correct it must be (A & B)"
Ok. It is what I was describing above, but slightly more serious.
Your code:
cout << "plz enter the grades :" << endl ;
for( int i = 0 ; i< m ; i++ )
cin >> n;
for( int i = 0 ; i< m ; i++ )
if(n>=17 && n<=20)
表示该行
cin >> n;
被计算"m"次.然后进入下一个for
循环.for
循环或if
条件仅在它之后立即执行该语句.
is exectuted "m" times. Then it moves on to the next for
loop.
A for
loop, or an if
condition, only executes the statement immediately after it.
for (int i = 0; i < 10; i++)
printf("This line prints 10 times\n");
printf("This line prints once\n");
或
if (myInteger == 0)
printf("This prints only when myInteger is zero\n");
printf("This prints every time\n");
如果要执行多个语句,则需要将其括在花括号中以使其成为语句块:
If you want to execute more than one statement, you need to make it a statement block by enclosing it in curly braces:
for (int i = 0; i < 10; i++)
{
printf("This line prints 10 times\n");
printf("So does this one\n");
}
printf("This line prints once\n");
或
if (myInteger == 0)
{
printf("This prints only when myInteger is zero\n");
printf("So is this one\n");
}
printf("This prints every time\n");
更改您的代码:将花括号放在要执行的所有位置-即使在if
条件下为单行.当您可以将它们放到外面时,您会在稍后理解-但是现在,请始终将它们放入.
Change your code: put curly braces in EVERYWHERE you want to execute - even if it is a single line in a if
condition. You will understand later when you can leave them out - but for now, always put them in.
cout << "plz enter the grades :" << endl ;
for( int i = 0 ; i< m ; i++ )
{
cin >> n;
for( int i = 0 ; i< m ; i++ )
{
if(n>=17 && n<=20)
{
cout << ''A'' << endl ;
}
if(n>=13 && n<=16)
{
cout << ''B'' << endl;
}
if(n>=10 && n<=12)
{
cout << ''C'' <<endl;
}
if(n>=6 && n<=9)
{
cout << ''D'' <<endl;
}
if(n>=0 && n<=5)
{
cout << ''E''<<endl;
}
}
}
return 0;
看到区别了吗?
这篇关于我希望该程序从用户那里得到一个数字,然后输入a,b,...,但只需为用户给出的最后一个成绩写a,b,...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!