习题1:取一个四位数,求各个位数数字的和
package Day2; import java.util.Scanner; public class Demo01 { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("请输入你的卡号"); int cardNum = in.nextInt(); // 1234 int i1 = cardNum % 10; int i2 = cardNum/10%10; int i3 = cardNum/100%10; int i4 = cardNum/1000; int result = i1+i2+i3+i4; System.out.println("会员卡号之和:"+result); } }
结果图:
基本数据类型与包装类之间的转换
package Day2; public class Demo021 { public static void main(String[] args) { // 基本数据类型转换为包装类的方法 valueOf() byte b = 21;//基本数据类型 Byte b2 = Byte.valueOf("21"); //包装数据类型(装箱) System.out.println(b2.MAX_VALUE); System.out.println(b2.MIN_VALUE); System.out.println(b2.SIZE); short s = 123;//基本数据类型 Short s2 = Short.valueOf("123"); System.out.println(s2.MAX_VALUE); System.out.println(s2.MIN_VALUE); System.out.println(s2.SIZE); // int--->Integer valueOf(装箱)可以使用包装类的方法 Integer i1 = Integer.valueOf(5); // Integer--->int xxxValue(拆箱) int i2 = i1.intValue(); // 字符串--->基本数据类型 parseXXX int i3 = Integer.parseInt("123"); System.out.println(i3); boolean b1 = Boolean.parseBoolean("1"); System.out.println(b1); short s3 = Short.parseShort("123"); System.out.println(s3); // 基本数据类型--->字符串 String.valueOf() String s4 = String.valueOf(1); System.out.println(s4); } }
转换总结
package Day2; public class Demo022 { public static void main(String[] args) { // 基本数据类型 ---> 包装类 valueOf(三种) Integer i1 = Integer.valueOf(23); Integer i2 = new Integer(24); Integer i3 = 25; // 包装类 -----> 基本数据类型 xxxValue int i4 = i1.intValue(); int i5 = i2; // String ---- >基本数据类型 int i6 = Integer.parseInt("123"); System.out.println(i6); // 基本数据类型 ----> String String s1 = String.valueOf(123); String s2 = Integer.toString(123); System.out.println(s2); float f = 12.5F; // 强制类型转换会损失精度 int ccy = (int)f; System.out.println(ccy); System.out.println(6.0%4); } }
结果图;
习题2:(if-else语句)
package Day2; import java.util.Scanner; public class Demo03 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入你的成绩"); int score = input.nextInt(); if (score > 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); } else if (score >= 60) { System.out.println("C"); } else { System.out.println("D"); } } }
结果图:
习题3:(switch语句)
package Day2; import java.util.Scanner; public class Demo04 { public static void main(String[] args) { System.out.println("商品列表:"); System.out.println("1:绿茶,三元"); System.out.println("2:冰红茶,三元"); System.out.println("3:脉动,五元"); System.out.println("4:咖啡,五元"); System.out.println("5:酸奶,六元"); Scanner input = new Scanner(System.in); System.out.println("请输入你要购买的商品的序号"); int num = input.nextInt(); switch(num){ 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; default: System.out.println("您输入的商品序号有误"); } } }
结果图:
习题4:(do-while语句嵌套if语句)
package Day2; import java.util.Scanner; public class Demo05 { public static void main(String[] args) { System.out.println("欢迎进入我行我素管理系統"); System.out.println("------------------"); Scanner scanner = new Scanner(System.in); do{ System.out.println("请输入会员号(四位整数)"); int cardNo = scanner.nextInt();//.nextInt:接收整形数据 System.out.println("请输入会员生日[月/日<用两位数表示>]"); String birthDay = scanner.next();//.next:接收字符串数据 System.out.println("请输入积分"); int score = scanner.nextInt(); if(cardNo>999&&cardNo<10000){ System.out.println("会员卡号为"+cardNo); System.out.println("会员生日为"+birthDay); System.out.println("会员积分为"+score); System.out.println("信息录入成功"); break; }else{ System.out.println("登入信息失败"); } }while(true); } }
结果图;
习题5:(do-while语句嵌套if-else语句)
package Day2; import java.util.Scanner; public class Demo06 { public static void main(String[] args) { System.out.println("请输入会员积分"); Scanner input = new Scanner(System.in); boolean goOn = false; do { int cardNo = input.nextInt(); if (cardNo < 2000) { System.out.println("该会员可享受九折"); } else if (cardNo < 4000) { System.out.println("该会员可享受八折"); } else if (cardNo < 8000) { System.out.println("该会员可享受七折"); } else { System.out.println("该会员可享受六折"); } System.out.println("是否继续(y/n)?"); String result = input.next(); if (result.equals("y")) { goOn = true; System.out.println("请再次输入积分"); } else { goOn = false; } } while (goOn); } }
结果图:
习题6:(do-while语句同时嵌套switch和if语句)
package Day2; import java.util.Scanner; public class Demo07 { public static void main(String[] args) { System.out.println("请选择想要购买的商品编号"); System.out.println("1.T恤\t2.网球鞋\t3.网球拍"); Scanner scanner = new Scanner(System.in); boolean goOn = false; do { System.out.println("请输入想要购买的商品编号"); int node = scanner.nextInt(); System.out.println("请输入想要购买的数量"); int num = scanner.nextInt(); int sum1 = 245; int sum2 = 570; int sum3 = 300; switch (node) { case 1: System.out.println("T恤$245.0" + "\t数量" + num + "\t合计$" + sum1 * num); break; case 2: System.out.println("网球鞋$570.0" + "\t数量" + num + "\t合计$" + sum2 * num); break; case 3: System.out.println("网球拍$300.0" + "\t数量" + num + "\t合计$" + sum3 * num); break; default: break; } System.out.println("是否要继续(y/n)?"); String result = scanner.next(); if (result.equals("y")) { goOn = true; } else { goOn = false; int all = (sum1 * num + sum2 * num + sum3 * num); System.out.println("折扣:0.8"); System.out.println("实付金额:" + all * 0.8); System.out.println("应付金额:" + all); System.out.println("找钱:" + all * 0.2); } } while (goOn); System.out.println("欢迎下次光临哦~小肥羊"); } }
结果图:
习题7:(do-while语句嵌套while语句(内涵if语句);外含if语句;注意语句中Continue和break的用法)
package Day2; import java.util.Scanner; public class Demo08 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String password = "111111";//银行卡密码 int passwordNum = 0;//密码输入次数 boolean passWordPass = false;//密码在三次以内通过 do { System.out.println("请输入密码:"); if(!scanner.next().equals("111111")){ passwordNum++; continue; } passWordPass = true; System.out.println("请输入取款金额:"); int money = scanner.nextInt(); while(money > 0){ if(money<=1000 && money%100==0){ System.out.println("您取了" +money+ "元"); System.out.println("交易完成,请取卡!"); break; //完成交易,退出 }else{ System.out.print("您输入金额的金额不合法,请重新输入:"); money = scanner.nextInt(); continue; //继续让用户输入金额 } } } while (passwordNum < 3 && !passWordPass); if(!passWordPass){ System.out.println("密码输入错误三次,请取卡!"); } } }
结果图: