Java-基本概念
- Java的工作方式
- Java的程序结构
- Java的类
- 语句、循环、条件分支
-
- 语句
- 循环
- 条件分支
- 编写程序
-
- 专家术语学习机
Java的工作方式
此例为交互式派对邀请系统。
Java的程序结构
Java的类
每个Java程序至少会有一个类以及一个main()。每个应用程序只能有一个main()函数。
语句、循环、条件分支
语句
声明、设定、调用方法等普通语句。
int x = 3;
String name= "zhangsan" ;
x = x * 7;
System.out.print("x = " + x);
double d = Math.random();
//注释行
循环
反复做某件事
while (x>12) {
x = x - 1;
}for (int x = 0;x < 10;x++) {
System.out.print("x = " + x);
}
条件分支
在适当的条件下做某件事
if(x == 0) {
System.out.print("x 是 0");
} else {
System.out.print("x 不是 0");
}
注
- 语句是以分号结束
- 程序块以{ }画出范围
- 以两条斜线开始的行是注释
- 空格符通常无关紧要
- 用名称与类型(type)来声明变量
- 类型与方法都必须定义在花括号{ }中
- 等号是赋值运算符
- 两个等号用来当等式等号运算符
- 只要条件测试结果为真,while循环就会一直执行块内的程序
- System.out.print与 System.out.println 差别在println 会再最后面插入换行
编写程序
专家术语学习机
public class PhraseOMatic {
public static void main(String[] args) {
// 专家术语String[] wordListOne = {
"24/7", "multiTier", "30,000 foot", "B-to-B", "win-win", "frontend","web-based", "pervasive", "smart", "sixsigma", "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", "tippingpoint", "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);}
}