java学习笔记2-thinking in java

//控制语句


//1.if-else
static int test(int testval) {
int result = 0;
if(testval > target)
result = -1;
else if(testval < target)
result = +1;
else 
result = 0; //match
return result;
}


//2.反复
//2.1 while
//:WhileTest.java
// Demonstrates the while loop(循环)


public class WhileTest {
public static void main(String[] args) {
double r = 0;
while(r < 0.99d) {
r = Math.random();
Systemout.println(r);
}
}
}


//2.2 do-while
/*
 do
 语句
 while(布尔表达式)
*/


//2.3 for
//:ListCharacters.java
// Demonstrates "for" loop by listing
//all the ASCII characters


public class ListCharacters {
public static void main(String[] args) {
for(char c = 0;c<128;c++) //只有for循环才具备在控制表达式里定义变量的能力
if(c!=26) //ANSI Clear screen
Systemout.println("value:" + (int)c + "character:" + c);
}
}


//2.3.1 逗号运算符(唯一用到逗号运算符的地方就是for循环)
//:CommaOperator.java


public class CommaOperator {
public static void main(String[] args) {
for(int i=1,j=i+100;i<5;i++,j=i*2) {
Systemout.println("i=" + i + "j=" + j)
}
}
}


//3.中断和继续
//beak:强行退出循环,不执行循环中剩余的语句
//continue:停止执行当前的反复,然后退回循环起始,开始新的反复
//:BreakAndContinue.java
//Demonstrates break and continue keywords


public class BreakAndContinue {
public static void main(String[] args) {
for(int i = 0;i < 100; i++) {
if(i == 74) break; //Out of for loop
if(i %9 != 0) continue; //Next iteration
Systemout.println(i);
}
int i = 0;
// An "infinite loop":
while(true) {
i++;
int j=i*27;
if(j == 1269) break; 
if(i%10 != 0) continue; //top of loop
Systemout.println(i);
}
}
}


//3.1 标签
//:LabeledFor.java
// Java's "labeled for loop"


public class LabeledFor {
public static void main(String[] args) {
int i = 0;
outer: //Can't have statements here
for(;true;) { //infinite loop
inner:
for(;i<10;i++) {
prt("i=" + i);
if(i==2) {
prt("continue");
continue;
}
if(i==3) {
prt("break");
i++; //Otherwise i never gets incremented(增加)
break;
}
if(i==7) {
prt("continue outer");
i++;
continue outer;
}
if(i==8) {
prt("break outer");
break outer;
}
for(int k=0;k<5;k++) {
if(k==3) {
prt("continue inner");
continue inner;
}
}
}
}
}
static void prt(String s) {
Systemout.println(s);
}
}


//3.开关
//选择因子必须是int或char那样的整数值,假如将字串或者浮点数作为选择因子使用,那么他们在switch语句是不会工作的,对于非整数类型必须使用一系列if语句
//:Vowel(元音)sAndConsonant(辅音)s.java
// Demonstrates the switch statement


public class VowelsAndConsonants {
public static void main(String[] args) {
for(int i=0;i<100;i++) {
char c = (cahr)(Math.random()*26+'a'); //造型成char时,小数部分直接"砍掉"
Systemout.print(c+":");
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
Systemout.println("vowel");
break;
case 'y':
case 'w':
Systemout.println("sometimes a vowel");
break;
default:
Systemout.println("consonant");
}
}
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值