No1_2. 流程控制_java学习笔记

 import java.util.Scanner;
import java.lang.Math; public class HelloForWhile { /**
* 文档注释,程序名称:HelloForWhile 流程控制 开发时间:2016-03-07 作者:嘿嘿
* */
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("test");
int y = 50;
int x = 800; // 复合语句又称块语句,用{}括起来
{
boolean z = y > x;
System.out.println("【1】y>x成立吗?" + z);
} // if语句,仅有一条语句可以省略大括号;
if (x > y)
System.out.println("【2】x大于y成立");
// if...else...
if (x > y) {
System.out.println("【3】x大于y!");
} else {
System.out.println("【4】x小于等于y!");
}
// if...else if...多分支语句
int math = 79;
if (math > 90) {
System.out.println("数学成绩“优”!");
} else if (math > 80) {
System.out.println("数学成绩“良”!");
} else if (math > 60) {
System.out.println("数学成绩“合格”!");
} else {
System.out.println("数学成绩“不及格”!");
} // 多分支语句,switch语句表达式中的值必须是整型或者字符型。
// 1.1,“ok”都是非法的
int season = 3;
System.out.println("输入个数字代表季节吧:" + season);
switch (season) {
case 1:
System.out.println("Spring");
break;
case 2:
System.out.println("Summer");
break;
case 3:
System.out.println("August");
break;
case 4:
System.out.println("Winter");
break;
default:
System.out.print("season的值不是1-4的整数,不代表任何季节~");
} // 示例一:验证登录信息的合法性
Scanner scan = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scan.nextLine();
System.out.println("请输入密码");
String userpw = scan.nextLine();
if (!username.equals("sunshine")) { // 为什么此处不可以用:(username="sunshine")
System.out.println("用户名不正确!");
} else if (!userpw.equals("123456")) {
System.out.println("密码不正确!");
} else {
System.out.println("恭喜您,登录成功!");
} // 示例二:为新员工分配部门
System.out.println("员工姓名?");
String staffname = scan.nextLine();
System.out.println("员工编程语言?");
String language = scan.nextLine(); switch (language.hashCode()) { // 不清楚hashCode的用法
case 3254818:
case 2301506:
case 2269730:
System.out.println("员工" + staffname + "被分配到JAVA程序开发部门。");
break;
case 3140:
case 2112:
System.out.println("员工" + staffname + "被分配到C#程序开发部门。");
break;
default:
System.out.println("本公司不需要" + language + "语言的程序人员。" + (char) 97); }
int hash1 = "JAVA".hashCode();
System.out.println(hash1); /******************************************
* 循环语句(此处注释应该用于注释文档的)
****************************************/
// while循环语句
int tenadd = 1, addten = 0;
while (tenadd <= 10) {
addten = addten + tenadd;
tenadd = tenadd + 1;
}
System.out.println("10以内的整数累加是" + addten); // do...while循环语句
int doa = 100;
while (doa == 60) {
System.out.println("ok! doa=100");
doa--;
}
int dob = 50;
do {
System.out.println("ok! dob=50,不判断条件,先执行该语句!!");
dob--;
} while (dob == 100); // for循环语句
// for(表达式 1;表达式2;表达式3;){语句块}
// 输出菱形!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! System.out.println("要输出菱行了,请输入单数整数行数");
int n = scan.nextInt();
for (int stari = -(n - 1) / 2; stari <= (n - 1) / 2; stari++) {
for (int stara = 1; stara <= Math.abs(stari); stara++) {
System.out.print(" ");
}
for (int starj = 0; starj < -Math.abs(stari) * 2 + n; starj++) {
System.out.print("*");
}
System.out.println();
} // foreach语句~~在遍历数组时,为程序员提供方便(java5之后加的)
// for(元素变量x:遍历对象obj){ 引用了x的JAVA语名;} int arr[] = { 5, 2, 0 };
System.out.println("一维数组中的元素分别为:");
for (int fx : arr) {
System.out.print(fx + "\t");
}
System.out.print("\n"); // 示例三:使用while循环遍历数组
String[] season1 = new String[] { "春", "夏", "秋", "冬" };
System.out.println("一年四季有啥:");
for (String fy : season1) {
System.out.print(fy + " ");
}
System.out.println("\n" + "一年四季有啥:");
int seasonCount = 0;
while (seasonCount < season1.length) {
System.out.print(season1[seasonCount++] + " ");
}
System.out.println(); // 示例四:使用for循环输出九九乘法表 for (int ninex = 1; ninex <= 9; ninex++) {
for (int niney = 1; niney <= ninex; niney++) {
System.out.print(niney + "*" + ninex + "=" + ninex * niney
+ " ");
}
System.out.println();
}
/******************************************
* 跳转语句(break\return\continue)
****************************************/
// break语句,强制退出循环。使用break语句计算1~100之间所有连续整数的和,但累加超1000跳出循环
int sumHundred = 0;
for (int sumx = 1; sumx <= 100; sumx++) {
if (sumHundred > 1000) {
System.out.println(sumx + "以内的整数累加是" + sumHundred);
break;
}
sumHundred = sumHundred + sumx;
}
// continue语句,只能用在for\while\do...while循环中,跳过其后面的语句,进行下次循环
// 用while循环输出10以内的所有奇数~~ int odd = 0;
System.out.print("10以内所有奇数是:");
while (odd <= 10) {
if (odd % 2 == 0) {
odd = odd + 1;
continue;
} else { System.out.print(odd + " ");
odd = odd + 1;
}
}
System.out.println(); // return语句可以从一个方法返回,并把控制权交给调用它的语句;
// return [表达式];
/****************** 还没有实践这个语句。。 ***********************/
// 示例五:终止循环
// 中断单重循环的例子
System.out.println("中断单重循环的例子,看到鬼就跳出来。");
String[] array = new String[] { "钢铁侠", "金刚狼", "贞子", "变形金刚", "蝙蝠侠",
"贞子", "绿巨人" };
for (String gost : array) {
if (gost == "贞子") {
System.out.println("遇到鬼了");
break;
} else {
System.out.println(gost);
}
}
// 中断双重循环的例子
int[][] myScores = new int[][] { { 78, 98, 67 }, { 80, 89, 80 },
{ 78, 93, 99 } };
System.out.println("寶寶這次考試成績:\n 数学\t 语文 \t英语");
No1: for (int[] is : myScores) {
for (int q : is) {
System.out.println(q + "\t");
if (q < 70) {
System.out.println("\n 等等" + q + "分的是什么?这个为什么不及格?");
break No1;
}
}
}
// 示例六:循环体的过滤器
System.out.println("最近一直在追美剧,最近还看了贞子,查一下看了几遍?");
int girlgost = 0;
for (String gost1 : array) {
if (gost1 == "贞子") {
girlgost = girlgost + 1;
continue;
}
System.out.println("最近看的美剧有" + gost1);
}
System.out.println("您一共看了" + girlgost + "次贞子"); // 作业一:使用for循环输出空心的菱形
// 作业二:使用for循环输出杨辉三解。。。。
// 作业三:使用while循环语句计算1+1/2!+1/3!+。。。+1/20!之和 }// main }// HelloForWhile

