forked from AustinCodingAcademy/csharp-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Checkpoint1 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamestglh
wants to merge
3
commits into
master
Choose a base branch
from
checkpoint1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Checkpoint1 #7
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| // Use IntelliSense to find out which attributes exist for C# debugging | ||
| // Use hover for the description of the existing attributes | ||
| // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "name": ".NET Core Launch (console)", | ||
| "type": "coreclr", | ||
| "request": "launch", | ||
| "preLaunchTask": "build", | ||
| // If you have changed target frameworks, make sure to update the program path. | ||
| "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/Checkpoint1.dll", | ||
| "args": [], | ||
| "cwd": "${workspaceFolder}", | ||
| // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window | ||
| "console": "internalConsole", | ||
| "stopAtEntry": false, | ||
| "internalConsoleOptions": "openOnSessionStart" | ||
| }, | ||
| { | ||
| "name": ".NET Core Attach", | ||
| "type": "coreclr", | ||
| "request": "attach", | ||
| "processId": "${command:pickProcess}" | ||
| } | ||
| ,] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "version": "2.0.0", | ||
| "tasks": [ | ||
| { | ||
| "label": "build", | ||
| "command": "dotnet", | ||
| "type": "process", | ||
| "args": [ | ||
| "build", | ||
| "${workspaceFolder}/Checkpoint1.csproj" | ||
| ], | ||
| "problemMatcher": "$msCompile" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,206 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Checkpoint1 | ||
| { | ||
| class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| Console.WriteLine("Hello World!"); | ||
|
|
||
| MainMenu(); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would you do this without each "sub program" calling MainMenu again? |
||
| int userChoice = 0; | ||
| void MainMenu(){ | ||
| Console.WriteLine("Welcome to Checkpoint 1. Please select a choice:"); | ||
| Console.WriteLine("1. Divisible by 3 tool"); | ||
| Console.WriteLine("2. Addition tool"); | ||
| Console.WriteLine("3. Factorial tool"); | ||
| Console.WriteLine("4. Guess a number"); | ||
| Console.WriteLine("5. Find largest number"); | ||
|
|
||
| if (Int32.TryParse(Console.ReadLine(), out userChoice)) | ||
| { | ||
| if (userChoice == 1) | ||
| { | ||
| Problem1(); | ||
| } | ||
| else if (userChoice == 2) | ||
| { | ||
| Problem2(); | ||
| } | ||
| else if (userChoice == 3) | ||
| { | ||
| Problem3(); | ||
| } | ||
| else if (userChoice == 4) | ||
| { | ||
| Problem4(); | ||
| } | ||
| else if (userChoice == 5) | ||
| { | ||
| Problem5(); | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("That was an invalid choice"); | ||
| MainMenu(); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("That was an invalid choice"); | ||
| MainMenu(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| // 1- Write a program to count how many numbers between 1 and 100 are divisible by 3 with no remainder. Display the count on the console. | ||
| void Problem1(){ | ||
| int divisibleCount = 0; | ||
| for (int i = 1; i <= 100; i++) | ||
| { | ||
| if (i % 3 == 0) | ||
| { | ||
| divisibleCount++; | ||
| Console.WriteLine(i + " is divisible by 3. That makes " + divisibleCount + " numbers so far that are divisible by 3."); | ||
| } | ||
| } | ||
| Console.WriteLine("Counting done. There are " + divisibleCount + "numbers between 1 and 100 that are divisble by 3."); | ||
| MainMenu(); | ||
| } | ||
|
|
||
|
|
||
| // 2- Write a program and continuously ask the user to enter a number or "ok" to exit. Calculate the sum of all the previously entered numbers and display it on the console. | ||
| void Problem2(){ | ||
| int playerChoice = 0; | ||
| List<int> numbers = new List<int>(); | ||
| GetNumber(); | ||
|
|
||
| void GetNumber(){ | ||
| Console.WriteLine("Please enter a number, or type ok if you are done entering numbers"); | ||
| string input = Console.ReadLine(); | ||
| if (Int32.TryParse(input, out playerChoice)) | ||
| { | ||
| numbers.Add(playerChoice); | ||
| GetNumber(); | ||
| } | ||
| else if (input == "ok") | ||
| { | ||
| int sum = numbers.Sum(); | ||
| Console.WriteLine("The sum of the numbers you entered is " + sum); | ||
| MainMenu(); | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("Your choice was invalid." ); | ||
| GetNumber(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // 3- Write a program and ask the user to enter a number. Compute the factorial of the number and print it on the console. For example, if the user enters 5, the program should calculate 5 x 4 x 3 x 2 x 1 and display it as 5! = 120. | ||
| void Problem3(){ | ||
| int userNumber = 0; | ||
| int i = 0; | ||
|
|
||
| Console.WriteLine("Enter a number: "); | ||
| // userNumber = int.Parse(Console.ReadLine()); | ||
| if (Int32.TryParse((Console.ReadLine()), out userNumber)) | ||
| { | ||
| int factorial = userNumber; | ||
| for (i = userNumber - 1; i >= 1; i--) | ||
| { | ||
| factorial = factorial * i; | ||
| } | ||
| Console.WriteLine("The factorial of " + userNumber + " is " + factorial); | ||
| MainMenu(); | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("Your choice was invalid." ); | ||
| Problem3(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| // 4- Write a program that picks a random number between 1 and 10. Give the user 4 chances to guess the number. If the user guesses the number, display “You won"; otherwise, display “You lost". (To make sure the program is behaving correctly, you can display the secret number on the console first.) | ||
| void Problem4(){ | ||
| int chances = 4; | ||
| int guess = 0; | ||
| Random rnd = new Random(); | ||
| int compChoice = rnd.Next(1, 10); | ||
|
|
||
| GuessingTime(); | ||
|
|
||
| void GuessingTime(){ | ||
| Console.WriteLine("The computer mastermind has chosen a number between 1 and 10. Can you guess it correctly?"); | ||
| while (chances > 0) | ||
| { | ||
| Console.WriteLine("Type a number between 1 and 10 and press Enter"); | ||
| if (Int32.TryParse((Console.ReadLine()), out guess)) | ||
| { | ||
| if ((guess > 10) || (guess < 1 )) | ||
| { | ||
| Console.WriteLine("Your selection is invalid."); | ||
| Problem4(); | ||
| } | ||
| else if (guess == compChoice) | ||
| { | ||
| Console.WriteLine("You guessed right with " + chances + " chances left! You win!"); | ||
| MainMenu(); | ||
| } | ||
| else | ||
| { | ||
| chances--; | ||
| Console.WriteLine("You guessed wrong! You have " + chances + " chances left!"); | ||
|
|
||
| } | ||
|
|
||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("Your choice was invalid." ); | ||
| Problem4(); | ||
| } | ||
| } | ||
| Console.WriteLine("You ran out of guesses! You lose! The computer mastermind's number was " + compChoice + "! You have perished."); | ||
| MainMenu(); | ||
| } | ||
|
|
||
| } | ||
| // 5- Write a program and ask the user to enter a series of numbers separated by comma. Find the maximum of the numbers and display it on the console. For example, if the user enters “5, 3, 8, 1, 4", the program should display 8. | ||
| void Problem5(){ | ||
| Console.WriteLine("Input a bunch of numbers separated by commas, then press enter"); | ||
| string input = Console.ReadLine(); | ||
|
|
||
| string[] numbers = (input).Split(","); | ||
| int[] numbersInt = new int[numbers.Length]; | ||
|
|
||
| for (int i = 0; i < numbers.Length; i++) | ||
| { | ||
| if (Int32.TryParse(numbers[i], out numbersInt[i])) | ||
| { | ||
|
|
||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("One of your inputs was invalid, please try again"); | ||
| Problem5(); | ||
| } | ||
|
|
||
| } | ||
| Console.WriteLine("The largest number you entered is " + numbersInt.Max()); | ||
| MainMenu(); | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems odd that you would define all these functions inside Main(), instead of as static methods in Program. Not wrong, just want to make sure you know you do not have to do that.