java第一章到第四章
class HelloWorld{
public static void main(String [] arguments) {
System.out.println("Hello World!");
System.out.println("Test Successly!");
}
}
简单的java测试
class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule");
System.out.println("The World");
}
}
class Loopy {
public static void main (String[] args) {
int x=1;
System.out.println("Before the Loop");
while(x < 4) {
System.out.println("In the Loop");
System.out.println("Value of x is " + x);
x = x + 1;
}
System.out.println("This is after the Loop");
}
}
简单if语句的测试
class IfTest {
public static void main (String [] args) {
int x = 3;
if(x==3) {
System.out.println("x must be 3");
}
System.out.println("This runs no matter what");
}
}
class IfTest2 {
public static void main (String[] args) {
int x = 2;
if (x == 3) {
System.out.println ("x must be 3");
} else {
System.out.println("x is NOT 3");
}
System.out.println("This runs no matter what");
}
}
class PhraseCMatic {
public static void main(String[] args) {
String[] wordListOne = {"24/7", "multiTier", "30,000", "B-to-B", "win-win", "front-end", "web-based", "pervasive", "smart", "six-sigma", "critical-path", "dynamic"};
String[] wordListTwo = {"empoweres", "sticky", "value-added", "oriented", "centric", "distributed", "clustered", "branded", "outside-the-box", "positioned", "networked", "focused", "leveraged", "aligned", "targeted", "shared", "cooperatived", "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);
}
}
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");
// }
if(beerNum <= 0)
{
System.out.println("No more bottles of beer on the wall" );
}
}
}
}
class Shuffel {
public static void main(String[] args) {
int x = 3;
while (x > 0) {
if(x == 1) {
System.out.print("d");
x = x - 1;
}
if(x == 2) {
System.out.print("b c");
}
if(x > 2) {
System.out.print("a");
}
x = x - 1;
System.out.print("-");
}
}
}
class Test {
public static void main(String[] args) {
int x = 0;
int y = 0;
while (x < 5) {
y = x - y;
y = y + x;
System.out.print(x + " " + y + " ");
x = x + 1;
}
}
}
class PoolPuzzleOne {
public static void main (String[] args) {
int x = 0;
while (x < 4) {
System.out.print("a");
if(x < 1) {
System.out.print(" ");
}
System.out.print("n");
if(x > 1) {
System.out.print("oyster");
x = x + 2;
}
if(x == 1) {
System.out.print("noys");
}
if(x < 1) {
System.out.print("oise");
}
System.out.println(" ");
x = x + 1;
}
}
}
class Dog {
int size;
String breed;
String name;
void bark() {
System.out.println("Ruff! Ruff!");
}
}
class DogTestDrive {
public static void main(String[] args) {
Dog d = new Dog();
d.size = 40;
d.bark();
}
}
class Movie {
String title;
String genre;
int rating;
void playIt() {
System.out.println("Playing the movie");
}
}
class MovieTestDrive {
public static void main (String[] aargs) {
Movie one = new Movie();
one.title = "Gone with the Stock";
one.rating = -2;
Movie two = new Movie();
two.genre = "Comedy";
two.rating = 5;
two.playIt();
Movie three = new Movie();
three.title = "Byte Club";
three.genre = "Tragic but ultimately uplifting";
three.rating = 127;
}
}
// 猜字游戏
class GuessGame {
Player p1;
Player p2;
Player p3; public void startGame() {
p1 = new Player();
p2 = new Player();
p3 = new Player(); int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0; boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false; int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinging of a number between o and 9..."); while(true) {
System.out.println("Number to guess is " + targetNumber); p1.guess();
p2.guess();
p3.guess(); guessp1 = p1.number;
System.out.println("Flayer one guessed " + guessp1); guessp2 = p2.number;
System.out.println("Flayer two guessed " + guessp2); guessp3 = p3.number;
System.out.println("Flayer three guessed " + guessp3); if(guessp1 == targetNumber) {
p1isRight = true;
} if(guessp2 == targetNumber) {
p2isRight = true;
} if(guessp3 == targetNumber) {
p3isRight = true;
} if(p1isRight || p2isRight || p3isRight) {
System.out.println("We have a winner!");
System.out.println("Player one got it right? " + p1isRight);
System.out.println("Player two got it right? " + p2isRight);
System.out.println("Player three got it right? " + p3isRight);
System.out.println("Game is over.");
break;
}
else {
System.out.println("Player will have to try again.");
}
}
}
} class Player {
int number = 0;
public void guess() {
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
} class GameLauncher {
public static void main(String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}
class DrumKit {
boolean topHat = true;
boolean snare = true;
void playTopHat() {
System.out.println("ding ding da-ding");
}
void playSnare() {
System.out.println("bang bang ba-bang");
}
}
class DrumKitTestDrive {
public static void main(String[] args) {
DrumKit d = new DrumKit();
d.playSnare();
d.snare = false;
d.playTopHat();
if(d.snare == true) {
d.playSnare();
}
}
}
class TapeDeck {
boolean canRecord = false;
void playTape() {
System.out.println("tape playing");
}
void recordTape() {
System.out.println("tape recording");
}
}
class TapeDeckTestDrive {
public static void main(String[] args) {
TapeDeck t = new TapeDeck();
t.canRecord = true;
t.playTape();
if(t.canRecord == true) {
t.recordTape();
}
}
}
class DVDPlayer {
boolean canRecord = false;
void recordDVD() {
System.out.println("DVD recording");
}
void playDVD() {
System.out.println("DVD playing");
}
}
class DVDPlayerTestDrive {
public static void main(String[] args) {
DVDPlayer d = new DVDPlayer();
d.canRecord = true;
d.playDVD();
if(d.canRecord == true) {
d.recordDVD();
}
}
}
class Echo {
int count = 0;
void hello() {
System.out.println("helloooo...");
}
}
class EchoTestDrive {
public static void main(String[] args) {
Echo e1 = new Echo();
Echo e2 = new Echo();
int x = 0;
while(x < 4) {
e1.hello();
e1.count = e1.count + 1;
if(x == 3) {
e2.count = e2.count + 1;
}
if(x > 0) {
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
class Dog {
String name;
public static void main (String[] args) {
Dog dog1 = new Dog();
dog1.bark();
dog1.name = "Bart";
Dog[] myDogs = new Dog[3];
myDogs[0] = new Dog();
myDogs[1] = new Dog();
myDogs[2] = dog1;
myDogs[0].name = "Fred";
myDogs[1].name = "Marge";
System.out.print("last dog's name is ");
System.out.println(myDogs[2].name);
int x = 0;
while(x < myDogs.length) {
myDogs[x].bark();
x = x + 1;
}
}
public void bark() {
System.out.println(name + " says Ruff!");
}
public void eat() {
}
public void chaseCat() {
}
}
class TestArrays {
public static void main(String[] args) {
int [] index = new int[4];
index[0] = 1;
index[1] = 3;
index[2] = 0;
index[3] = 2;
String[] islands = new String[4];
islands[0] = "Bermuda";
islands[1] = "Fiji";
islands[2] = "Azores";
islands[3] = "Cozumel";
int y = 0;
int ref;
while(y < 4) {
ref = index[y];
System.out.print("island = ");
System.out.println(islands[ref]);
y = y + 1;
}
}
}
class Books {
String title;
String author;
}
class BooksTestDrive {
public static void main (String[] args) {
Books [] myBooks = new Books[3];
int x = 0;
myBooks[0] = new Books();
myBooks[1] = new Books();
myBooks[2] = new Books();
myBooks[0].title = "The Grapes of java ";
myBooks[1].title = "The Java Gatsby ";
myBooks[2].title = "The Java Cookbook ";
myBooks[0].author = "bob";
myBooks[1].author = "sue";
myBooks[2].author = "lan";
while(x < 3) {
System.out.print(myBooks[x].title);
System.out.print(" by ");
System.out.println(myBooks[x].author);
x = x + 1;
}
}
}
class Hobbits {
String name;
public static void main(String[] args) {
Hobbits[] h = new Hobbits[3];
int z = -1;
while(z < 2) {
z = z + 1;
h[z] = new Hobbits();
h[z].name = "bilbo";
if(z == 1) {
h[z].name = "frodo";
}
if(z == 2) {
h[z].name = "sam";
}
System.out.print(h[z].name + " is a ");
System.out.println("good hobbit name");
}
}
}
class Triangle {
double area;
int height;
int length;
public static void main(String[] args) {
int x = 0;
Triangle [] ta = new Triangle[4];
while(x < 4) {
ta[x] = new Triangle();
ta[x].height = (x + 1) * 2;
ta[x].length = x + 4;
ta[x].setArea();
System.out.print("triangle " + x + ", area ");
System.out.println(" = " + ta[x].area);
x = x + 1;
}
int y = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
System.out.print("y = " + y);
System.out.println(", t5 area = " + t5.area);
}
void setArea() {
area = (height * length) / 2;
}
}
class Dog {
int size;
String name;
void bark() {
if(size > 60) {
System.out.println("Wooof! Wooof!");
} else if(size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
class DogTestDrive {
public static void main(String[] args) {
Dog one = new Dog();
one.size = 70;
Dog two = new Dog();
two.size = 8;
Dog three = new Dog();
three.size = 35;
one.bark();
two.bark();
three.bark();
}
}
class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;
}
void bark() {
if(size > 60) {
System.out.println("Wooof! Wooof!");
} else if(size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
class GoodDogTestDrive {
public static void main(String[] args) {
GoodDog one = new GoodDog();
one.setSize(70);
GoodDog two = new GoodDog();
two.setSize(8);
System.out.println("Dog one : " + one.getSize());
System.out.println("Dog two : " + two.getSize());
one.bark();
two.bark();
}
}
class PoorDog {
private int size;
private String name;
public int getSize() {
return size;
}
public String getName() {
return name;
}
}
class PoorDogTestDrive {
public static void main(String[] args) {
PoorDog one = new PoorDog();
System.out.println("Dog size is " + one.getSize());
System.out.println("Dog name is " + one.getName());
}
}
class Clock {
String time;
void setTime (String t) {
time = t;
}
String getTime() {
return time;
}
}
class ClockTestDrive {
public static void main(String[] args) {
Clock c = new Clock();
c.setTime("1245");
String tod = c.getTime();
System.out.println("time : " + tod);
}
}
class Puzzle4 {
public static void main(String[] args) {
Puzzle4b [] obs = new Puzzle4b[6];
int y = 1;
int x = 0;
int result = 0;
while (x < 6) {
obs[x] = new Puzzle4b();
obs[x].ivar = y;
y = y * 10;
x = x + 1;
}
x = 6;
while(x > 0) {
x = x - 1;
result = result + obs[x].doStuff(x);
}
System.out.println("result " + result);
}
}
class Puzzle4b {
int ivar;
public int doStuff(int factor) {
if(ivar > 100) {
return ivar * factor;
} else {
return ivar * (5 - factor);
}
}
}
java第一章到第四章的更多相关文章
- “全栈2019”Java多线程第三十四章:超时自动唤醒被等待的线程
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- 《码出高效:Java开发手册》第四章学习记录,内容想当的多,前后花了几天的时间才整理好。
<码出高效:Java开发手册>第四章学习记录,内容想当的多,前后花了几天的时间才整理好. https://naotu.baidu.com/file/e667435a4638cbaa15eb ...
- Kafka 权威指南阅读笔记(第三章,第四章)
Kafka 第三章,第四章阅读笔记 Kafka 发送消息有三种方式:不关心结果的,同步方式,异步方式. Kafka 的异常主要有两类:一种是可重试异常,一种是无需重试异常. 生产者的配置: acks ...
- JAVA: httpclient 详细说明——第四章;
httpclient 具体解释--第一章. httpclient 具体解释--第二章: httpclient 具体解释--第三章: httpclient 具体解释--第四章: httpclient 具 ...
- JavaScript DOM编程艺术-学习笔记(第三章、第四章)
第三章: 1.js的对象分为三种:①用户自定义对象 ② 内建对象(js提供的对象) ③宿主对象(js寄宿的环境-浏览器,提供的对象) 2.文档是由节点组成的集合,即dom树,html元素是根元素,是唯 ...
- (第三章,第四章)http报文内的http信息,返回结果的http状态码
第三章 http报文内的http信息 用于http协议交互的信息被称为http报文,包括请求报文和响应报文. 1.编码提升传输速率,在传输时编码能有效的处理大量的访问请求.但是编码的操作是计算机完成的 ...
- 算法导论 第三章 and 第四章
第三章 渐进的基本O().... 常用函数 % 和 // 转换 斯特林近似公式 斐波那契数 第四章 分治策略:分解(递归)--解决(递归触底)--合并 求解递归式的3种方法: 1:代入法(替代法): ...
- java多线程编程核心技术——第四章总结
第一节使用ReentrantLock类 1.1使用ReentrantLock实现同步:测试1 1.2使用ReentrantLock实现同步:测试2 1.3使用Condition实现等待/同步错误用法与 ...
- 《深入理解Java虚拟机》笔记--第四章、虚拟机性能监控与故障处理工具
主要学习并记录在命令行中操作服务器时使用的六大命令工具,可视化工具JConsole和VisualVM在开发过程中熟悉. 一.jps:虚拟机进程状况工具(JVM Process Status Tool) ...
随机推荐
- checkbox在jquery版本1.9 以上用attr不可重复操作的问题【附解决方案】
最近做个项目,需要重复多次更改checkbox的状态,使用jquery 1.10.2的最新版本时发现,对checkbox的选中状态无法多次选中.测试代码如下: <!DOCTYPE html PU ...
- 手机摇一摇效果-html5
1.手机摇一摇效果实现 2.播放声音 <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- js函数与变量同名
console.log(a); var a = 3; function a(){} 输出的结果是:[Function: a] 注意一下几点就能知道原因了! 1)函数声明会置顶2)变量声明也会置顶3)函 ...
- Java NIO之Selector
选择器是JavaNIO重磅推出的一个概念:在旧有的系统中为了跟踪多端口消息,需要为每一个端口配备一个线程做监听:但是有了selector就不需要了,一个Selector可以管理一众渠道(channel ...
- jedis源码阅读
package redis.clients.jedis; import java.util.ArrayList; import java.util.HashSet; import java.util. ...
- VIM一些常用命令,方法,配置
配置文件地址 github 工具只是为了更好的工具,选择一种,坚持使用学习记忆,熬过瓶颈期就可以了. 现在我基本大的项目是IDE+vim 插件,写小代码是VIM. 常用的操作,便捷的方法 1.如何选中 ...
- Aircrack-ng官方文档翻译[中英对照]---Aireplay-ng
Aircrack-ng官方文档翻译---Aireplay-ng[90%] Description[简介] Aireplay-ng is used to inject frames. Aireplay- ...
- bzoj 2806: [Ctsc2012]Cheat 后缀自动机DP
2806: [Ctsc2012]Cheat Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 583 Solved: 330[Submit][Statu ...
- 【原创】Matlab中plot函数全功能解析
[原创]Matlab中plot函数全功能解析 该帖由Matlab技术论(http://www.matlabsky.com)坛原创,更多精彩内容参见http://www.matlabsky.com 功能 ...
- poj1436 Horizontally Visible Segments
这是一个区间更新的题目,先将区间放大两倍,至于为什么要放大可以这样解释,按照从左到右有4个区间,y值是[1,5],[1,2],[3,4],[1,4]如果不放大的话,查询[1,4]区间和前面区间的”可见 ...