Head First Java_Study Notes_Chapter 1 Break the Surface
来源:互联网 发布:mysql 最近3天 编辑:程序博客网 时间:2024/06/06 17:28
Chapter 1 Break the Surface
1.Code structure in Java:
Put a class in a source file:
private class Dog{}
Put methods in a class:
private class Dog{void Bark (){}}
Put statements in a method:
private class Dog{void Bark (){statements1;statements2;}}
2.Anatomy of a Class
The Java Virtural Machine(JVM) starts with the main method:public static void main (String[] args) { // your code goes here}
Each statement must end in a semicolon.
x = x + 1;
3.Looping
Java has three standard looping constructs:while,do-while, andfor. The key to a loop is the conditional test, an expression that results in a boolean value—in other words, something that is eithertrue orfalse.
Practice:
Given the output:% java DooBee
DooBeeDooBeeDo
Fill in the missing code:
public class DooBee {public static void main (String[] args) {
int x = 1;
while (x < 3) {
System.out._____(“Doo”);
System.out._____(“Bee”);
x = x + 1;
}
if(x==__ ){
System.out.print(“Do”); }
}
}
My answer:
public class DooBee {public static void main (String[] args) {int x = 1;while (x < 3) {System.out.print("Doo"); System.out.print("Bee"); x = x + 1;}if(x==3){System.out.print("Do"); }} }
Coding a Serious Business Application: "99 bottles of beer"
Additional information (from Wikipedia): "99 Bottles of Beer" is an anonymous United States folk song dating to the mid-20th century. It is a traditional song in both the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. In particular the song is frequently sung by children on long bus trips, such as class field trips, or on Scout and/or Girl Guide outings. The song is derived from the English "Ten Green Bottles".
The song's lyrics are as follows:
99 bottles of beer on the wall, 99 bottles of beer.Take one down, pass it around, 98 bottles of beer on the wall...
Alternate line: If one of those bottles should happen to fall, 98 bottles of beer on the wall...
The same verse is repeated, each time with one bottle less. The song is completed when the singer or singers reach zero. Variations on the last verse following the last bottle going down include lines such as "No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall." and simply "If that one bottle should happen to fall, what a waste of alcohol!". It takes approximately 20 to 30 minutes to sing all 100 verses.
Sometimes, the word "beer" is replaced by the word "Coke" or "milk" when being sung by groups (such as religious groups) that consider the consumption of alcohol to be inappropriate.
Below is the original code on the book:
public class BeerSong {public static void main (String[] args){int beerNum = 99;String word = "bottles";while (beerNum > 0){if (beerNum == 1){word = "bottle";}System.out.println(beerNum + " " + word + " of beer on the wall");System.out.println(beerNum + " " + word + " of beer.");System.out.println("Take one down.");System.out.println("Pass it around.");beerNum = beerNum - 1;if (beerNum > 0){System.out.println(beerNum + " " + word + " of beer on the wall");}else{System.out.println("No more bottles of beer on the wall");}}}}
But this code is not 100% perfect. So I made a little change as below:
public class BeerSong {public static void main (String[] args){int beerNum = 99;String word = "bottles";while (beerNum > 0){System.out.println(beerNum + " " + word + " of beer on the wall");System.out.println(beerNum + " " + word + " of beer.");System.out.println("Take one down.");System.out.println("Pass it around.");beerNum = beerNum - 1;if (beerNum == 1){word = "bottle";}if (beerNum > 0){System.out.println(beerNum + " " + word + " of beer on the wall");}else{System.out.println("No more bottles of beer on the wall");}}}}
- Head First Java_Study Notes_Chapter 1 Break the Surface
- Head First Java_Study Notes_Chapter 6 Using the Java Library
- Head First Java_Study Notes_Chapter 2 A Trip to Objectville
- Head First Java_Study Notes_Chapter 3 Know Your Variables
- Head First Java_Study Notes_Chapter 4 How Objects Behave
- Head First Java_Study Notes_Chapter 5 Extra-Strength Methods
- Head.First - 读书笔记[1]
- {head first} --- networking 1
- Head First C# Save the Humans
- The language of the web --Head First into HTML and CSS Chapter 1
- Head First SQL 笔记1
- head first c 笔记<1>
- Head First JAVA 拾忆-1
- First Break All The Rules - The Measuring Stick
- Head First
- Head First
- ?head first ???? ????? ????
- Link in the HTML--Head first into HTML
- html5中关于canvas标签用法(绘图)
- (转)理解(Memory Reordering)从JVM并发看CPU内存指令重排序
- 事件处理
- 《轻松搞定网页设计》学习笔记系列之一
- python dict排序之一
- Head First Java_Study Notes_Chapter 1 Break the Surface
- 树莓派学习笔记——I2C设备载入和速率设置
- SDL游戏之路(八)--2D游戏添加3D元素
- python写的简答学生管理系统Demo(练习python语法)
- 五、java面向对象编程(二)——类变量、类方法
- JVM原理和优化
- 初识FreeRTOS
- 关于聚合的几个例子
- 32-求1+2!+3!+...+20!的和