public class DoWhile {
float flag = 1;
void print() {
flag = flag + 0.1f;
System.out.println(flag);
}
public static void main(String[] args)
{
DoWhile obj = new DoWhile();
float value = obj.flag;
do
{
System.out.println(value);
value +=0.1f;
}
while (value<=2.0f); // 布尔表达式
{
System.out.println(false);
obj.print();
}
}
}
Foreach:
//★ Java SE5引入一种新的更加简洁的语法
//来访问数组和容器,即Foreach语法
//◆声明数组的语法:
// (1) 数组元素类型[ ] 数组引用;
// (2) 数组元素类型 数组引用[ ];//数组名也是引用
//创建数组空间的语法:
//数组引用 = new 数组元素类型[数组元素个数];
//float[ ] f = new float[5]; //数组也是对象
import java.util.*;
public class FOREACH {
public static void main(String[] args) {
Random rand = new Random(47);
float[] f = new float[5];
// 传统:利用循环控制变量控制数组下标
for (int i = 0; i < f.length; i++) {
f[i] = rand.nextFloat();//[0,1)
// 调用随机数对象中的方法以产生随机数
}
// Foreach语法:将每一个f的元素赋值给x
for (float x : f) {
System.out.println(x);
}
for (char c : "An African".toCharArray()) {
System.out.println(c + " ");
}
//break用于强行退出当前循环。而continue则停止执行当前的循环,然后从循环起始处,开始下一次的循环。
for(int i = 0; i < 100; i++)
{
if(i == 74) break;
if(i % 9 != 0) continue;
System.out.println(i);
}
}
}
If-Else:
public class IfElse {
static int result = 0;
static void test(int t1, int t2) {
if(t1 > t2)
result = -1;
else if(t1 < t2)
result = +1;
else
result = 0;
}
public static void main(String[ ] args) {
test(1, 5);
System.out.println(result);
}
}
public class Swith {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
char c = (char) (Math.random() * 26 + 'a');
System.out.print(c + ": ");
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("vowel");
break;
case 'y':
case 'w':
System.out.println("Sometimes a vowel");
break;
default:
System.out.println("consonant");
}
}
}
}
While:
public class While {
static boolean condition( ){
boolean result = Math.random( )< 0.99;
System.out.println(result);
return result;
}
public static void main(String[ ] args) {
while(condition( )) ;
}
}
迭代(for)
public class 迭代 {
public static void main(String[] args) {
for (char c = 0; c < 128; c++) {
if (Character.isLowerCase(c)) {
System.out.println("value: " + (int) c);
System.out.println("Character: " + c);
}
}
}
}