练习1
2012年培养学员25万人,每年增长25%。请问按此增长速度,到哪一年培训学员人数将达到100万人?
package com.day4;
/**
* @Title
* @Author eastlin
* @Description:
*/
//练习1
// 2012年培养学员25万人,每年增长25%。请问按此增长速度,到哪一年培训学员人数将达到100万人?
public class Training1 {
public static void main(String[] args) {
int s=25;
int i=1;
while(s<100){
s*=1.25;
i++;
}
System.out.println("第"+i+"年培训学员人数将达到100万人");
System.out.println("人数为"+s);
}
}
练习2
计算100以内(包括100)的偶数之和
package com.day4;
/**
* @Title
* @Author eastlin
* @Description:
*/
//计算100以内(包括100)的偶数之和
public class Sum2 {
public static void main(String[] args) {
int i=0;
int sum1=0;
while(i<=100){
if((i%2)==0) {
sum1 += i;
}
i++;
}
System.out.println(sum1);
}
}
练习3
需求说明
查询商品价格
循环输入商品编号,显示对应的商品价格
输入“n“结束循环
package com.day4;
import java.util.Scanner;
/**
* @Title
* @Author eastlin
* @Description:
*/
//需求说明
// 查询商品价格
// 循环输入商品编号,显示对应的商品价格
// 输入“n“结束循环,请选择购买的商品编号
//1.T恤 2.网球鞋 3.网球拍
//请输入商品编号
//是否继续(y/n)
public class NeedExplain3 {
public static void main(String[] args) {
System.out.println("请选择购买的商品编号\n1.T恤 2.网球鞋 3.网球拍");
Scanner sc=new Scanner(System.in);
int no=0;
String s="n";
do{
System.out.println("请输入商品编号:");
no=sc.nextInt();
if(no==1){
System.out.println("T恤\t¥245.0 ");
}
if(no==2){
System.out.println("网球鞋 \t¥300.0");
}
if(no==3){
System.out.println("网球拍\t¥145.0");
}
System.out.println("是否继续(y/n):");
s=sc.next();
}while(s.equals("y"));
System.out.println("欢迎下次光临!");
}
}
练习4
升级购物结算
需求说明
循环输入商品编号和购买数量
当输入n时结账
结账时计算应付金额并找零
package com.day4;
import java.util.Scanner;
/**
* @Title
* @Author eastlin
* @Description:
*/
//练习4
// 升级购物结算
// 需求说明
// 循环输入商品编号和购买数量
// 当输入n时结账
// 结账时计算应付金额并找零
public class ShopCompute4 {
public static void main(String[] args) {
System.out.println("请选择购买的商品编号\n1.T恤 2.网球鞋 3.网球拍");
Scanner sc=new Scanner(System.in);
double discount=0.8;
int no=0;
int count=0;
double p=0;
double sum=0,sum1=0,sum2=0,sum3=0;
String s="n";
do{
System.out.printf("请输入商品编号:");
no=sc.nextInt();
System.out.printf("请输入购买数量:");
count=sc.nextInt();
if(no==1){
p=245.0;
sum1+=p*count;
System.out.println("T恤¥"+p+"\t"+"数量"+count+"\t"+"合计¥"+sum1);
}
if(no==2){
p=300.0;
sum2+=p*count;
System.out.println("网球鞋¥"+p+"\t"+"数量"+count+"\t"+"合计¥"+sum2);
}
if(no==3){
p=145.0;
sum3+=p*count;
System.out.println("网球拍¥"+p+"\t"+"数量"+count+"\t"+"合计¥"+sum3);
}
System.out.printf("是否继续(y/n):");
s=sc.next();
}while(s.equals("y"));
sum=sum1+sum2+sum3;
System.out.println("折扣"+discount);
System.out.println("应付金额"+sum*discount);
System.out.printf("实付金额:");
double pay=sc.nextDouble();
System.out.println("找钱"+(pay-(sum*discount)));
System.out.println("欢迎下次光临!");
}
}
练习5
使用do-while实现:输出摄氏温度与华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。
转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32
package com.day4;
/**
* @Title
* @Author eastlin
* @Description:
*/
//练习5
// 使用do-while实现:输出摄氏温度与华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。
// 转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32
public class Conversion5 {
public static void main(String[] args) {
double temp=0,hua;
int count=0;
do{
count++;
hua=temp* 9 / 5.0 + 32;
System.out.println(" 摄氏温度:"+temp+"\t"+"华氏温度 "+hua);
temp+=20;
}while(temp<=250&&count<10);
}
}
练习6
升级菜单切换
需求说明
如果用户输入错误,可以重复输入直到输入正确,执行相应的操作后退出循环
package com.day4;
import java.util.Scanner;
/**
* @Title
* @Author eastlin
* @Description:
*/
//升级菜单切换
// 需求说明
// 如果用户输入错误,可以重复输入直到输入正确,执行相应的操作后退出循环
public class Menu6 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("欢迎来到MyShopping管理信息系统");
System.out.println("*****************************************");
System.out.println("1.客户信息管理\n 2.购物结算\n 3.真情回馈\n4.注销");
int no=0;
System.out.printf("请选择,输入数字:");
no = sc.nextInt();
do {//正确选项只执行一次
if (no == 1) {
System.out.println("1");
}
if (no == 2) {
System.out.println("执行购物结算");
}
if (no == 3) {
System.out.println("3");
}
if (no == 4) {
System.out.println("over");//不写亦可,结束
}
if(no<1||no>4){//循环输出,用while一样
System.out.print("输入错误,请重新输入:");
no=sc.nextInt();
}
} while (no < 1 || no > 4);//错误选项反复执行
System.out.println("程序结束");
}
}