- 11
- 0
- 约1.52万字
- 约 12页
- 2018-03-08 发布于河南
- 举报
thinking injava习题答案2
这是第3章的答案:
Chapter 3
Exercise 1
//: c03:E01_Precedence.java
//+M java E01_Precedence
/****************** Exercise 1 ******************
* There are two expressions in the section
* labeled precedence early in this chapter.
* Put these expressions into a program and
* demonstrate that they produce different
* results.
***********************************************/
public class E01_Precedence {
static int a,
x = 40,
y = 60,
z = 10;
public static void main(String[] args) {
a = x + y - 2/2 + z;
System.out.println(a);
a = x + (y - 2)/(2 + z);
System.out.println(a);
}
} ///:~
Results are 109 44. The difference is because the default order of evaluation is changed by the use of the parentheses.
Exercise 2
//: c03:E02_Ternary.java
//+M java E02_Ternary
/****************** Exercise 2 ******************
* Put the methods ternary() and alternative()
* into a working program.
***********************************************/
public class E02_Ternary {
static int ternary(int i) {
return i 10 ? i * 100 : i * 10;
}
static int alternative(int i) {
if (i 10)
return i * 100;
else
return i * 10;
}
public static void main(String[] args) {
System.out.println(ternary(9));
System.out.println(ternary(11));
System.out.println(alternative(9));
System.out.println(alternative(11));
}
} ///:~
Exercise 3
//: c03:E03_IfElse3.java
//+M java E03_IfElse3
/****************** Exercise 3 ******************
* From the sections labeled if-else and
* return, modify the two test() methods so
* that testval is tested to see if it is within
* the range between (and including) the
* arguments begin and end. (This is a change to
* the exercise in the printed version of the
* book, which was in error).
***********************************************/
public class E03_IfElse3 {
static boolean
test(int testval, int begin, int end) {
boolean result = false;
if(testval = begin testval = end)
您可能关注的文档
最近下载
- 平凡的世界读书分享.ppt VIP
- 药物化学习题仉文升主编).pdf VIP
- 2025年山东济南高三一模数学试卷及答案.pdf VIP
- 2025年辽宁省委党校在职研究生招生考试(政治理论)历年参考题库含答案详解.docx VIP
- 保险双录标准话术.pptx
- 2025年辽宁省委党校在职研究生招生考试(法学)历年参考题库含答案详解.docx VIP
- 江苏省南通市2025年中考语文试卷(附答案解析).doc VIP
- 《人工智能在机械设计制造及其自动化中的实践》8900字.docx VIP
- 普传(POWTRAN)变频器PI500变频器使用说明书.pdf
- 最新部编版一年级数学下册教案(全册)表格式二次备课.doc VIP
原创力文档

文档评论(0)