gpt4 book ai didi

c# - C# 中的简单数学问题

转载 作者:太空狗 更新时间:2023-10-29 22:05:25 25 4
gpt4 key购买 nike

我有这个程序,它从可能的 200 分中取 3 分,然后应该得到平均值并显示百分比。但是当我输入数字时,我得到 00.0 作为答案。我可能做错了什么?

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();
}
}
}

最佳答案

您将一个整数除以一个整数 - 它始终使用整数运算,即使您将结果分配给 float。解决这个问题的最简单方法是将其中一个操作数设为 float ,例如

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

请注意,这不会实际上给你一个百分比 - 它会给你一个介于 0 和 1 之间的数字(假设输入介于 0 和 200 之间)。

要获得实际百分比,您需要乘以 100 - 这相当于仅除以 6:

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

关于c# - C# 中的简单数学问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3698800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com