分析:该题型属于行拼接题 按行拼接 用System.out.print(),结束一行的时候使用 System.out.println()换行 打印星星图案 都是 拼接行的问题从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印输出“baz”。
解法一:
public class Test6 {
public static void main(String[] args) {
//1:获取1到150的数
//分析 步骤一 因为(num % 3 == 0 && num % 7 == 0 && num % 5 == 0)的限制比较严格
// 属于(num % 3 == 0)的子集因此在用if结构的时候范围小的要放到上面
//因为对于 if -- else if -- else if --else 结构 如果 遇到一个true后面的代码
// 将不会在被执行直接结束,因此应将 范围小的放到执行语句的上面
for (int num = 1; num < 151; num++) {
System.out.print(num);
if (num % 3 == 0 && num % 7 == 0 && num % 5 == 0){
System.out.print(" foo biz baz");
}else if (num % 3 == 0 && num % 5 == 0){
System.out.print(" foo biz");
}else if (num % 3 == 0 && num % 7 == 0){
System.out.print(" foo baz");
}else if (num % 3 == 0){
System.out.print(" foo ");
}else if (num % 5 == 0){
System.out.print(" biz ");
}else if (num % 7 == 0){
System.out.print(" baz ");
}
System.out.println();
}
解法二:
package com.java;
public class Test6 {
public static void main(String[] args) {
//解法二
for (int i = 1; i < 151; i++) {
System.out.print(i);
if (i % 3 == 0){
System.out.print(" foo ");
}
if (i % 5 == 0){
System.out.print(" biz ");
}
if (i % 7 == 0){
System.out.print(" baz ");
}
System.out.println();
}
}
}
方法三:方法二的代码优化
package com.java;
public class Test6 {
public static void main(String[] args) {
//抽取代码 当前代码 有冗余 System.out.println();
// 代码抽取 如果记录的是 总集合 则放在大循环外边
// 如果记录的是单词则放到循环内层
for (int i = 1; i < 151; i++) {
String str = "";
if (i % 3 == 0){
str = "tfoo ";
}
if (i % 5 == 0){
str += "tbiz ";
}
if (i % 7 == 0 ){
str += "tbaz ";
}
System.out.println(i + str);
}
}
}
for循环测试的重要的几个知识点:
①初始化
②循环条件
③循环体
④迭代条件
1> 循环内声明的变量只能在循环内部使用作用域
2> 初始化条件可以提到循环的外部
3> 缺少迭代条件 => 死循环 无限循环
4> 缺少循环条件 => 死循环 无限循环
5> 缺少初始化条件 迭代条件 循环条件
package com.java;
public class Test7 {
public static void main(String[] args) {
//都缺少
for (;;) {
System.out.println("hello world");
}
}
}
注意:在做任何一道问题的时候,都不要急着写代码,代码没有思路值钱,先把思路旅顺了
问题的解决思路就是思路的分析步骤,然后用代码实现。
生成一个区间的随机数公式:[m,n] Math.random()*(n - m + 1) + n 注意生成的随机数区间前后都是闭区间
小知识点:
flag 标识符的应用 : 当我们需要在整个问题解决过程最后获得某个值,我们需要使用到标识符,过程的细节都不在展示。
do {
} while()
①初始化
②循环条件
③循环体
④迭代条件
注意 即使 初始条件不满足,也会执行一次,开发中用的比较少。
//此处的num可以随意赋值 因为在do中至少执行一次,将会被覆盖
int num = -1;
do {
num = scanner.nextInt()
}
for : 当循环次数固定时
while :当循环次数不固定时候使用
do while 当初始化条件 不满足循环条件 也要执行一次
流程控制语句重点
break 结束循环 只能在switch 或 循环内使用
continue 结束本次循环 继续下一次循环 只能在循环内使用
return 结束方法 只要在方法内使用都行
注意 : 流程控制语句下 不能存在其他的内容
嵌套循环的通俗化解释
内层循环作为外层循环的循环体
外层控制行,内层控制列
嵌套循环内:
1: break 结束 break所在层循环
2: 如果存在标签 结束标签所在层循环
package com.java;
public class Test7 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j == 6) {
break;
}
System.out.print(j + " ");
}
System.out.println();
}
}
}
package com.java;
public class Test7 {
public static void main(String[] args) {
a:
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j == 6) {
break a;
}
System.out.print(j + " ");
}
System.out.println();
}
}
}
continue:
嵌套循环内:
1: continue 结束continue所在层循环 继续下一次循环
2: 如果存在标签continue 结束本次循环 继续标签所在层循环
package com.java;
public class Test7 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j == 6) {
continue;
}
System.out.print(j + " ");
}
System.out.println();
}
}
}
package com.java;
public class Test7 {
public static void main(String[] args) {
a: for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 10; j++) {
if (j == 6) {
continue a;
}
System.out.print(j + " ");
}
System.out.println();
}
}
}
求100以内质数的经典算法:(特别巧妙的使用了continue)
方法一:
package com.java;
public class Test7 {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean flag = true;
//for (int j = 2; j < i ; j++) {
//for (int j = 2; j < i / 2; j++) {
for (int j = 2; j < Math.sqrt(i); j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.println(i + "是质数");
}
}
}
}
方法二:
package com.java;
public class Test7 {
public static void main(String[] args) {
a :for (int i = 2; i <= 100; i++) {
// boolean flag = true;
//for (int j = 2; j < i ; j++) {
//for (int j = 2; j < i / 2; j++) {
for (int j = 2; j < Math.sqrt(i); j++) {
if (i % j == 0) {
// flag = false;
continue a;
}
}
System.out.println(i + "是质数");
}
}
}
二进制转换:
package com.java;
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
//键盘
Scanner scanner = new Scanner(System.in);
System.out.println("输入");
int num = scanner.nextInt();
int shang ;
int yu;
String str = "";
while (num != 0){
shang = num / 2;
yu = num % 2;
str = yu + str;
num = shang;
}
System.out.println("当前数的二进制为:"+str);
}
}
求一年的第几天
public class TestExer11{
public static void main(String[] args){
//1、从键盘分别输入年、月、日
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("年:");
int year = input.nextInt();
System.out.print("月:");
int month = input.nextInt();
System.out.print("日:");
int day = input.nextInt();
//判断这一天是当年的第几天==>从1月1日开始,累加到xx月xx日这一天
//(1)[1,month-1]个月满月天数
//(2)第month个月的day天
//(3)单独考虑2月份是否是29天(依据是看year是否是闰年)
//2、声明一个变量days,用来存储总天数,直接初始化为day,这样就不用再加day天了
int days = day;
//3、累加[1,month-1]个月满月天数
switch(month){
case 12:
//累加的1-11月
days += 30;//这个30是代表11月份的满月天数
//这里没有break,继续往下走
case 11:
//累加的1-10月
days += 31;//这个31是代表10月的满月天数
//这里没有break,继续往下走
case 10:
days += 30;//9月
case 9:
days += 31;//8月
case 8:
days += 31;//7月
case 7:
days += 30;//6月
case 6:
days += 31;//5月
case 5:
days += 30;//4月
case 4:
days += 31;//3月
case 3:
days += 28;//2月
//4、在这里考虑是否可能是29天
if(year%4==0 && year%100!=0 || year%400==0){
days++;//多加1天
}
case 2:
days += 31;//1月
}
//5、输出结果
System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + days + "天");
}
}



