我希望该程序从用户那里得到一个数字,然后输入a,b,...,但只需为用户给出的最后一个成绩写a,b,... [英] i want this program get a number from user then enter a, b ,... but it just write a,b,... for the last grade that the user give

查看:93
本文介绍了我希望该程序从用户那里得到一个数字,然后输入a,b,...,但只需为用户给出的最后一个成绩写a,b,...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// 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>

推荐答案

如果您说自己遇到了什么问题,这将有所帮助.
我要进行的第一个更改是:
将循环内容括在一个块中.总是值得做的.
It would help if you said what problem you are getting.
The first change I would make is:
Enclose your loop content in a block. It is always worth doing.
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;

看到区别了吗?


这部分代码不适用于您的提议:
for(int i = 0; i< m mode ="hold"> cin>> n;

如果要具有m个数字,则可以创建一个长度为m的动态数组,并将每个数字存储在单独的数组成员中.在程序的最后,您应该删除指向创建的动态数组的指针.
This part of your code is not proper for your propose:
for( int i = 0 ; i<m mode="hold"> cin >> n;

if you want to have m numbers, you can create a dynamic array with the length of m and store each number in a separate array member. At the end of your program, you should delete the pointer to created dynamic array.


这篇关于我希望该程序从用户那里得到一个数字,然后输入a,b,...,但只需为用户给出的最后一个成绩写a,b,...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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