Java的工作方式
源代码——编译器——输出字节码——Java虚拟机读取并执行字节码
你要做的事
1.编辑源代码文件,如Party.java。
2.执行javac程序编译源文件,若无错误则生成Party.class文件,此文件是由字节码组成的。
javac Party.java
3.启动java虚拟机(JVM)运行class文件。JVM会将字节码转换成平台能够理解的形式运行。
java Party
Java的程序结构
类存与源文件里面,方法存于类中,语句存于方法中。
注意:源文件名称必须与带有main方法的类的名称相同,否则编译不通过。
执行java程序也就是执行类中的main()。
System.out.print与System.out.println的区别
println会在最后插入换行。
BeerSong程序
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");
}
}
}
}
专家术语学习机
public class PhraseOMatic {
public static void main(String[] args) {
String[] wordListOne = {"24/7", "multi-Tier", "30,000 foot", "B-to-B",
"win-win", "front-end", "web-based", "pervasive", "smart", "six-sigma",
"critical-path", "dynamic"};
String[] wordListTwo = {"empowered", "sticky", "value-added", "oriented", "centric",
"distributed", "clustered", "branded", "outside-the-box", "positioned", "networked",
"focused", "leveraged", "aligned", "targeted", "shared", "cooperative", "accelerated"};
String[] wordListThree = {"process", "tipping-point", "solution", "architecture", "core competency",
"strategy", "mindshare", "portal", "space", "vision", "paradigm", "mission"};
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length;
int rand1 = (int) (Math.random() * oneLength);
int rand2 = (int) (Math.random() * twoLength);
int rand3 = (int) (Math.random() * threeLength);
String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
System.out.println("What we need is a " + phrase);
}
}