…
作業(yè)1
…
要求用戶輸入用戶名和密碼,只要不是admin、888888就一直提示用戶名或密碼錯誤,請重新輸入
…
代碼
…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string username="";
string password="";
do
{
Console.WriteLine("請輸入用戶名");
username = Console.ReadLine();
Console.WriteLine("請輸入密碼");
password = Console.ReadLine();
if (("admin" != username) && ("888888" != password))
{
Console.WriteLine("用戶名或密碼錯誤,請重新輸入");
}
} while (("admin" != username) & ("888888" != password));
Console.WriteLine("登陸成功");
Console.ReadKey();
}
}
}
…
效果
…

…
作業(yè)2
…
不斷要求用戶輸入一個數(shù)字,然后打印這個數(shù)字的二倍,當(dāng)用戶輸入q的時候程序退出
循環(huán)體:提示用戶輸入數(shù)字 轉(zhuǎn)換 打印這個數(shù)字的2倍
循環(huán)條件:輸入的不能是q
…
代碼
…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入數(shù)字");
string number=Console.ReadLine();
if (number != "q")
{
try
{
int num = Convert.ToInt32(number);
Console.WriteLine("這個數(shù)字的2倍是{0}", num *= 2);
}
catch
{
Console.WriteLine("輸入有誤,請重新輸入");
}
}
else
{
Console.WriteLine("你輸入的是q,程序退出");
} while (number != "q");
Console.WriteLine("執(zhí)行完成");
Console.ReadKey();
}
}
}
…
效果
…

…
作業(yè)3
…
不斷要求用戶輸入一個數(shù)字(假定用戶輸入的都是正整數(shù)),當(dāng)用戶輸入end的時候顯示剛才輸入的數(shù)字中的最大值
…
代碼
…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入數(shù)字");
string number = Console.ReadLine();
int max =Convert.ToInt32( number);
while (number != "end")
{
number = Console.ReadLine();
if (number != "end")
{
try
{
int num = Convert.ToInt32(number);
if (num > max)
{
max = num;
}
}
catch
{
Console.WriteLine("輸入有誤,請重新輸入");
}
}
else
{
Console.WriteLine("您剛才輸入的數(shù)字中最大值是{0}", max);
}
}
Console.ReadKey();
}
}
}
…
效果
…

…