C#作业样本⑸-- 测试

1 view
Skip to first unread message

bluefin

unread,
Jun 6, 2008, 4:41:48 PM6/6/08
to C# Guide
(注:以下教程适合初学者,介绍C#。下载.NET Framework 2.0 版本到你的Windows
2000 SP4 或者Windows XP SP2。)


①打开notepad.exe

②输入以下内容:

using System;
using System.Windows.Forms;

public class frmMain : Form
{
public static void Main()
{
bool flag;
float operand1; string strOperand1;
float operand2; string strOperand2;
float answer; string strResult;
// ask for 1st value and 2nd value
Console.Write ( "Enter 1st whole number: ");
strOperand1 = Console.ReadLine( );
Console.Write ( "Enter 2nd whole number: ");
strOperand2 = Console.ReadLine( );
// convert text to numeric for 1st value
flag = float.TryParse(strOperand1, out operand1);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "Input Error");
return;
}
// convert text to numeric for 2nd value
flag = float.TryParse(strOperand2, out operand2);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "Input Error");
return;
}
// divide two values
answer = operand1 / operand2;
// display answer
strResult = operand1.ToString() + " divided by " +
operand2.ToString()
+
" equals " +answer.ToString();
MessageBox.Show ( strResult, "Output Result" );
}
}

③储存名档为test1.cs

④进入"C:\Windows\Microsoft.NET\Framework\v2.0.50727" 里头,执行csc.exe
如下:

csc.exe test1.cs [ 回车键 ]

这时它会显示以下讯息:

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

□测试数据Ⅰ□ 执行test1.exe......
Enter 1st whole number: 输入 0 [ 回车键 ]
Enter 2nd whole number: 输入 12 [ 回车键 ]
答案:0 divided by 12 equals 0

□测试数据Ⅱ□执行test1.exe......
Enter 1st whole number: 输入 a [ 回车键 ]
Enter 2nd whole number: 输入 7 [ 回车键 ]
答案:???

□测试数据Ⅲ□执行test1.exe......
Enter 1st whole number: 输入 ??? [ 回车键 ]
Enter 2nd whole number: 输入 ??? [ 回车键 ]
答案:??? divided by ??? equals Infinity

下课哆。。。。
Reply all
Reply to author
Forward
0 new messages