输出结果:

test
【1】y>x成立吗?false
【2】x大于y成立
【3】x大于y!
数学成绩“合格”!
输入个数字代表季节吧:3
August
请输入用户名:
sunshine
请输入密码
123456
恭喜您,登录成功!
员工姓名?
liuyq
员工编程语言?
java
员工liuyq被分配到JAVA程序开发部门。
2269730
10以内的整数累加是55
ok! dob=50,不判断条件,先执行该语句!!
要输出菱行了,请输入单数整数行数
45
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*******************************
*********************************
***********************************
*************************************
***************************************
*****************************************
*******************************************
*********************************************
*******************************************
*****************************************
***************************************
*************************************
***********************************
*********************************
*******************************
*****************************
***************************
*************************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
一维数组中的元素分别为:
5 2 0
一年四季有啥:
春 夏 秋 冬
一年四季有啥:
春 夏 秋 冬
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
46以内的整数累加是1035
10以内所有奇数是:1 3 5 7 9
中断单重循环的例子,看到鬼就跳出来。
钢铁侠
金刚狼
遇到鬼了
寶寶這次考試成績:
数学 语文 英语
78
98
67 等等67分的是什么?这个为什么不及格?
最近一直在追美剧,最近还看了贞子,查一下看了几遍?
最近看的美剧有钢铁侠
最近看的美剧有金刚狼
最近看的美剧有变形金刚
最近看的美剧有蝙蝠侠
最近看的美剧有绿巨人
您一共看了2次贞子
上一篇:unsigned long类型转换为CString出现的问题


下一篇:Python笔记·第十一章—— 函数 (二) 装饰器