标签:
System.out.println("第1遍写:好好学习,天天向上!");
System.out.println(“第2遍写:好好学习,天天向上!");
……
System.out.println(“第100遍写:好好学习,天天向上!");
public class WhileDemo1 {
public static void main(String[] args) {
String str="好好学习,天天向上";
int i=1;
while (i<=100) {
System.out.println("第"+i+"次写:"+str);
i++;
}
}
}
while ( 循环条件 ) {
循环操作
}
public class WhileDemo {
public static void main(String[] args) {
int i=1;
while (i<=50) {
System.out.println("打印第"+i+"份试卷");
i++;
}
}
}
public class WhileDemo2 {
public static void main(String[] args) {
System.out.print("合格了吗?(y/n):");
Scanner input = new Scanner(System.in);
String answer = input.next();
while("n".equals(answer)){
System.out.println("上午阅读教材!");
System.out.println("下午上机编程!\n");
System.out.print("合格了吗?(y/n):");
answer = input.next();
}
System.out.println("完成学习任务!");
}
}
public class EvenSum {
/*
* 计算100以内偶数之和
*/
public static void main(String[] args) {
int sum = 0; // 当前之和
int num = 2; // 加数
while (num <= 100) {
sum = sum + num; // 累加
num = num + 2;
}
System.out.println("100以内的偶数之和为:" + sum);
}
}
public class PriceLookup {
/*
* 商品价格查询
*/
public static void main(String[] args) {
String name = ""; // 商品名称
double price = 0.0; // 商品价格
int goodsNo = 0; // 商品编号
System.out.println("MyShopping管理系统 > 购物结算\n");
// 商品清单
System.out.println("*******************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T 恤 2.网球鞋 3.网球拍");
System.out.println("*******************************************");
Scanner input = new Scanner(System.in);
String answer = "y"; // 标识是否继续
while ("y".equals(answer)) {
System.out.print("请输入商品编号:");
goodsNo = input.nextInt();
switch (goodsNo) {
case 1:
name = "T 恤";
price = 245.0;
break;
case 2:
name = "网球鞋";
price = 570.0;
break;
case 3:
name = "网球拍";
price = 320.0;
break;
}
System.out.println(name + "\t" + "¥" + price + "\n");
System.out.print("是否继续(y/n)");
answer = input.next();
}
System.out.println("程序结束!");
}
}
经过几天的学习,老师给张浩一道测试题,让他先上机编写程序完成,然后老师检查是否合格。如果不合格,则继续编写。……
do {
循环操作
} while ( 循环条件 );
public class DoWhileDemo {
/*
* 如何使用do-while循环
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer = ""; // 标识是否合格
do {
System.out.println("上机编写程序!");
System.out.print("合格了吗?(y/n)");
answer = input.next();
System.out.println("");
} while (!"y".equals(answer));
System.out.println("恭喜你通过了测试!");
}
}
标签:
原文地址:http://www.cnblogs.com/mentorStudio/p/4635578.html