简体   繁体   English

C#中的简单数学问题

[英]Simple Math Issue in C#

I have this program that takes 3 scores out of a possible 200 each then is supposed to get the average and display the percentage. 我有这个程序,每个可能200分中取3分,然后应该得到平均值并显示百分比。 but when i input numbers i get 00.0 as an answer. 但是当我输入数字时,我得到00.0作为答案。 What could i be doing wrong? 我能做错什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int Score1;
            int Score2;
            int Score3;

            Console.Write("Enter your score (out of 200 possible) on the first test: ");

            Score1 = int.Parse(Console.ReadLine());
            Console.Write("Enter your score (out of 200 possible) on the second test: ");

            Score2 = int.Parse(Console.ReadLine());
            Console.Write("Enter your score (out of 200 possible on the third test: ");

            Score3 = int.Parse(Console.ReadLine());
            Console.WriteLine("\n");

            float percent = (( Score1+ Score2+ Score3) / 600);

            Console.WriteLine("Your percentage to date is: {0:00.0}", percent);
            Console.ReadLine();
        }
    }
}

You're dividing an integer by an integer - that always uses integer arithmetic, even when you're assigning the result to a float . 您将整数除以整数 - 即使您将结果赋值给float ,也始终使用整数运算。 The simplest way of fixing that is to make one of the operands a float, eg 最简单的修复方法是使其中一个操作数浮动,例如

float percent = (Score1 + Score2 + Score3) / 600f;

Note that this won't actually give you a percentage though - it'll give you a number between 0 and 1 (assuming the inputs are between 0 and 200). 请注意,这实际上不会给你一个百分比 - 它会给你一个介于0和1之间的数字(假设输入介于0和200之间)。

To get an actual percentage, you need to multiply by 100 - which is equivalent to only dividing by 6: 要获得实际百分比,您需要乘以100 - 相当于仅除以6:

float percent = (Score1 + Score2 + Score3) / 6f;

You are not calculating a percentage. 您没有计算百分比。 Imagine the user enters maximum score: 200 + 200 + 200 = 600, that gets divided by 600 = 1. If any one of the scores get entered below 200, the total will be less than 1 and get rounded down to 0. You should store them as floats (to make sure you lose no information to rounding) and multiply by 100. 想象一下,用户输入最高分:200 + 200 + 200 = 600,除以600 = 1.如果任何一个分数输入低于200,则总数将小于1并向下舍入为0。您应该将它们存储为浮点数(以确保您没有丢失任何舍入信息)并乘以100。

It's a datatype problem, I think. 我认为这是一个数据类型问题。 You should typecast one of the scores to float, since your variable percent is float, and all the scores is int. 您应该将其中一个分数转换为浮动,因为您的变量百分比是浮点数,并且所有分数都是int。

using System;

namespace stackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            int Score1;
            int Score2;
            int Score3;

            Console.Write("Enter your score (out of 200 possible) on the first test: ");
            Score1 = int.Parse(Console.ReadLine());
            Console.Write("Enter your score (out of 200 possible) on the second test: ");
            Score2 = int.Parse(Console.ReadLine());
            Console.Write("Enter your score (out of 200 possible on the third test: ");
            Score3 = int.Parse(Console.ReadLine());
            Console.WriteLine("\n");
            var percent = ((Score1 + Score2 + Score3) / 6D);
            Console.WriteLine("Your percentage to date is: {0:00.0}", percent);
            Console.ReadLine();

        }
    } 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM