Fall 2020
October 23, 2020
int c;
string message;
int count;
bool res;
Console.WriteLine("Please, enter an integer.");
message = Console.ReadLine();
res = int.TryParse(message, out c);
count = 0; // The user has 3 tries: count will be 0, 1, 2, and then we default.
while (!res && count < 3)
{
count++;
if (count == 3)
{
c = 1;
Console.WriteLine("I'm using the default value 1.");
}
else
{
Console.WriteLine("The value entered was not an integer.");
Console.WriteLine("Please, enter an integer.");
message = Console.ReadLine();
res = int.TryParse(message, out c);
}
}
Console.WriteLine("The value is: " + c);Do … while Loopsstring ent = "";
int b;
int sum = 0;
bool flag;
do
{
Console.WriteLine("Enter an integer, or anything to quit.");
ent = Console.ReadLine();
flag = int.TryParse(ent, out b);
sum += b;
} while (flag);
Do … while with complex conditionusing System;
class Loan
{
private string account;
private char type;
private int cscore;
private decimal amount;
private decimal rate;
public Loan()
{
account = "Unknown";
type = 'o';
cscore = -1;
amount = -1;
rate = -1;
}
public Loan(string nameP, char typeP, int cscoreP, decimal needP, decimal downP)
{
account = nameP;
type = typeP;
cscore = cscoreP;
if (cscore < 300)
{
Console.WriteLine("Sorry, we can't accept your application");
amount = -1;
rate = -1;
}
else
{
amount = needP - downP;
switch (type)
{
case ('a'):
rate = .05M;
break;
case ('h'):
if (cscore > 600 && amount < 1000000M)
rate = .03M;
else
rate = .04M;
break;
case ('o'):
if (cscore > 650 || amount < 10000M)
rate = .07M;
else
rate = .09M;
break;
}
}
}
public override string ToString()
{
string typeName = "";
switch (type)
{
case ('a'):
typeName = "an auto";
break;
case ('h'):
typeName = "a house";
break;
case ('o'):
typeName = "another reason";
break;
}
return "Dear " + account + $", you borrowed {amount:C} at {rate:P} for "
+ typeName + ".";
}
}using System;
class Program
{
static void Main()
{
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("Do you want a loan for an Auto (A, a), a House (H, h), or for some Other (O, o) reason?");
char type = Console.ReadKey().KeyChar; ;
Console.WriteLine();
string typeOfLoan;
if (type == 'A' || type == 'a')
{
type = 'a';
typeOfLoan = "an auto";
}
else if (type == 'H' || type == 'h')
{
type = 'h';
typeOfLoan = "a house";
}
else
{
type = 'o';
typeOfLoan = "some other reason";
}
Console.WriteLine($"You need money for {typeOfLoan}, great.\nWhat is your current credit score?");
int cscore = int.Parse(Console.ReadLine());
Console.WriteLine("How much do you need, total?");
decimal need = decimal.Parse(Console.ReadLine());
Console.WriteLine("What is your down paiement?");
decimal down = decimal.Parse(Console.ReadLine());
Loan myLoan = new Loan(name, type, cscore, need, down);
Console.WriteLine(myLoan);
}
}