Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Checkpoint1/.vscode/launch.json
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}"
}
,]
}
15 changes: 15 additions & 0 deletions Checkpoint1/.vscode/tasks.json
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"
}
]
}
196 changes: 195 additions & 1 deletion Checkpoint1/Checkpoint1.cs
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();
Copy link

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.


Copy link

Choose a reason for hiding this comment

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



}
}
}
}