需要帮助完成学校任务 [英] Need help with a school task

查看:73
本文介绍了需要帮助完成学校任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!在学校里,我们一直在学习C语言编程。在学习之前我知道很多编程但是我仍然坚持我们必须完成的项目。我们有5个项目,我已经完成了4个项目,非常简单。我们刚刚学习了基础知识,比如循环,if,else,数组和我已经知道的简单东西但是在这个项目中我必须扫描5个数字并以相反的顺序打印它们我实际上并不知道我怎么能这样做..我有一个例子,但我想知道是否有更好的方法吗?



Hello everyone! In school we've been learning programming in C. Well i knew alot of programming before we learned it but still I'm stuck on a project we had to finish. We got 5 projects and i've completed 4 of them, really easy. We've just been learning the basics like loops, if, else, arrays and easy stuff which i already knew but in this project i have to scan 5 numbers and print them in the opposite order and i don't actually know how i can do this.. I have an example here but i wonder if there are any better ways to do it?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int number1, number2, number3, number4, number5; //Numbers to input
  
  printf("Enter five numbers. First number: ");
  scanf("%d", &number1);
  printf("Second number: ");
  scanf("%d", &number2);
  printf("Third number: ");
  scanf("%d", &number3);
  printf("fourth number: ");
  scanf("%d", &number4);
  printf("fifth number: ");
  scanf("%d", &number5);
  
  printf("%d, %d, %d, %d, %d", number5, number4, number3, number2, number1);
  
  getch();	
  return 0;
}





看起来不是很好,是吗?我可以使用数组或其他东西,以更好的方式做到这一点吗?如果是这样,怎么样?



It doesn't look very good done, does it? Could i use an array or something and do this in a better way? if so, how?

推荐答案

你已经提到了一种自己做的方法!



为什么不在输入时将数字分配到数组中,然后通过从数组末尾开始并向后工作来显示它们?
You've mentioned one way of doing it for yourself!

Why not assign the numbers into an array as they are entered, then display them by starting at the end of the array and working backwards?


http://www.cprogramming.com/tutorial/c/lesson8.html

检查链接有一个带char的程序数组,你可以判断如何使用整数数组



在程序中循环扫描并使用循环打印

这个使用数组的优点是它会减少代码。

无需声明太多变量number1,number2 ......,number5也不需要写太多行来扫描



快乐编码!

:)
http://www.cprogramming.com/tutorial/c/lesson8.html
check link there is a program with char array you can judge from that how do this with integer array

in that program values scanned in a loop and printed using a loop
this is advantage of using array it will reduce code.
no need to declare too many variables number1,number2...,number5 and also no need to write too many lines to scan

Happy Coding!
:)


//简单数组

// simple array
int main(int argc, char *argv[])
{
    int index;
    int number[5]; //Numbers to input
  
    printf("Enter five numbers\n");

    for (index = 0; index < 5; index++)
    {
        printf("Next number: ");
        scanf("%d", &number[index]);
    }

    for (index = 4; index > -1; index--)
    {
        printf("%d", number[index]);
        if (index > 0)
            printf(", ");
    }
    printf("\n");

    getch();	
    return 0;
}


这篇关于需要帮助完成学校任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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