This lab serves multiple goals:
TryParse
statements,TryParse
,int.TryParse
and
double.TryParse
,In your IDE, copy and paste the following:
Console.WriteLine("Enter… something!");
int answer;
bool valid = int.TryParse(Console.ReadLine(), out answer);
Console.WriteLine($"returns: {valid}\nvalue:{answer}");
For each input in the table below:
true
or false
).TryParse
operation.The first few lines are given as examples; your task is to complete the
rest of the table. You will need to update the program by replacing all
the occurrences of int
with double
to test if your answers were
correct in the second half of the table.
"160519"
| true
| 160519
| true
| 160519
|"9432.0"
| false
| 0
| true
| 9432.0
|"nope"
| false
| 0
| false
| 0
|"12,804"
| | | | |"+5102"
|||||"2+2"
|||||" -322 "
|||||"(72);"
|||||"000"
|||||"78 095"
|||||Question:
After completing the table, can you detect a pattern between “returns”
and “value”?
For the following problems, perform this series of steps:
If the provided input is not valid, request new input from the user until the user provides valid input. The beginning of the first and second problems are given to get you started.
Write a loop that displays: Enter yes to quit:
and then checks the
user’s input. Consider any of these variations to mean yes: “yes”,
“YES”, “y”, “Y”. Once the user enters yes, exit the loop.
Ask the user to enter a positive integer between (and including) 2 and 100. Validate the input, compute the sum of integers starting from 1 up to the integer the user entered, and display that sum. Here are examples:
Do the following problem using the decimal
type. Ask the user to
enter any numbers which can be positive, negative, or zero. Ignore
all non-numeric inputs using TryParse
. Choose an appropriate
sentinel value to enable the user to indicate when they are done.
Compute and display the average of all the numbers that the user
entered. If the user didn’t enter any numbers, display “You did not
enter any numbers”.
Here is an example of the desired execution, where the user input is underlined, and hitting “enter” is represented by ↵:
Please enter a number, or "Done" to exit:
8̲↵
Please enter a number, or "Done" to exit:
2̲↵
Please enter a number, or "Done" to exit:
H̲o̲l̲d̲ ̲o̲n̲↵
Please enter a number, or "Done" to exit:
-̲5̲↵
Please enter a number, or "Done" to exit:
D̲o̲n̲e̲
The average of the numbers you entered is 1.66666666667.
This part is focused on input validation with classes. It requires reading a lengthy (but not very complicated) class implementation and then, improving it. It is difficult and is designed to offer an interesting challenge. However, you should be able to complete such exercises by the end of the semester without too many difficulties.
Start by downloading the LoanCalculator solution which mixes classes and decision structures. Spend some time studying the implementation to understand what the program is doing and how it is doing it.
Next edit the Program.cs
file of the LoanCalculator
solution to add
the following validation features:
A
, a
, H
, h
, O
, or o
for the loan type will be asked again; they will be asked until they
give a valid answer.char
class to make the program more readable – you
will be able to greatly simplify the if
statement that checks the
loan type.ToString
method and in the application program instead of doing it
manually.