运算符与选择结构的练习(if / if elseif / switch)

运算符与选择结构的练习(if / if elseif / switch)

	public static void main(String[] args) {
		System.out.println("请输入机考成绩:");
		Scanner src = new Scanner(System.in);
		int jikao = src.nextInt();
		System.out.println("请输入理论成绩:");
		int lilun = src.nextInt();
		System.out.println("请输入综合成绩:");
		int zonghe = src.nextInt();
		
		boolean condation1 = jikao>=90 && lilun>=90;
		boolean condation2 = (jikao+lilun)/2>=80 && zonghe>=90;
		if(condation1 || condation2){
			System.out.println("考试通过");
		}else{
			System.out.println("no");
		}
		src.close();
	}

运算符与选择结构的练习(if / if elseif / switch)

public static void main(String[] args) {
		System.out.println("请输入购买数量:");
		Scanner src = new Scanner(System.in);
		int kucun = 10;  //库存
		int input = src.nextInt();
		if(input < 0){
			System.out.println("您输入的数据有误,请重新输入。");
		}else if(input > kucun){
			System.out.println("抱歉,没有足够的库存。");
			System.out.println("当前库存数为:"+(kucun));
		}else{
			System.out.println("购买成功。");
			System.out.println("当前库存数为:"+(kucun-input));
		}	
		src.close();
	}

运算符与选择结构的练习(if / if elseif / switch)

@Test
	public void test2() {
		double salary = 0; //工资
		double rate = 0; //税率
		int cut = 0; //速算扣除数
		
		System.out.println("请输入工资:");
		salary = new Scanner(System.in).nextDouble();
		if(salary < 3500) {
			rate = 0;
			cut = 0;
		} else if((salary-3500)>=1500 && (salary-3500)<4500) {
			rate = 10 / 100;
			cut = 105;
		} else if((salary-3500)>=4500 && (salary-3500)<9000) {
			rate = 20 / 100;
			cut = 555;
		} else if((salary-3500)>=9000 && (salary-3500)<35000) {
			rate = 25 / 100;
			cut = 1005;
		} else if((salary-3500)>=35000 && (salary-3500)<55000) {
			rate = 30 / 100;
			cut = 2755;
		} else if((salary-3500)>=55000 && (salary-3500)<80000) {
			rate = 35 / 100;
			cut = 5505;
		} else {
			rate = 45 / 100;
			cut = 13505;
		}
		
		System.out.println("税前工资:"+salary);
		double totle = salary - (salary-3500)*rate-cut;
		System.out.println("税后工资:"+totle);
	}

运算符与选择结构的练习(if / if elseif / switch)

运算符与选择结构的练习(if / if elseif / switch)

@Test
	public void test3(){
	    System.out.println("请输入数字:");
		Scanner src2 = new Scanner(System.in);
		int num = src2.nextInt();
		
		switch(num){
		case 0:
			System.out.println("星期一");
			break;
		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;
		case 6:
			System.out.println("星期日");
			break;
		default:
			System.out.println("请输入0-6之间的数字");
		}
	}

运算符与选择结构的练习(if / if elseif / switch)

@Test
	public void test2(){
		//做一个登录效果,输入用户名和密码,如果用户名是:张三
		//密码是:123 就提示登录成功,否则提示登录失败
		System.out.println("请输入用户名:");
		Scanner src1 = new Scanner(System.in);
		String name = src1.next();
		int password = 123;
		if("张三".equals(name)){ /****正确写法,正确写法,正确写法,正确写法****/
			if( password == 123){
				System.out.println("密码正确");
			}else{
				System.out.println("密码错误");
			}
			System.out.println("登陆成功!!!");
		}else{
			System.out.println("用户名输入错误");
		}
	}

运算符与选择结构的练习(if / if elseif / switch)

@Test
	public void test3(){
		double num1 = 0;
		double num2 = 0;
		String flag = "";//符号
		double result = 0;//运算结果
		
		System.out.println("请输入一个数:");
		num1 = new Scanner(System.in).nextDouble();
		System.out.println("请再输入一个数:");
		num2 = new Scanner(System.in).nextDouble();
		System.out.println("请输入一个符号(+ - * /)");
		flag = new Scanner(System.in).next();
		
		if("+".equals(flag)){
			result = num1 + num2;
		}else if("-".equals(flag)){
			result = num1 - num2;
		}else if("*".equals(flag)){
			result = num1 * num2;
		}else if("/".equals(flag)){
			if(num2 != 0){
				result = num1 / num2;
			}else{
				System.out.println("除数不能为0");
			}
		}else{
			System.out.println("请按要求输入运算符号");
		}
		System.out.println("程序的运行结果为:"+result);
	}
	

运算符与选择结构的练习(if / if elseif / switch)

@Test
	public void test1(){
		
		System.out.println("请输入月份:");
		Scanner src = new Scanner(System.in);
		int month = src.nextInt();
		switch(month){
		case 1 :
		case 3 :
		case 5 :
		case 7 :
		case 8 :
		case 10 :
		case 12 :
			System.out.println(month+"月有31天");
			break;
		case 2 :
			System.out.println(month+"月有28天");
			break;
		case 4 :
		case 6 :
		case 11 :
			System.out.println(month+"月有30天");
		    break;
		default:
		    System.out.println("请正确输入月份!!!");
		}
	}

运算符与选择结构的练习(if / if elseif / switch)

运算符与选择结构的练习(if / if elseif / switch)
运算符与选择结构的练习(if / if elseif / switch)

@Test
	public void test4(){
		//员工根据A B C 等级进行加薪,分别为500 300 100
		System.out.println("请输入你的工资:");
		Scanner src4 = new Scanner(System.in);
		int salary = src4.nextInt();
		System.out.println("请输入你的等级(A B C):");
		String grade = src4.next();
		int A = 500;
		int B = 300;
		int C = 100;
		
		switch(grade){
			case "A" :
				System.out.println("您的工资为:"+(salary+A));
				break;
			case "B" :
				System.out.println("您的工资为:"+(salary+B));
				break;
			case "C" :
				System.out.println("您的工资为:"+(salary+C));
				break;
		}
	src4.close();	
	}

运算符与选择结构的练习(if / if elseif / switch)
运算符与选择结构的练习(if / if elseif / switch)

@Test
	public void test2(){
		System.out.println("请输入月份:");
		Scanner src1 = new Scanner(System.in);
		int jije = src1.nextInt();
		switch(jije){
		case 3:
		case 4:
		case 5:
			System.out.println("春季");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("夏季");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("秋季");
			break;
		case 12:
		case 1:
		case 2:
			System.out.println("冬季");
			break;
		default :
			System.out.println("请输入1-12之间的月份");
		}
	}

运算符与选择结构的练习(if / if elseif / switch)
运算符与选择结构的练习(if / if elseif / switch)

上一篇:3.2 if语句实现选择结构


下一篇:HDU 4920 Matrix multiplication(bitset)