JAVA接收输入语句
import java.util.Scanner; //声明
int cardNum = in.nextInt(); //函数内语句
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(); System.out.println("您输入的会员卡号为:"+cardNum); 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 Demo02 { 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); long l = 123; Long l2 = Long.valueOf("123"); System.out.println(l2.MAX_VALUE); System.out.println(l2.MIN_VALUE); System.out.println(l2.SIZE); float f = 123; Float f2 = Float.valueOf("123"); System.out.println(f2.MAX_VALUE); System.out.println(f2.MIN_VALUE); System.out.println(f2.SIZE); Double d2 = Double.valueOf("123"); System.out.println(d2.MAX_VALUE); System.out.println(d2.MIN_VALUE); System.out.println(d2.SIZE); // int----->Integer valueOf()装箱 可以使用包装类的方法 Integer i1 = Integer.valueOf(5); // Integer----->int intValue()拆箱 int i2 = i1.intValue(); // 字符串----->基本数据类型 parseXXX int i3 = Integer.parseInt("123"); System.out.println(i3); boolean b1 =Boolean.parseBoolean("true"); //只有true为true,其他的都为false System.out.println(b1); // 基本数据类型----->字符串 String.valueOf() String s4 = String.valueOf(true); System.out.println(s4); // 陈总总结 // 基本数据类型--->包装类 Integer iq = Integer.valueOf(23); Integer iq2 = new Integer(24); Integer iq3 = 25; // 包装类--->基本数据类型xxxValue int iq4 = i1.intValue(); int iq5 = iq2; // String--->基本数据类型 int iq6 = Integer.parseInt("123"); // 基本数据类型--->String String ss1 = String.valueOf(123); // or String ss2 = Integer.toString(123); System.out.println(ss2); // 强制类型转换会损失精度 float f23 = 12.5F; int ccy = (int)f23; System.out.println(ccy); System.out.println(6.0%4); } }
一个练习
效果是输入会员号:且会员号不是4位数会报错、重新输入一直到正确
如果会员号输入正确就会打印出会员信息
package day2; import java.util.Scanner; public class Demo05 { public static void main(String[] args) { System.out.println("我行我素购物管理系统 > 客户信息管理 > 添加客户信息"); System.out.println("----------------------------------------"); Scanner in = new Scanner(System.in); // do{ // System.out.println("请输入会员号:(<4位整数>)"); // int num = in.nextInt(); // System.out.println("请输入会员生日:"); // String birthDay = in.next(); // System.out.println("请输入积分:"); // int jifen = in.nextInt(); // if (num > 999 && num < 10000) { // System.out.println("已录入的会员信息是:"); // System.out.println("卡号是:" + num); // System.out.println("生日是:"); // System.out.println(birthDay); // System.out.println("积分是:"); // System.out.println(jifen); // System.out.println("chenggong"); // break; // } else { // System.out.println("错误"); // }}while(true); // or for (int i = 0; i < 3; i++) { System.out.println("请输入会员号:(<4位整数>)"); int num = in.nextInt(); System.out.println("请输入会员生日:"); String birthDay = in.next(); System.out.println("请输入积分:"); int jifen = in.nextInt(); if (num > 999 && num < 10000) { System.out.println("已录入的会员信息是:"); System.out.println("卡号是:" + num); System.out.println("生日是:"); System.out.println(birthDay); System.out.println("积分是:"); System.out.println(jifen); } else { System.out.println("错误"); } } } }
练习之输入积分查看折扣
注意看里面的
System.out.println("是否继续(y/n)?"); String result = in.next(); if (result.equals("y")) { //equals比较两个字符是否相等 goOn = true; System.out.println("请再次输入积分"); }
public class Demo06 { public static void main(String[] args) { System.out.println("请输入积分:"); Scanner in = new Scanner(System.in); boolean goOn = false; do { int score = in.nextInt(); if (score >= 8000) { System.out.println("0.6"); } else if (score >= 7000) { System.out.println("0.7"); } else if (score >= 6000) { System.out.println("0.8"); } else { System.out.println("0.9"); } System.out.println("是否继续(y/n)?"); String result = in.next(); if (result.equals("y")) { //equals比较两个字符是否相等 goOn = true; System.out.println("请再次输入积分"); } } while (goOn); System.out.println("欢迎使用本系统"); } }
练习之计算总价格
package day2; import java.util.Scanner; public class Demo07 { public static void main(String[] args) { System.out.println("请选择购买商品的编号"); System.out.println("1:T恤¥300 \t2:网球鞋¥400\t3:网球拍¥900"); System.out.println("*************************"); Scanner scanner = new Scanner(System.in); int sum = 0;//购买商品总价格 boolean goOn = false; do { System.out.println("请输入您要购买的商品编号:"); int no = scanner.nextInt(); System.out.println("请输入您要购买的商品数量:"); int num = scanner.nextInt(); int account = 0; switch (no) { case 1: account = 300*num; System.out.println("T恤 ¥300"+"\t数量:"+num+"\t合计:¥"+account); break; case 2: account = 400*num; System.out.println("网球鞋 ¥400"+"\t数量:"+num+"\t合计:¥"+account); break; case 3: account = 900*num; System.out.println("T恤 ¥900"+"\t数量:"+num+"\t合计:¥"+account); break; default: break; } sum += account; System.out.println("是否继续(y/n)?"); String s = scanner.next(); if(s.equals("y")){ goOn = true; }else{ goOn = false; System.out.println("折扣:8折;"); System.out.println("应付金额:"+sum*0.8); System.out.println("付款金额为:"); int money = scanner.nextInt(); System.out.println("找零:"+(money - sum*0.8)); } } while (goOn); System.out.println("欢迎下次光临…………"); } }
练习之银行取钱系统
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("密码输入错误三次,请取卡!"); } } }
时间: 2024-11-03 22:16:56