JAVA基础语法03
笔记
循环结构
-
do-while 至少循环一次,并且while后面分号。
-
Math.random();{【0-1)的数值} 注意:random是浮点型数据。
(int)(Math.random*10);[0,10] 需加上括号
-
for( ; ; ) 无穷循环
-
例:1!+2!+3!。。。。
int n=1,sum=0; for (int i=1;i<=4;i++) { // 每次回去都需重新制1 n=1; for (int j=1;j<=i;j++) { n*=j; } sum+=n; } System.out.println("1!+2!+3!+4!="+sum);
-
break 跳出套用循环 ,只能在循环体内和switch语句体内使用break语句。
for() { for() { break; //跳出内循环 } }
-
continue 结束当前循环,再次循环下一次。
// 计算1+3+5+7.。。 int sum=0; for (int i=1;i<8;i++) { if (i%2==0) continue; sum+=i; } System.out.println("1+3+5+7="+sum);
-
程序调试 debug
看清程序的每一步的效果,使用debug查看结果是否与预期结果一致。 -
调试步骤
-
设置断点
双击该语句前面 出现圆断点
-
调试断点
debug as 或者 run->debug
作用域 功能 快捷键
全局 单步返回 F7
全局 单步跳过 F6
全局 单步跳入 F5
全局 单步跳入选择 Ctrl+F5
全局 调试上次启动 F11
全局 继续 F8
全局 使用过滤器单步执行 Shift+F5
全局 添加/去除断点 Ctrl+Shift+B
全局 显示 Ctrl+D
全局 运行上次启动 Ctrl+F11
全局 运行至行 Ctrl+R
全局 执行 Ctrl+U
-
-
练习题
- 三角形
int n = 6; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } // 最后总的三角形 11 ,n-i是空白区的个数 for (int t = 1; t <= 11 - (n - i) * 2; t++) { System.out.print("*"); } System.out.println(); }
-
Scanner 接收类型
- nextInt
- nextLong
- nextFloat
- nextDouble
- next // 字符串
- char
//charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。 String str=scan.next(); char c=str.charAt(0);