if、switch选择结构/字符串判断相等/获取随机数/数字相除处理
1 package com.unit1.test; 2 3 import java.util.Scanner; 4 5 public class Test5 { 6 7 public static void main(String[] args) { 8 //if结构 9 Scanner input=new Scanner(System.in); 10 System.out.println("请输入分数:"); 11 12 int score=input.nextInt(); 13 14 if(score>80 && score<=100) { 15 System.out.println("优秀!"); 16 17 System.out.println("请输入性别,1:男,2:女"); 18 int sex=input.nextInt(); 19 20 if(sex==1) { 21 System.out.println("奖励一个ipad!"); 22 }else if(sex==2) { 23 System.out.println("奖励一个ihone!"); 24 }else { 25 System.out.println("输入错误!"); 26 } 27 28 }else if(score>70) { 29 System.out.println("良好!"); 30 }else if(score>60) { 31 System.out.println("及格!"); 32 }else if(score<60 && score>=0) { 33 System.out.println("差!"); 34 }else { 35 System.out.println("输入错误!"); 36 } 37 38 } 39 40 }
1 package com.unit1.test; 2 3 import java.util.Scanner; 4 5 public class Test6 { 6 7 public static void main(String[] args) { 8 //字符串判断相等 9 Scanner input=new Scanner(System.in); 10 11 // System.out.println("请输入性别,男,女"); 12 // String sex=input.next(); 13 // 14 // if("男".equals(sex)) { 15 // System.out.println("奖励一个ipad!"); 16 // }else if("女".equals(sex)) { 17 // System.out.println("奖励一个ihone!"); 18 // }else { 19 // System.out.println("输入错误!"); 20 // } 21 22 System.out.println("请输入用户名:"); 23 String user=input.next(); 24 25 System.out.println("请输入密码:"); 26 String pwd=input.next(); 27 28 if("zhangsan".equals(user) && "123".equals(pwd)) { 29 System.out.println("登陆成功!"); 30 }else { 31 System.out.println("用户名或密码错误!"); 32 } 33 34 } 35 36 }
1 package com.unit1.test; 2 3 public class Test7 { 4 5 public static void main(String[] args) { 6 // 例:数字排序 7 int a=123; 8 int b=789; 9 int c=456; 10 11 //第一种方法 12 // if(a>=b && b>=c) { 13 // System.out.println(a+"----"+b+"----"+c); 14 // }else if(a>=c && c>=b) { 15 // System.out.println(a+"----"+c+"----"+b); 16 // }else if(b>=a && a>=c) { 17 // System.out.println(b+"----"+a+"----"+c); 18 // }else if(b>=c && c>=a) { 19 // System.out.println(b+"----"+c+"----"+a); 20 // }else if(c>=a && a>=b) { 21 // System.out.println(c+"----"+a+"----"+b); 22 // }else if(c>=b && b>=a) { 23 // System.out.println(c+"----"+b+"----"+a); 24 // } 25 26 //第二种方法 27 if(a>=b) { 28 if(b>=c) { 29 System.out.println(a+"----"+b+"----"+c); 30 } 31 else if(b<=c) { 32 if(a>=c) { 33 System.out.println(a+"----"+c+"----"+b); 34 }else if(a<=c) { 35 System.out.println(c+"----"+a+"----"+b); 36 } 37 } 38 } 39 else if(b>=a) { 40 if(a>=c) { 41 System.out.println(b+"----"+a+"----"+c); 42 } 43 else if(a<=c) { 44 if(b>=c) { 45 System.out.println(b+"----"+c+"----"+a); 46 } 47 else if(b<=c) { 48 System.out.println(c+"----"+b+"----"+a); 49 } 50 } 51 } 52 53 } 54 55 }
1 package com.unit1.test; 2 3 public class Test8 { 4 5 public static void main(String[] args) { 6 // int place=1; 7 // 8 // switch(place) { 9 // case 1: 10 // System.out.println("奖励一台iphone"); 11 // break; 12 // case 2: 13 // System.out.println("奖励一台ipad"); 14 // break; 15 // case 3: 16 // System.out.println("奖励一台iwatch"); 17 // break; 18 // default: 19 // System.out.println("错误"); 20 // break; 21 // } 22 23 String place="武汉"; 24 switch(place) { 25 case "武汉": 26 System.out.println("湖北省"); 27 break; //中断 28 case "上海": 29 System.out.println("上海"); 30 break; 31 case "深圳": 32 System.out.println("广东省"); 33 break; 34 default: 35 System.out.println("不知道"); 36 break; 37 38 } 39 40 } 41 42 }
1 package com.unit1.test; 2 3 import java.util.Random; 4 5 public class Test9 { 6 7 public static void main(String[] args) { 8 // 获取随机数 9 double re=Math.random(); //获取0-1之间的随机数字 10 // System.out.println(re); 11 // System.out.println((int)(re*100)); 12 13 Random e=new Random(); 14 int re2=e.nextInt(10); //获取0-10之间的随机数字,不包含10 15 System.out.println(re2); 16 17 } 18 19 }
1 package com.unit1.test; 2 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class Test10 { 7 8 public static void main(String[] args) { 9 // 例:掷骰子 10 Scanner input=new Scanner(System.in); 11 System.out.println("欢迎来到掷骰子游戏!"); 12 System.out.println("请选择大小:1大(4-6),2小(1-3)!"); 13 int num=input.nextInt(); 14 15 Random re=new Random(); 16 int point=re.nextInt(6)+1; 17 System.out.println("系统给出的点数是:"+point); 18 19 int result=0; 20 if(point<=3) { 21 result=2; 22 }else if(point>3) { 23 result=1; 24 } 25 26 if(result==num) { 27 System.out.println("恭喜,你赢了!"); 28 }else { 29 System.out.println("对不起,你输了!"); 30 } 31 32 33 } 34 35 }
1 package com.unit1.test; 2 3 import java.util.Scanner; 4 5 public class Test11 { 6 7 public static void main(String[] args) { 8 // 例:超市收银 9 String pName1="苹果"; 10 double pPrice1=3.9; 11 String pNum1="1"; 12 13 String pName2="香蕉"; 14 double pPrice2=2.7; 15 String pNum2="2"; 16 17 String pName3="橘子"; 18 double pPrice3=5.5; 19 String pNum3="3"; 20 21 Scanner input=new Scanner(System.in); 22 System.out.println("输入商品编号:"); 23 String num=input.next(); 24 25 System.out.println("输入商品数量:"); 26 int data=input.nextInt(); 27 28 double price=0; 29 String name=""; 30 31 if(num.equals(pNum1)) { 32 price=pPrice1; 33 name=pName1; 34 }else if(num.equals(pNum2)) { 35 price=pPrice2; 36 name=pName2; 37 }else if(num.equals(pNum3)) { 38 price=pPrice3; 39 name=pName3; 40 }else { 41 System.out.println("输入错误"); 42 } 43 44 double total=price*data; 45 System.out.println("您购买的商品名字是:"+name+",价格是:"+price+",总价是:"+total); 46 47 System.out.println("请输入付款金额:"); 48 double fprice=input.nextDouble(); 49 50 if(fprice>=total) { 51 System.out.println("您的付款金额是:"+fprice+"。需找零:"+(fprice-total)); 52 }else { 53 System.out.println("钱不够"); 54 } 55 56 System.out.println("谢谢惠顾!"); 57 58 } 59 60 }
1 package com.unit1.test; 2 3 import java.text.DecimalFormat; 4 5 public class Test12 { 6 7 public static void main(String[] args) { 8 //数字相除处理 9 double a=15.3; 10 double b=3; 11 12 // System.out.println(a/b); //打印结果:5.1000000000000005 13 14 DecimalFormat df=new DecimalFormat("0.00"); 15 String c=df.format(a/b); //打印结果字符串格式:5.10 16 double result=Double.valueOf(c); //打印结果double格式:5.1 17 18 System.out.println(result); 19 20 } 21 22 }
posted on 2020-06-01 19:38 cherry_ning 阅读(226) 评论(0) 收藏 举报
浙公网安备 33010602011771号