在c#中没有接收到正确的输出

本文关键字:输出 | 更新日期: 2023-09-27 18:04:58

所以我正在做家庭作业,由于某种原因,我的变量没有给我正确的输出。以6、7、8、9、10作为评判分数,以1.2作为难度等级,我的最终跳水得分应该是9.6。但由于某种原因,我收到了8…什么好主意吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rickerson_Bret_iLab3
{
class Program
{
    static void Main(string[] args)
    {
        string wouldContinue;
        do
        {
            string diverName;
            string diverCity;
            double degreeofDiff = 0;
            double scoreJudge = 0;
            bool validDegree = false;
            double totalJudgeScore = 0;
            int i = 1;
            double highJudgeScore = 0;
            double lowJudgeScore = 0;
            Console.WriteLine("Enter the diver's name...");
            diverName = Convert.ToString(Console.ReadLine());
            Console.WriteLine("Enter the diver's city...");
            diverCity = Convert.ToString(Console.ReadLine());
            while (validDegree == false)
            {
                Console.WriteLine("Enter the degree of difficulty for this dive...");
                degreeofDiff = Convert.ToDouble(Console.ReadLine());
                if (degreeofDiff < 1.00 || degreeofDiff > 1.67)
                {
                    Console.WriteLine("Re-enter a valid degree of difficulty (Valid Range: 1.00 - 1.67");
                    validDegree = false;
                }
                else
                {
                    validDegree = true;
                }
            }
            while (i < 6)
            {
                Console.WriteLine("Enter the judge #" + i + " score...");
                scoreJudge = Convert.ToDouble(Console.ReadLine());
                if (scoreJudge > 10 || scoreJudge < 1)
                {
                    bool validScore = false;
                    while (validScore == false)
                    {
                        Console.WriteLine("Enter a valid Judge Score for judge #" + i + "...");
                        scoreJudge = Convert.ToDouble(Console.ReadLine());
                        if (scoreJudge > 10 || scoreJudge < 1)
                        {
                            validScore = false;
                        }
                        else
                        {
                            validScore = true;
                        }
                    }
                }
                if (scoreJudge > highJudgeScore)
                {
                    highJudgeScore = scoreJudge;
                    Console.WriteLine(highJudgeScore);
                }
                if (scoreJudge < lowJudgeScore)
                {
                    lowJudgeScore = scoreJudge;
                    Console.WriteLine(lowJudgeScore);
                }
                i++;
                totalJudgeScore = totalJudgeScore + scoreJudge;
                Console.WriteLine(totalJudgeScore);
                Console.WriteLine(scoreJudge);
            }
            double highLow = highJudgeScore + lowJudgeScore;
            totalJudgeScore = totalJudgeScore - highLow;
            totalJudgeScore = (totalJudgeScore / 3) * degreeofDiff;
            Console.WriteLine("Diver: " + diverName);
            Console.WriteLine("Diver City: " + diverCity);
            Console.WriteLine("Dive Degree of Difficulty: " + degreeofDiff);
            Console.WriteLine("Dive Score: " + totalJudgeScore);
            Console.WriteLine("Would you like to enter another diver? Enter y for yes or n for no...");
            wouldContinue = Convert.ToString(Console.ReadLine());
            wouldContinue.ToUpper();


        } while (wouldContinue == "Y");
    }
}

}

我想添加。我试图通过让它显示变量来验证它是否正确地接受数据,因为它经过或任何时候变量被操纵…它似乎是正确的,但最后是当我有变量"totalJudgeScore"的问题

Edit2:我后来发现,由于某种原因,代码没有正确地遵循最后2个if语句。它每次都将"scoreJudge"存储到"highJudgeScore"answers"lowJudgeScore",并且错误地覆盖数据。

在c#中没有接收到正确的输出

你的lowJudgeScore在被设置为0后永远不会被设置。您应该将其默认值设置为10或更高,以便正确设置。

试试这个:

double lowJudgeScore = 10.0;

在循环过程中,lowJudgeScore永远不会被设置。添加else块并初始化lowJudgeScore

                if (scoreJudge < lowJudgeScore)
                {
                    lowJudgeScore = scoreJudge;
                    Console.WriteLine(lowJudgeScore);
                }
                //Add this else block to initialize the low score variable
                else if (lowJudgeScore == 0)
                {
                    lowJudgeScore = scoreJudge;
                }