需要创建一个程序,以找出有多少羊让你成为一条新龙。 [英] Need create a program for finding out how many sheep gets you a new dragon.

查看:67
本文介绍了需要创建一个程序,以找出有多少羊让你成为一条新龙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作业#1

C编程简介 - COP 3223



目标

1.给予学生在打字,编译和运行简单程序时练习。

2.学习如何阅读用户的输入。

3.学习如何使用赋值语句和算术表达式进行计算



简介:谁不爱龙?

关于龙和龙训练的电影今年夏天很受欢迎。你的朋友并没有停止谈论龙是多么棒,培训它们有多酷。为了逗你的朋友,你决定创建一系列关于龙的程序。



问题:Dragon Feeding(dragonfeeding.c)

每个人都知道龙从蛋中孵化,需要大量的肉才能长成全尺寸骑龙。在这个程序中,我们将计算本月新龙需要多少只绵羊。



您需要询问用户龙的重量以磅为单位。然后,您可以使用以下公式确定他或她每天需要的蛋白质克数:



重量磅/ 2.2 * 1.5



如果龙岛上的每只绵羊都含有200克蛋白质,请确定当月(30天)需要多少只绵羊并将此信息打印给用户。



输入规格

1.重量为正整数。



输出规格

使用以下格式输出绵羊数量作为整数:



你需要X只绵羊为你的新龙!



输出样本

以下是运行程序的一些示例输出。请注意,这些样本不是全面的测试。您应该根据上面给出的规范,使用与此处显示的数据不同的数据来测试您的程序。在下面的示例运行中,为了清晰和易于阅读,用户输入以斜体显示,而程序输出以粗体显示。 (注意:当你实际运行你的程序时,根本不应该出现粗体或斜体。为了清楚起见,这些只是在本说明书中使用。)



什么我试过了:



Assignment #1
Introduction to C Programming – COP 3223

Objectives
1. To give students practice at typing in, compiling and running simple programs.
2. To learn how to read in input from the user.
3. To learn how to use assignment statements and arithmetic expressions to make calculations

Introduction: Who doesn’t love dragons?
Movies about dragons and dragon training were very popular this summer. Your friend has not stopped talking about how awesome dragons are and how cool it would be to train them. To amuse your friend, you have decided to create a series of programs about dragons.

Problem: Dragon Feeding (dragonfeeding.c)
Everyone knows that dragons hatch from eggs and need lots of meat to grow into full sized riding dragons. In this program, we will calculate how many sheep we will need for our new dragon for this month.

You will need to ask the user for the weight of the dragon in pounds. You can then determine the number of grams of protein he or she will need each day using the following formula:

Weight in pounds / 2.2 * 1.5

If each sheep on Dragon Island has 200g of protein, determine how many sheep will be necessary for the month (30 days) and print this information to the user.

Input Specification
1. The weight will be a positive integer.

Output Specification
Output the number of sheep as a whole number using the format below:

You will need X sheep for your new dragon!

Output Sample
Below are some sample outputs of running the program. Note that these samples are NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given above. In the sample run below, for clarity and ease of reading, the user input is given in italics while the program output is in bold. (Note: When you actually run your program no bold or italics should appear at all. These are simply used in this description for clarity’s sake.)

What I have tried:

/* Ashley Prieto
 * This program prints the number of sheep you will need for a new dragon
 */

 #include <stdio.h>

 int main() {
    //variable declaration
    int pounds;

    //prompt user for input information
    printf("How much does your dragon weigh?\n");
    scanf("%d", £s);

    //calculations
    Weight in pounds = pounds / 2.2 * 1.5;


    //output results
    printf("Your dragon weighs %d pounds! \n", pounds);

    return 0;
 }

推荐答案

第一个问题出在你的计算中,你从未将结果设置为变量。要解决此问题,您需要添加另一个变量,并将其值设置为计算结果,如下所示:
The first problem is in your calculations, you never set the result to a variable. To fix that, you would need to add another variable and set the value of it to the results of the calculation like so:
#include <stdio.h>

int main() {
   //variable declaration
   int weight, sheep;

   //prompt user for input information
   printf("How much does your dragon weigh?\n");
   scanf("%d", &weight);

   //calculations
   sheep = pounds / 2.2 * 1.5;


   //output results
   printf("Your dragon weighs %d pounds! \n", pounds);

   return 0;
}



接下来是在分配提示中,它表示计算是每天,而结果必须是每月(30天)。所以你的计算实际应该是


Next thing is that in the assignment prompt, it states that the calculation is per day while the results must be per month (30 days). So your calculation should actually be

sheep = (weight/ 2.2 * 1.5 * 30) / 200;



200是每只羊的蛋白质克数提供。当提示询问龙每月需要多少只绵羊时,你输出龙的重量结果。要解决这个问题,您需要将输出更改为


The 200 is the grams of protein each sheep provides. You outputted the results for how much the dragon weighs when the prompt asks for how many sheep the dragon needs per month. To fix that, you would need to change your output to

printf("You will need %d sheep for your new dragon!\n", sheep);

毕竟是固定的,它应该是这样的:

After all that is fixed, it should look like this:

#include <stdio.h>

int main() {
   //variable declaration
   int weight, sheep;

   //prompt user for input information
   printf("How much does your dragon weigh?\n");
   scanf("%d", &weight);

   //calculations
   sheep = weight / 2.2 * 1.5 * 30 / 200;


   //output results
   printf("Your dragon weighs %d pounds!\n", weight);

   return 0;
}



希望这会有所帮助。


Hope this helped.


这篇关于需要创建一个程序,以找出有多少羊让你成为一条新龙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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