正文之前
这两节太弱了。基本大一的C++程序设计课就足够对付理解了。所以还是水一波吧,实在没办法,整个教程还是全部抄全吧。免得到时候新人小白入门的时候还要去看别的了~
(郑重声明:本文非原创,只是笔记,取自菜鸟编程!好东西,继续打CALL)
正文
1、 Java 循环结构 - for, while 及 do...while
顺序结构的程序语句只能被执行一次。如果您想要同样的操作执行多次,,就需要使用循环结构。Java中有三种主要的循环结构:
- while 循环
- do…while 循环
- for 循环
public class Loop{
public static void main(String args[])
{
int x = 10;
int count = 0;
while( x < 20 )
{
System.out.print("value of x : " + x );
x++;
count++;
if(count>=4)
{
System.out.print("\n");
count=0;
}
else System.out.print(" ");
}
//=====================//
System.out.print("\n");
System.out.print("\n");
x = 10;
count=0;
do
{
System.out.print("value of x : " + x );
x++;
count++;
if(count>=4)
{
System.out.print("\n");
count=0;
}
else System.out.print(" ");
}while( x < 20 );
//=====================//
System.out.print("\n");
System.out.print("\n");
count=0;
for(x = 10; x < 20; x = x+1)
{
System.out.print("value of x : " + x );
count++;
if(count>=4)
{
System.out.print("\n");
count=0;
}
else System.out.print(" ");
}
}
}
value of x : 10 value of x : 11 value of x : 12 value of x : 13
value of x : 14 value of x : 15 value of x : 16 value of x : 17
value of x : 18 value of x : 19
value of x : 10 value of x : 11 value of x : 12 value of x : 13
value of x : 14 value of x : 15 value of x : 16 value of x : 17
value of x : 18 value of x : 19
value of x : 10 value of x : 11 value of x : 12 value of x : 13
value of x : 14 value of x : 15 value of x : 16 value of x : 17
value of x : 18 value of x : 19 [Finished in 1.3s]
2、 Java 增强 for 循环
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
3、 break 关键字
break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 跳出最里层的循环,并且继续执行该循环下面的语句。
4、 continue 关键字
continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
public class Loop{
public static void main(String args[])
{
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
System.out.print("==========\n");
for(int x : numbers ) {
// x 等于 30 时跳出循环
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
5、 Java 分支结构
顺序结构只能顺序执行,不能进行判断和选择,因此需要分支结构。
if 语句
switch 语句
6、 switch 语句
switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串类型了,同时 case 标签必须为字符串常量或字面量。
switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
switch 语句可以包含一个 default 分支,该分支必须是 switch 语句的最后一个分支。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。
public class Branch {
public static void main(String args[]){
int x = 30;
//=====================// if else语句结构
System.out.print("\n");
if( x < 20 ){
System.out.print("这是 if 语句");
}else{
System.out.print("这是 else 语句");
}
//=====================// if else if语句结构
System.out.print("\n");
System.out.print("\n");
if( x == 10 ){
System.out.print("Value of X is 10");
}else if( x == 20 ){
System.out.print("Value of X is 20");
}else if( x == 30 ){
System.out.print("Value of X is 30");
}else{
System.out.print("这是 else 语句");
}
//=====================//嵌套if结构
System.out.print("\n");
System.out.print("\n");
int y = 10;
if( x == 30 ){
if( y == 10 ){
System.out.print("X = 30 and Y = 10");
}
}
//=====================// switch结构
System.out.print("\n");
System.out.print("\n");
char grade = 'C';
switch(grade)
{
case 'A' :
System.out.println("优秀");
break;
case 'B' :
case 'C' :
System.out.println("良好");
break;
case 'D' :
System.out.println("及格");
case 'F' :
System.out.println("你需要再努力努力");
break;
default :
System.out.println("未知等级");
}
System.out.println("你的等级是 " + grade);
}
}
这是 else 语句
Value of X is 30
X = 30 and Y = 10
良好
你的等级是 C
[Finished in 1.4s]
正文之后
不得不说,这些都是很基础的了。所以编程语言学多了,语法基本都趋近于相近了。所以更多的还是要理解思想,更深层次就是要理解计算机的体系内容了~ 惨啊!!老师都没给我指路。