多层循环的使用和实例
使用循环打印平行四边形
public class Demo0 {
public static void main(String[] args) {
//矩形和平行四边形的:倒空白三角形加一个正方形
for (int i = 0; i < 5; i++) {
// System.out.println("*****");
//改造方案
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
各类直角三角形和等腰三角形的打印
public class Demo1 {
public static void main(String[] args) {
//直角在左下的三角形
System.out.println("直角在左下的三角形:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i ; j++) {
System.out.print("*");
}
System.out.println();
}
//直角在左上的三角形
System.out.println("直角在左上的三角形:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i ; j++) {
System.out.print("*");
}
System.out.println();
}
//直角在右下的三角形
System.out.println("直角在右下的三角形:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i ; j++) {
System.out.print("* ");
}
System.out.println();
}
//直角在右上的三角形
System.out.println("直角在右上的三角形:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < 5 - i; j++) {
System.out.print("* ");
}
System.out.println();
}
//等腰三角形
System.out.println("等腰三角形:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
菱形的打印
public class Demo3 {
public static void main(String[] args) {
//1.正等腰三角形
for (int j = 0; j < 5; j++) {
for (int i = 0; i < 4 - j; i++) {
System.out.print(" ");
}
for (int i = 0; i < 2 * j + 1; i++) {
System.out.print("*");
}
System.out.println();
}
//2.倒等腰三角形
for (int j = 0; j < 4; j++) {
for (int i = 0; i <= j; i++) {
System.out.print(" ");
}
for (int i = 0; i < 7 - (2 * j); i++) {
System.out.print("*");
}
System.out.println();
}
}
}
九九乘法表的实现
public class Demo4 {
public static void main(String[] args) {
//1、先打出一个9*9的直角三角形
//2、把三角形的每一个*换成对应的值
for (int i = 1; i <= 9 ; i++) {
for (int j = 1; j <= i ; j++) {
System.out.print(i + " * " + j + " = " + (i * j) + "\t");
}
System.out.println();
}
}
}
直角梯形的实现
public class Test1 {
public static void main(String[] args) {
//第一种
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("*");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
/*for (int i = 0; i < 6; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}*/
//第二种
for (int i = 3; i < 9; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
break,continue,return的使用
break在之前我们在switch中见到,用于跳出switch选择语句,在循环中break是指退出当前的整个循环体,执行该循环体下面的语句,同时如果语句紧跟着break,编译报错。而continue只用于循环语句,是指跳过当前循环体的本次循环进行下一次循环。return是指退出整个方法。
使用break来解决卖瓜问题:有1020个西瓜,第一天卖掉总数的一半后又多卖出两个,以后每天卖剩下的一半多两个,问几天以后能卖完?
public class Test2 {
public static void main(String[] args) {
int num = 1020;
int day = 0;
while(true){
if(num <= 0){
//瓜卖完了
break;
}
num = num / 2 - 2;
day ++;
System.out.println("经过" + day + "天" + "瓜还剩" + num);
}
System.out.println("经过" + day + "天,1020个瓜卖完了");
}
}
break练习2:循环录入某学生5门课的成绩并计算平均分,如果某分数录入为负,停止录入并提示录入错误
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入学生的姓名:");
String name = sc.next();
int num = 1;
int score = 0;
int num2 = 0;
while(true){
System.out.println("请输入学生的第" + num + "成绩:");
score= sc.nextInt();
num2 += score;
if(score < 0){
System.out.println("抱歉,分数录入错误,请重新输入;");
System.out.println("平均分为:" + (num2 - score) /(num - 1));
break;
}
num++;
}
}
}
continue练习:循环录入java课的学生成绩,统计分数大于等于80分的学生比例
public class Test4 {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int count = 0;
for(int i = 1; i <= 5; i++){
System.out.println("请输入第" + i + "门课的成绩");
double score = sc.nextDouble();
if(score < 80){
continue;
}
count ++;
}
System.out.println("一共" + count + "门课大于等于80分");
System.out.println("比例为:" + count /5.0 * 100 + "%");
}
}
return练习
public class Test5 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
switch (j){
case 2:
case 3:
case 4:
// return;//退出整个方法
System.exit(-4);//中断程序的运行
}
System.out.print("*");
}
System.out.println();
}
}
}
课后练习1:输出九九成法口诀表,要求从右往左开始
public class Work1 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 0; j < 9 - i ; j++) {
System.out.print("\t" + "\t\t" );
}
for (int j = 1; j <= i ; j++) {
System.out.print(i + " * " + j + " = " + j * i + "\t");
}
System.out.println();
}
}
}
课后练习2:分别使用for、while、do_while实现循环输出100、95、90、85。。。。。5
public class Work2 {
public static void main(String[] args) {
//for循环实现
System.out.println("for循环实现:");
for (int i = 100; i > 0; i--) {
if(i % 5 == 0){
System.out.print(i + "、");
}
}
System.out.println();
//while实现
System.out.println("while实现:");
int num = 100;
while(num > 0){
if(num % 5 == 0){
System.out.print(num + "、");
}
num --;
}
System.out.println();
//do_while 实现
System.out.println("do_while实现:");
int num1 = 100;
do{
if(num1 % 5 == 0){
System.out.print(num1 + "、");
}
num1 --;
}while(num1 > 0);
}
}
课后练习3:从键盘输入一位整数,输入1~7时,输出“星期一 ~ 星期日”,输入0退出程序,输入其他数字提示用户重新输入
public class Work3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean a = true;
do{
System.out.println("请输入一个1~7之间的整数,输入0退出程序:");
int b = sc.nextInt();
if(b == 0){
a = false;
}else if (b >= 1 && b <= 7){
switch (b){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
}
}else{
System.out.println("你输入的数字不合格,只能输入1~7范围的数字!");
}
}while(a);
}
}
课后练习4:使用双循环根据用户输入的数字,输出如下体的图形
public class Work4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("你想输入几行数字:");
int a = sc.nextInt();
for (int i = 1; i <= a; i++) {
for (int j = 0; j < a - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print(i);
}
System.out.println();
}
}
}