ScriptUO

Casa de TrailMyx => Programming / Operating Systems => .NET Programming => Topic started by: dxrom on November 18, 2012, 09:15:14 PM

Title: Learing C#
Post by: dxrom on November 18, 2012, 09:15:14 PM
So, I'm in my third, almost fourth week of a C# course for college and this week we played around with Loops, I wrote a program for class (The topic was given out by the teacher and was the same for everyone). I think it turned out well, but after reading through some class mate's posts I'm wondering if it could have been better (Note, we're not aloud to use arrays, only simple loops). Because I notice alot of students complaining about having to use a lot of Break; Continue; statements in order to leave the loop. My program however didn't utilize any. I'll toss up source for input.

Code: [Select]
// ---------------------------------------------------------------
// Programming Assignment:    LAB3A
// Developer:                 MP
// Date Written:              November 18, 2012
// Purpose:                   Diver's Score Calculator
// ---------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LAB3A
{
    class Program
    {
        static void Main(string[] args)
        {
            do
            {
                //vars
                double rating, score, scoreTotal = 0, highScore = 0, lowScore = 10, dedScore = 0;
                string dName, dHome;

                //Prompt for name/city/rating
                Console.Write("Diver's Name: ");
                dName = Console.ReadLine();
                Console.Write("Diver's City: ");
                dHome = Console.ReadLine();
                //rating stuff
                Console.Write("Dive degree of difficulty: ");
                rating = Double.Parse(Console.ReadLine());
                while (rating < 1 || rating > 1.67)
                {
                    Console.WriteLine("Invalid degree of difficulty - Please reenter (Valid Range: 1 - 1.67)");
                    Console.Write("Dive degree of difficulty: ");
                    rating = Double.Parse(Console.ReadLine());
                }
                //Judge Scoring
                for (int i = 1; i <= 5; i++)
                {
                    Console.WriteLine("Judge #{0} score: ", i);
                    score = Double.Parse(Console.ReadLine());
                    while (score < 0 || score > 10)
                    {
                        Console.WriteLine("Invalid score - Please reenter (Valid Range: 0 - 10)");
                        Console.WriteLine("Judge #{0} score: ", i);
                        score = Double.Parse(Console.ReadLine());
                    }
                    if (score > highScore)
                    {
                        highScore = score;
                    }
                    if (score < lowScore)
                    {
                        lowScore = score;
                    }
                    scoreTotal = score + scoreTotal;
                }
                dedScore = highScore + lowScore;
                scoreTotal = (scoreTotal - dedScore) / 3 * rating;
                //Display stuffs
                Console.WriteLine("Diver: {0}", dName);
                Console.WriteLine("City: {0}", dHome);
                Console.WriteLine("Dive Score: {0}", scoreTotal);
                Console.WriteLine("Do you want to process another diver (Y/N)?");
            } while (Console.ReadLine().ToUpper() != "N");
            break;
        }
    }
}
Title: Re: Learing C#
Post by: TrailMyx on November 19, 2012, 07:07:14 AM
Those complainers are ones that are also against gotos.  They are only happy when execution flow terminates in a "controlled" way.  I've always felt that if a tool is in the toolbox, you might give it a whirl.
Title: Re: Learing C#
Post by: dxrom on November 19, 2012, 10:48:45 AM
Well, I got full credit for it. So I assume it was done correctly :D
Title: Re: Learing C#
Post by: Crome969 on November 23, 2012, 06:50:56 AM
Instead of twice using if you could use
Code: [Select]
if(statement){...}else if(statement){}Think on rtc applications the double jumping in if statement could decrease performance... But i think the flow of your code is good.. Nothing to complain :)