没有合适的资源?快使用搜索试试~ 我知道了~
《C#入门经典》(第四版)课后答案
4星 · 超过85%的资源 需积分: 9 22 下载量 3 浏览量
2010-03-28
12:35:56
上传
评论 1
收藏 640KB DOC 举报
温馨提示
试读
64页
《C#入门经典》(第四版)课后答案。英文版的。也可以直接从网站P2P.WROX.COM下载的。
资源推荐
资源详情
资源评论
Exercise Solutions
Chapter 1: Introducing C#
No exercises.
Chapter 2: Writing a C# Program
No exercises.
Chapter 3: Variables and Expressions
Exercise 1
Q. In the following code, how would you refer to the name great from code
in the namespace fabulous?
namespace fabulous
{
// code in fabulous namespace
}
namespace super
{
namespace smashing
{
// great name defined
}
}
A.
super.smashing.great
Exercise 2
Q. Which of the following is not a legal variable name?
a. myVariableIsGood
b. 99Flake
c. _floor
d time2GetJiggyWidIt
e wrox.com
A.
b. Because it starts with a number, and,
e Because it contains a full stop.
Exercise 3
Q. Is the string "supercalifragilisticexpialidocious" too big to fit in a
string variable? Why?
A. No, there is no theoretical limit to the size of a string that may be
contained in a string variable.
Exercise 4
Q. By considering operator precedence, list the steps involved in the
computation of the following expression:
resultVar += var1 * var2 + var3 % var4 / var5;
A. The * and / operators have the highest precedence here, followed by +, %,
and finally +=. The precedence in the exercise can be illustrated using
parentheses as follows:
resultVar += (((var1 * var2) + var3) % (var4 /
var5));
Exercise 5
Q. Write a console application that obtains four int values from the user and
displays their product.
A.
static void Main(string[] args)
{
int firstNumber, secondNumber, thirdNumber, fourthNumber;
Console.WriteLine("Give me a number:");
firstNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Give me another number:");
secondNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Give me another number:");
thirdNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Give me another number:");
fourthNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The product of {0}, {1}, {2}, and {3}
is {4}.",
firstNumber, secondNumber, thirdNumber,
fourthNumber,
firstNumber * secondNumber * thirdNumber *
fourthNumber);
}
Note that Convert.ToInt32() is used here, which isn’t covered in the
chapter.
Chapter 4: Flow Control
Exercise 1
Q. If you have two integers stored in variables var1 and var2, what Boolean
test can you perform to see if one or the other (but not both) is greater than
10?
A. (var1 > 10) ^ (var2 > 10)
Exercise 2
Q. Write an application that includes the logic from Exercise 1, obtains two
numbers from the user, and displays them, but rejects any input where both
numbers are greater than 10 and asks for two new numbers.
A.
static void Main(string[] args)
{
bool numbersOK = false;
double var1, var2;
var1 = 0;
var2 = 0;
while (!numbersOK)
{
Console.WriteLine("Give me a
number:");
var1 =
Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Give me another
number:");
var2 =
Convert.ToDouble(Console.ReadLine());
if ((var1 > 10) ^ (var2 > 10))
{
numbersOK = true;
}
else
{
if ((var1 <= 10) && (var2 <= 10))
{
numbersOK = true;
}
else
{
Console.WriteLine("Only one
number may be greater than 10.");
}
}
}
Console.WriteLine("You entered {0} and
{1}.", var1, var2);
}
This can be performed better using different logic, for example:
static void Main(string[] args)
{
bool numbersOK = false;
double var1, var2;
var1 = 0;
var2 = 0;
while (!numbersOK)
{
Console.WriteLine("Give me a
number:");
var1 =
Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Give me another
number:");
var2 =
Convert.ToDouble(Console.ReadLine());
if ((var1 > 10) && (var2 > 10))
{
Console.WriteLine("Only one number
may be greater than 10.");
}
else
{
numbersOK = true;
}
}
Console.WriteLine("You entered {0} and
{1}.", var1, var2);
}
Exercise 3
Q. What is wrong with the following code?
int i;
for (i = 1; i <= 10; i++)
{
if ((i % 2) = 0)
continue;
Console.WriteLine(i);
}
A. The code should read:
int i;
for (i = 1; i <= 10; i++)
{
if ((i % 2) == 0)
continue;
Console.WriteLine(i);
}
Using the = assignment operator instead of the Boolean == operator is a
common mistake.
Exercise 4
Q. Modify the Mandelbrot set application to reQuest image limits from the
user and display the chosen section of the image. The current code outputs
as many characters as will fit on a single line of a console application;
剩余63页未读,继续阅读
资源评论
- wjzt04102018-06-18非常适合于新手
helllo000
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功