循环结构
######纯粹记录#############
1switch选择结构
/**
* 测试switch选择结构语句
* @author Jsh
*
*/
public class TestSwitch {
public static void main(String[] args) {
char a = 'a';
int r = (int)(26*Math.random());
char a1 = (char)('a'+r);
System.out.println("a1="+a1);
switch(a1) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(a1+"是原音");
break;
case 'y':
case 'w':
System.out.println(a1+"是半原音");
break;
default:
System.out.println(a1+"是辅音");
}
}
}
2if语句
(1)if
/**
* 测试if单选择结构
* @author Jsh
*
*/
public class TestIf {
public static void main(String[] args) {
double a = Math.random(); //产生【0,1)之间的double类型的一个数,不包含1
System.out.println(a);
System.out.println((int)(6*Math.random())); //产生【0,5)之间随机整数
//System.out.println((int)(6*Math.random()+1)); //产生【1,6)之间随机整数
int i = (int)(6*Math.random()+1);
int j = (int)(6*Math.random()+1);
int k = (int)(6*Math.random()+1); 产生【1,6)之间随机整数
int count = i + j+ k;
System.out.println(count);
if(count>15){
System.out.println("今天手气好");
}
if(count>=10&&count<=15){
System.out.println("今天手气一般");
}
if(count<10){
System.out.println("今天手气不好");
}
}
}
(2)if else
/**
* 测试if else双选择语句结构
* @author Jsh
*
*/
public class TestIfElse {
public static void main(String[] args) {
double r = 4 *(Math.random());//随机产生一个【0,4)的半径
//final double PI = 3.14; //可以这么定义PI为一个符号常量
double area = Math.PI*Math.pow(r, 2); //也可以直接用Math类里的PI
double circle = 2*Math.PI*r;
System.out.println("半径="+r);
System.out.println("面积="+area);
System.out.println("周长circle="+circle);
if(area>circle) {
System.out.println("面积大于周长");
}
else {
System.out.println("面积小于周长");
}
}
}
(3)if else多选择语句
/**
* 测试IfElse IfElse多选择语句结构
* @author JFJ
*
*/
public class TestIfElseIfElse {
public static void main(String[] args) {
int age = (int)(100*Math.random()); //生成【0,100)得数
System.out.println("年龄"+age+",属于");
if(age<15) {
System.out.println("小孩");
}
else if (age<25) {
System.out.println("青年");
}
else if (age<45) {
System.out.println("中年");
}
else if (age<65) {
System.out.println("中老青年");
}
else if (age<85) {
System.out.println("老年");
}
else {
System.out.println("老寿星,古来稀");
}
}
}
3,while循环
/**
*测试while循环
* @author Jsh
*
*/
public class TestWhile {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while(i<=100) {
sum = sum + i;
i++;
}
System.out.println("sum="+sum);//1到100的累加和
}
}
4.for循环
/**
* 测试for循环
* @author Jsh
*
*/
public class TestFor {
public static void main(String[] args) {
int sum = 0;
for(int i = 0;i<=100;i++) {
sum +=i;
}
System.out.println("sum="+sum);//1到100的累加和
for(;;) {
System.out.println("死循环");//一直输出“死循环”
}
}
}
5嵌套循环
/**
* 测试嵌套循环
* @author Jsh
*
*/
public class TestQianTao {
public static void main(String[] args) {
for(int i = 1; i <= 5;i++)
{
for(int j = 1;j <= 5;j++)
{
System.out.print(i+"\t");
}
System.out.println();
}
System.out.println("****************************");
//输出99乘法表
for (int a = 1;a <= 9;a++)
{
for(int b = 1;b <= a;b++)
{
System.out.print(a+"*"+b+"="+a*b+"\t");
}
System.out.println();
}
System.out.println("****************************");
//while计算100以内奇数与偶数的和
int c = 1;
int sum1 = 0;
int sum2 = 0;
while(c<=100)
{
sum1 += c;
sum2 += (c+1);
c += 2;
}
System.out.println("奇数和="+sum1+"\n"+"偶数和="+sum2);
System.out.println("****************************");
//输出1~1000以内能被5整除的数,每行输出五个
int y = 0;
for (int x = 1;x<=1000;x++)
{
if (x%5==0)
{
System.out.print(x+"\t");
y++;
}
if (y%5==0)
{
System.out.println();
}
/*
if(x%25==0)
{
System.out.println();
}*/
}
}
}
6。break语句
/**
* 测试break语句
* @author Jsh
*
*/
public class TestBreak {
public static void main(String[] args) {
int total = 0;
System.out.println("开始");
while(true)
{
total++;
int i = (int)Math.round(100*Math.random());
System.out.println(i);
if(i==88)
{
break;//退出循环
}
}
System.out.println("结束,用了"+total+"次");
}
}
7.continue语句
/**
* 测试continue语句
* @author Jsh
*
*/
public class TestContinue {
public static void main(String[] args) {
//把100~150之间不能被3整除得数输出,每行五个
int j = 0;//计数器
for(int i = 100;i<=150;i++)
{
if(i%3==0)
{
continue;
}
System.out.print(i+"\t");
j++;
if(j%5==0)
{
System.out.println();
}
}
}
}