首页 > 编程 > Java > 正文

Java 基础程序设计

2019-11-08 19:47:18
字体:
来源:转载
供稿:网友

前言:此为个人笔记梳理,如若侵权,请联系作者删除

1. java数据类型的划分

graph TD A(数据类型) --> B(基本数据类型) A(数据类型) --> C(引用数据类型) B --> D(数值型) D --> G(整数类型 byte,short,int,long) D --> H(浮点类型 float,double) B --> E(字符型 char) B --> F(布尔型 boolean) C --> I(类 class) C --> J(接口 interface) C --> K(数组)分为两大类型: 基本数据类型:类似于普通的值引用数据类型:传递的是内存的地址

注:浮点类型实际上就是表示小数


2. Java基本数据类型

数据过长public class DateDemo01 { public static void main(String args[]) { int num = 999999999999; }}打印结果:DateDemo01.java:3: 错误: 过大的整数: 999999999999 int num = 999999999999; ^1 个错误

注:一定要在整数允许的范围之中保存数字

数据溢出

当一个数字已经是最大或者最小值的时候,再进行增加或者减少操作,就会产生数据的溢出

例如:求出整数的最大值public class DateDemo02 { public static void main(String args[]) { int max = Integer.MAX_VALUE; System.out.PRintln("整数的最大值:" + max); }}**整数的最大值:2147483647**对其进行加法操作:max + 1 变为最小值 = 整数的最大值:-2147483648max + 2 最小值加 1 = 整数的最大值:-2147483647*注:整数位最小值到最大值循环***解决数据溢出,扩大数据范围的方式**
示例一:
public class DateDemo03 { public static void main(String args[]) { int max = Integer.MAX_VALUE; max = (long)max + 2; System.out.println("整数的最大值:" + max); }}打印结果:DateDemo03.java:4: 错误: 不兼容的类型: 从long转换到int可能会有损失 max = (long)max + 2; ^1 个错误
示例二:
public class DateDemo03 { public static void main(String args[]) { int max = Integer.MAX_VALUE; System.out.println("整数的最大值:" + ((long)max + 2)); }}**整数的最大值:2147483649**字符类型(也算整型的一种) ASCII码表:字符对应一个整型 public class DateDemo04 { public static void main(String args[]) { char ch1 = 'a'; //字符是使用"'"括起来的数据 char ch2 = 97; //通过数字定义字符变量 System.out.println("ch1 = " + ch1); System.out.println("ch2 = " + ch2); }}ch1 = ach2 = a常用的转义字符(/)
转义字符 含义
/f 换页
/b 倒退一格
/r 归位
/t 制表符Tab
// 反斜线
/’ 单引号
/” 双引号
/n 换行

public class DateDemo05 { public static void main(String args[]) { char ch1 = '/"'; //表示一个" char ch2 = '//'; //表示一个/ System.out.println("ch1 = " + ch1); System.out.println("ch2 = " + ch2); System.out.println("/"Hello World!/""); }}浮点型与双精度类型

Java中存在默认类型:整型为int,小数为double

定义两个小数相乘public class DateDemo06 { public static void main(String args[]) { float num = 3.0f; //定义一个浮点型变量 System.out.println("两个小数相乘:" + num*num); }}*注:实际开发中,使用float就够了*布尔类型 boolean flag = true; // 声明布尔变量flag,并赋值为true public class DateDemo07 { public static void main(String args[]) { boolean flag = true; //定义布尔型变量 System.out.println("flag = " + flag); //打印输出 }}基本数据类型的默认值
基本数据类型 默认值
byte (byte)0
short (short)0
int 0
long 0L
float 0.0f
double 0.0d
char /u0000(空,”)
boolean false

注:开发中指定具体内容,不要使用其默认值

数据类型的转换 自动转换: 1.类型兼容 2.转换后比转换前类型数据大 public class DateDemo08 { public static void main(String args[]) { int x = 30; //定义整型变量 float y = 22.19f; //定义浮点型变量 System.out.println("x/y = " + (x/y)); //floar型 System.out.println("10/3.5 = " + (10/3.5)); //double型 System.out.println("10/3 = " + (10/3)); //整型 }}打印结果:x/y = 1.351960310/3.5 = 2.85714285714285710/3 = 3String类型 提示:任何类型的数据都向String转型 public class DateDemo09 { public static void main(String args[]) { String str = "dingli"; int x = 30; str = str + x; System.out.println("str = " + str);}打印结果:str = dingli30public class DateDemo10 { public static void main(String args[]) { int i = 1; int j = 2; System.out.println("1+2= " + 1 + 2); System.out.println("1+2= " + (1 + 2)); }}打印结果:1+2= 121+2= 3强制转换 (欲转换的数据类型) 变量名称 public class DateDemo11 { public static void main(String args[]) { float f = 30.3f; int x = (int)f; double y = 10d; System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("10/3 = " + ((float)10/3)); }}打印结果:x = 30y = 10.010/3 = 3.3333333

3. 运算符、表达式与语句

表达式 表达式由操作数与运算符所组成,操作数可以是常量、变量,也可以是方法;运算符就是数学中的运算符号,”+,-,*,/,%”等 public class OperateDemo01 { public static void main(String args[]) { int num = 22; System.out.println("第一次输出:num = " + num); num = num - 3; System.out.println("第二次输出:num = " + num); }}一元运算符
符号 含义
+ 正号
- 负号
! NOT,否
public class OperateDemo02 { public static void main(String args[]) { boolean b = false; int x = 10; int y = -30; System.out.println("b = " + b + ", !b = " + !b); System.out.println("x = " + x + ", -x = " + -x); System.out.println("y = " + y + ", +y = " + +y); }}打印结果:b = false, !b = truex = 10, -x = -10y = -30, +y = -30算术运算符
符号 含义
+
-
*
/
% 取模(取余数)
public class OperateDemo03 { public static void main(String args[]) { int i = 10; int j = 3; System.out.println(i + " + " + j + " = " + (i+j)); System.out.println(i + " - " + j + " = " + (i-j)); System.out.println(i + " * " + j + " = " + (i*j)); System.out.println(i + " / " + j + " = " + (i/j)); System.out.println(i + " % " + j + " = " + (i%j)); }}打印结果:10 + 3 = 1310 - 3 = 710 * 3 = 3010 / 3 = 310 % 3 = 1关系运算符
符号 含义
> 大于
< 小于
= 大于等于
<= 小于等于
== 等于
!= 不等于
public class OperateDemo04 { public static void main(String args[]) { System.out.println("3 > 1 = " + (3>1)); System.out.println("3 < 1 = " + (3<1)); System.out.println("3 >= 1 = " + (3>=1)); System.out.println("3 <= 1 = " + (3<=1)); System.out.println("3 == 1 = " + (3==1)); System.out.println("3 != 1 = " + (3!=1)); }}打印结果:3 > 1 = true3 < 1 = false3 >= 1 = true3 <= 1 = false3 == 1 = false3 != 1 = truepublic class OperateDemo05 { public static void main(String args[]) { if(5>2) { System.out.println("条件成立:5大于2!"); } if(true) { System.out.println("直接写的true!"); } if((3+6)==(3-6)) { System.out.println("这是不可能成立的!"); } }}打印结果:条件成立:5大于2!直接写的true!递增与递减运算符
符号 含义
++ 自增,变量值加1
自减,变量值减1
public class OperateDemo06 { public static void main(String args[]) { int a = 3, b = 3; int x = 6, y = 6; System.out.println("a = " + a); System.out.println("/t a++ = " + (a++) + ", a = " + a); System.out.println("b = " + b); System.out.println("/t ++b = " + (++b) + ", b = " + b); System.out.println("x = " + x); System.out.println("/t x-- = " + (x--) + ", x = " + x); System.out.println("y = " + y); System.out.println("/t --y = " + (--y) + ", y = " + y); }}打印结果:a = 3 a++ = 3, a = 4b = 3 ++b = 4, b = 4x = 6 x-- = 6, x = 5y = 6 --y = 5, y = 5

同理:–一样

逻辑运算符
符号 含义
& ANG,与
| OR,或
&& 短路与
|| 短路或

不管是短路还是非短路,其基本的操作结果是一样的

public class OperateDemo07 { public static void main(String args[]) { boolean a = true; boolean b = false; System.out.println("a&b = " + (a&b)); System.out.println("a|b = " + (a|b)); System.out.println("a&&b = " + (a&&b)); System.out.println("a||b = " + (a||b)); }}打印结果:a&b = falsea|b = truea&&b = falsea||b = truepublic class OperateDemo08 { public static void main(String args[]) { int score = 50; if((score<0)||(score>100)) { System.out.println("输入有误!"); } if((score<60)&&(score>49)) { System.out.println("准备补考吧!"); } }}打印结果:准备补考吧!

注:&&:第一个为false,后面的不判断;||:第一个为true,后面的不判断(节约计算机资源)

对比示例一:
public class OperateDemo09 { public static void main(String args[]) { int x = 10/0; System.out.println("x = " + x); }}打印结果:Exception in thread "main" java.lang.ArithmeticException: / by zero at OperateDemo09.main(OperateDemo09.java:3)public class OperateDemo10 { public static void main(String args[]) { if(10!=10&10/0==0) { System.out.println("条件满足!"); } }}打印结果:Exception in thread "main" java.lang.ArithmeticException: / by zero at OperateDemo10.main(OperateDemo10.java:3)
规避了错误:
public class OperateDemo11 { public static void main(String args[]) { if(10!=10&&10/0==0) { System.out.println("条件满足!"); } }}打印结果:无结果,不报错!
对比示例二:
public class OperateDemo12 { public static void main(String args[]) { if(10==10|10/0==0) { System.out.println("条件满足!"); } }}打印结果:Exception in thread "main" java.lang.ArithmeticException: / by zero at OperateDemo12.main(OperateDemo12.java:3)
规避了错误:
public class OperateDemo13 { public static void main(String args[]) { if(10==10||10/0==0) { System.out.println("条件满足!"); } }}打印结果:条件满足!括号运算符 ():提高括号内表达式的优先级 public class OperateDemo14 { public static void main(String args[]) { int result1 = 3 + 5 + 4*6 - 7; int result2 = (3 + 5 + 4)*(6 - 7); System.out.println("不加括号结果是:" + result1); System.out.println("加括号结果是:" + result2); }}打印结果:不加括号结果是:25加括号结果是:-12位运算符

进行位操作,将十进制转换为二进制数

符号 含义
& 按位与
| 按位或
^ 异或(相同为0,不同为1)
~ 取反
<< 左移位
> 右移位
> 无符号右移位
public class OperateDemo15 { public static void main(String args[]) { int x = 3; //3 = 0000 0000 0000 0000 0000 0000 0000 0011 int y = 6; //6 = 0000 0000 0000 0000 0000 0000 0000 0110 System.out.println("x&y = " + (x&y)); //都1为1,结果是:0000 0000 0000 0000 0000 0000 0000 0010 = 2 System.out.println("x|y = " + (x|y)); //有1为1,结果是:0000 0000 0000 0000 0000 0000 0000 0111 = 7 System.out.println("x^y = " + (x^y));//相同为0,不同为1:0000 0000 0000 0000 0000 0000 0000 0101 = 5 }}打印结果:x&y = 2x|y = 7x^y = 5

正数的原码、反码和补码都一致,为其本身

计算机中都是以补码的形式存储数据

public class OperateDemo16 { public static void main(String args[]) { int x = -3; //原码:1000 0000 0000 0000 0000 0000 0000 0011 //反码:1111 1111 1111 1111 1111 1111 1111 1100 //补码:1111 1111 1111 1111 1111 1111 1111 1101 System.out.println("~ x = " + (~x));//取反:0000 0000 0000 0000 0000 0000 0000 0010 = 2 }}打印结果:~ x = 2左移和右移(左移乘2,右移除2) 负数使用补码进行位运算 左移操作:二进制码整体左移指定位数,左移之后的空位使用0来填充右移操作:二进制码整体右移,右移之后空出来的位置以符号位来填充(正数填充‘0’,负数填充‘1’)public class OperateDemo17 { public static void main(String args[]) { int x = 3; //原码:0000 0000 0000 0000 0000 0000 0000 0011 //补码:0000 0000 0000 0000 0000 0000 0000 1100 = 12 System.out.println(x + "左移2位之后的内容:" + (x<<2)); }}打印结果:3左移2位之后的内容:12public class OperateDemo18 { public static void main(String args[]) { int x = 3; //补码:0000 0000 0000 0000 0000 0000 0000 0011 int y = -3; //补码:1111 1111 1111 1111 1111 1111 1111 1101 System.out.println(x + "右移2位置后的内容:" + (x>>2)); //结果:0000 0000 0000 0000 0000 0000 0000 0000 = 0 System.out.println(y + "右移2位置后的内容:" + (y>>2)); //移位:1111 1111 1111 1111 1111 1111 1111 1111 } //取反:1000 0000 0000 0000 0000 0000 0000 0000 } //加一:1000 0000 0000 0000 0000 0000 0000 0001 = -1(原码)打印结果:3右移2位置后的内容:0-3右移2位置后的内容:-1*注:关于负数:原码除第一位符号位外,其余取反为反码,加1为补码;补码除符号位外,其余位全部取反加1为原码*无符号右移位

无符号右移以“0”填空位,负数经操作后变为正数

重点移位操作,易搞混淆

public class OperateDemo19 { public static void main(String args[]) { int x = 3; //补码:0000 0000 0000 0000 0000 0000 0000 0011 int y = -3; //补码:1111 1111 1111 1111 1111 1111 1111 1101 System.out.println(x + "无符号右移2位置后的内容:" + (x>>>2)); //结果:0000 0000 0000 0000 0000 0000 0000 0000 = 0 System.out.println(y + "无符号右移2位置后的内容:" + (y>>>2)); //移位:0011 1111 1111 1111 1111 1111 1111 1111(为正数) = 2^30 - 1 = 1073741823 } } 打印结果:3无符号右移2位置后的内容:0-3无符号右移2位置后的内容:1073741823

运算符的优先级(很无聊,不记!)

简单的运算符及表达式的使用

+=:表示两个内容相加之后再赋值给指定的变量-=:表示两个内容相减之后再赋值给指定的变量*=:表示两个内容相乘之后再赋值给指定的变量/=:表示两个内容相整除之后再赋值给指定的变量%=:表示两个内容取余之后再赋值给指定的变量public class SimpleExpressDemo01 { public static void main(String args[]) { int a = 5, b = 8; System.out.println("改变之前的数是:a = " + a + ", b = " + b); a += b; System.out.println("改变之后的数是:a = " + a + ", b = " + b); } }打印结果:改变之前的数是:a = 5, b = 8改变之后的数是:a = 13, b = 8public class SimpleExpressDemo02 { public static void main(String args[]) { int a = 10, b = 6; System.out.println("改变之前的数是:a = " + a + ", b = " + b); a -= b++; System.out.println("改变之后的数是:a = " + a + ", b = " + b); } } 打印结果:改变之前的数是:a = 10, b = 6改变之后的数是:a = 4, b = 7

注:在实际开发中,不提倡使用。即使它使计算机运算效率提高,现在的计算机不差那点运算能力

表达式类型的转换 1.占用字节较少——>占用字节较多2.字符类型会转换成int类型3.int类型——>float类型4.布尔类型不能转换成其他类型5.表达式中若存在double类型,则其他类型则转换成double类型public class TypeChangeDemo { public static void main(String args[]) { char ch = 'a'; short a = -2; int b = 3; float f = 5.3f; double d = 6.28; System.out.print("(ch/a)-(d/f)-(a+b) = "); System.out.println((ch/a)-(d/f)-(a+b)); }}打印结果:(ch/a)-(d/f)-(a+b) = -50.18490561773532

变形一:

public class TypeChangeDemo { public static void main(String args[]) { char ch = 'a'; short a = -2; int b = 3; float f = 5.3f; double d = 6.28; System.out.print("(ch/a)-(d/f)-(a+b) = " + (ch/a)-(d/f)-(a+b)); }}打印结果:TypeChangeDemo.java:8: 错误: 二元运算符 '-' 的操作数类型错误 System.out.print("(ch/a)-(d/f)-(a+b) = " + (ch/a)-(d/f)-(a+b)); ^ 第一个类型: String 第二个类型: double1 个错误

变形二:

public class TypeChangeDemo { public static void main(String args[]) { char ch = 'a'; short a = -2; int b = 3; float f = 5.3f; double d = 6.28; System.out.println("(ch/a)-(d/f)-(a+b) = " + ((ch/a)-(d/f)-(a+b))); }}打印结果:(ch/a)-(d/f)-(a+b) = -50.18490561773532

4. 判断与循环语句

graph TD A(程序结构) --> B(顺序结构) A(程序结构) --> C(选择结构) A(程序结构) --> D(循环结构)public class IfDemo { public static void main(String args[]) { int x = 3; int y = 10; System.out.println("===比较开始==="); if(x>y) { System.out.println("x比y大!"); } if(x<y) { System.out.println("x比y小!"); } System.out.println("===比较完成==="); }}public class IfElseDemo { public static void main(String args[]) { int x = 3; if(x%2==1) { System.out.println("x是奇数!"); }else { System.out.println("x是偶数!"); } }}三目运算符 变量 = 条件判断?表达式一:表达式二; public class MaxDemo { public static void main(String args[]) { int max = 0; int x = 3; int y = 10; max = x>y?x:y; System.out.println("最大值为:" + max); }}打印结果:最大值为:10

以下的if,else if……else之间有相应的联系

public class MoreIfElseDemo { public static void main(String args[]) { int x = 3; if(x==1) { System.out.println("x的值是1!"); }else if(x==2) { System.out.println("x的值是2!"); }else if(x==3) { System.out.println("x的值是3!"); }else { System.out.println("x的值不是1,2,3中的一个!"); } }}打印结果:x的值是3!*注:多条件判断,Java用Switch语句替换;if else语句占资源,使计算机设备等运行不平缓*

switch里面放数字、字符,以后加枚举

public class SwitchDemo01 { public static void main(String args[]) { int x = 3; int y = 6; char oper = '+'; switch(oper) { case '+': { System.out.println("x + y = " + (x+y)); break; } case '-': { System.out.println("x - y = " + (x-y)); break; } case '*': { System.out.println("x * y = " + (x*y)); break; } default : { System.out.println("未知的操作!"); break; } } }}打印结果:x + y = 9**变形:**public class SwitchDemo02 { public static void main(String args[]) { int x = 3; int y = 6; char oper = '+'; switch (oper) { case '+': System.out.println("x + y = " + (x+y)); break; case '-': System.out.println("x - y = " + (x-y)); break; case '*': System.out.println("x * y = " + (x*y)); break; default: System.out.println("未知的操作!"); } }}打印结果:x + y = 9*注:break表示退出整个switch()语句;谨防break穿越*循环语句(while……,do……while,for……语句)public class WhileDemo { public static void main(String args[]) { int x = 1; int sum = 0; while(x<=10) { sum +=x; x++; } System.out.println("1-->10累加结果为:" + sum); }}打印结果:1-->10累加结果为:55public class DoWhileDemo { public static void main(String args[]) { int x = 1; int sum = 0; do{ sum +=x; x++; } while(x<=10); System.out.println("1-->10累加结果为:" + sum); }}

至少执行一次循环体!

打印结果:1-->10累加结果为:55public class ForDemo { public static void main(String args[]) { int sum = 0; for(int x=1;x<=10;x++) { sum += x; } System.out.println("1-->10累加结果为:" + sum); }}打印结果:1-->10累加结果为:55

嵌套的使用:九九乘法表

示例:

public class ForNestedDemo { public static void main(String args[]) { for(int i=1;i<=9;i++) { for(int j=1;j<=9;j++) { System.out.println(i + "*" + j + "=" + (i*j)); } } }}打印结果:1*1=11*2=21*3=31*4=41*5=51*6=61*7=71*8=81*9=92*1=22*2=42*3=62*4=82*5=102*6=122*7=142*8=162*9=183*1=33*2=63*3=93*4=123*5=153*6=183*7=213*8=243*9=274*1=44*2=84*3=124*4=164*5=204*6=244*7=284*8=324*9=365*1=55*2=105*3=155*4=205*5=255*6=305*7=355*8=405*9=456*1=66*2=126*3=186*4=246*5=306*6=366*7=426*8=486*9=547*1=77*2=147*3=217*4=287*5=357*6=427*7=497*8=567*9=638*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=648*9=729*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81

变一:

public class ForNestedDemo { public static void main(String args[]) { for(int i=1;i<=9;i++) { for(int j=1;j<=9;j++) { System.out.print(i + "*" + j + "=" + (i*j) + " "); } System.out.println(); } }}打印结果:1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=92*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=183*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=274*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=365*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=456*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=547*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=638*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=729*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

变二:

public class ForNestedDemo { public static void main(String args[]) { for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) { System.out.print(i + "*" + j + "=" + (i*j) + " "); } System.out.println(); } }}打印结果:1*1=12*1=2 2*2=43*1=3 3*2=6 3*3=94*1=4 4*2=8 4*3=12 4*4=165*1=5 5*2=10 5*3=15 5*4=20 5*5=256*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=367*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=498*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=649*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

变三:

public class ForNestedDemo { public static void main(String args[]) { for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) { System.out.print(j + "*" + i + "=" + (j*i) + " "); } System.out.println(); } System.out.println("===我是分界线!==="); for(int i=9;i>=1;i--) { for(int j=1;j<=i;j++) { System.out.print(j + "*" + i + "=" + (j*i) + " "); } System.out.println(); } }}打印结果:1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*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===我是分界线!===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=811*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*5=5 2*5=10 3*5=15 4*5=20 5*5=251*4=4 2*4=8 3*4=12 4*4=161*3=3 2*3=6 3*3=91*2=2 2*2=41*1=1

变四:(变一中改)

public class ForNestedDemo { public static void main(String args[]) { int sum; for(int i=1;i<=9;i++) { for(int j=1;j<=9;j++) { sum = i*j; System.out.print(i + "*" + j + "=" + (sum)); Judge(sum); //判断是个位数还是百位数,并给出几个空格 } System.out.println(); } } public static void Judge(int number) { if(number/10==0) { //此时sum为个位数,'/0'为一空字符 System.out.print("/0/0"); }else { //此时sum为十位数 System.out.print("/0"); } }}打印结果:1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=92*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=183*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=274*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=365*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=456*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=547*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=638*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=729*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

变五:(变四中改)

public class ForNestedDemo { public static void main(String args[]) { int sum; for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) { sum = i*j; System.out.print(j + "*" + i + "=" + (sum)); Judge(sum); //判断是个位数还是百位数,并给出几个空格 } System.out.println(); } } public static void Judge(int number) { if(number/10==0) { //此时sum为个位数 System.out.print("/0/0"); }else { //此时sum为十位数 System.out.print("/0"); } }}打印结果:1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*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

中断语句

break语句:退出当前循环,中断整个循环 public class BreakDemo { public static void main(String args[]) { for(int i=0;i<10;i++) { System.out.println("先前的i = " + i); if(i==3) { break; } System.out.println("后来的i = " + i); } }}打印结果:先前的i = 0后来的i = 0先前的i = 1后来的i = 1先前的i = 2后来的i = 2先前的i = 3

continue语句:继续执行循环主体的部分语句

使用continue就是中断一次循环的执行,中断一次循环

public class ContinueDemo { public static void main(String args[]) { for(int i=0;i<10;i++) { if(i==3) { //退出一次循环操作 continue; } System.out.println("i = " + i); } }}打印结果:i = 0i = 1i = 2i = 4i = 5i = 6i = 7i = 8i = 9
参考补充资料:

http://blog.csdn.net/qinglu/article/details/13766351

http://www.cnblogs.com/gw811/archive/2012/10/13/2722752.html

http://blog.csdn.net/zhangwei1120112119/article/details/18823101

http://baike.baidu.com/link?url=040xvX-fkYGWu0uOjYePa5imkClCzum4YDm-hO9WNts9uiAQkoC5EFOgTL0zGXEiaeehiCxRxr6x_B_bazXsPi3eAL_8HsjMChemoLNjtXlNhxAvFHW5w5b6ivbt5duFXWh1NAwtY8Wtom8lH4RXujCBf9RlkMEJOSL8dAJOv-W


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表