java if选择 题目总结

1、将百分制转为优秀良好

import java.util.Scanner;

public class MyTwelve {
	
	public static void main (String [] args) {
		
		Scanner input = new Scanner (System.in);
		
		System.out.print ("请输入您的成绩:");
		double score = input.nextDouble();
		
		if  ( score >= 90 ) {
			System.out.println("优秀");
		}else if ( score >= 80 ) {
			System.out.println("良好");
		}else if ( score >= 60 ) {
			System.out.println("及格");
		}else {
			System.out.println("吃芥末");
		}
			
		
	}
}

2、输入三个数,比较大小, 将最大的输出

import java.util.Scanner;

public class MyTwelve {
	
	public static void main (String [] args) {
		
		Scanner input = new Scanner (System.in);
		
		System.out.print ("请输入您的成绩:");
		double score = input.nextDouble();
		
		if  ( score >= 90 ) {
			System.out.println("优秀");
		}else if ( score >= 80 ) {
			System.out.println("良好");
		}else if ( score >= 60 ) {
			System.out.println("及格");
		}else {
			System.out.println("吃芥末");
		}
			
		
	}
}

3、输入三个数,按照从小到大排列并输出

方法1:

import java.util.Scanner;
/**
*输入三个整数,按照从小到大排列
*/
public class Test3 {
	public static void main (String [] args) {
		
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("请输入第一个数a:");
		int a = input.nextInt();
		
		System.out.print("请输入第二个数b:");
		int b = input.nextInt();
		
		System.out.print("请输入第三个数c:");
		int c = input.nextInt();
		
		
		
		//1、首先a>b   (1)c和a相比 如果c大于a,bca
		//             (2)c和a相比 如果c小于a,还要看c和b相比,1)如果c大于b  bca
	    //                                                       2)如果b大于c  cba
		if(a>b){
			
			//(1)c和a相比 如果c大于a,bca
			if(c>a){
				System.out.println("bac");
			}else{
				//(2)c和a相比 如果c<a
				if(c>b){
					//1)如果c大于b  bca
					System.out.println("bca");
				}else{
					//2)如果b大于c  cba
					System.out.println("cba");
				}
			}
			
			
	    //2、b>a  c和b比较 (1)如果c大于b  abc
		//                 (2)如果c小于b  c和a比较   1)c大于a    acb
		//                                             2)c小于a    cab  
		}else{
			                       
			if(c>b){
				//(1)如果c大于b  abc
				System.out.println("abc");
				
			//(2)如果c小于b  c和a比较 
			}else{
				if(c>a){
					//1)c大于a    acb
					System.out.println("acb");
				}else{
					//2)c小于a    cab  
					System.out.println("cab");
				}
			}
		}
	}
}

方法二:

import java.util.Scanner;
/**
*输入三个数,按照从小到大的顺序输出
*/

public class Test8 {
	
	public static void main (String []args) {
		
		int max;
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("请输入第一个数字a:");
		int a = input.nextInt();
		
		System.out.print("请输入第二个数字b:");
		int b = input.nextInt();
		
		System.out.print("请输入第三个数字c:");
		int c = input.nextInt();
		
		//比较三个数的大小,首先比较两个数,再和第三个数比较;
		//a和b比较 
		
		if(a>b){
			//a为3 b是2 c为1
		
			max = a;//a放到max=3
			a = b;//b放到a=2
			b = max;//max放到b=3
			//执行之后 a = 2  b = 3
		}
		if(a>c){
			
			//a为2 b是3 c为1 执行之后c = 2 a = 1
			
			max = a;
			a = c;
			c = max;
			
		}
		if(b>c){
			
			// a=1 b=3 c=2 执行之后 b=2 c=3
			max = b;
			b = c;
			c = max;
			
		}
		System.out.print("从小到大:"+a+"<"+b+"<"+c);
	}
}

4、输入一个年份,判断是平年还是闰年

import java.util.Scanner;
/**
*输入一个年份,判断闰年还是平年
能被4整除且不能被100整除;
能被400整除。
任意满足以上的条件之一就是闰年。
*/
public class Test4 {
	
	public static void main (String [] args) {
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("请输入一个年份:");
		int years = input.nextInt();
		
		if((years%4==0)&&!(years%100==0)||(years%400==0)){
			System.out.println("闰年");
		}else{
			System.out.println("平年");
		}
		
	}
}

5、输入一个数, 判断是奇数还是偶数

import java.util.Scanner;
/**
*输入一个数,判断是奇数还是偶数
*/
public class Test5 {
	
	public static void main (String [] args){
		
		Scanner input = new Scanner(System.in);
		System.out.print("输入一个整数:");
		
		int number = input.nextInt();
		
		if(number%2==0){
			System.out.println("偶数");
		}else{
			System.out.println("奇数");
		}
	}
}

6、换算狗的年龄,前两年相当于人类的10.5岁,之后每一年相当于人类的4岁

import java.util.Scanner;
/**
*换算狗的年龄,前两年相当于人类的10.5岁,之后每一年相当于人类的4岁
*/
public class Test6{
	
	public static void main(String [] args){
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("请输入狗狗的年龄:");
		int years = input.nextInt();
		
		if( years > 2 ) {
			double years1 = (years-2)*4+(2*10.5);
			
			System.out.println("相当于人的年龄:"+years1);
		}else if( (years>0) && (years<=2) ) {
			System.out.println("相当于人的年龄:"+years*10.5);
		}else if(years<0){
			System.out.println("非法");
		}
		
	}
}

7、李蕾买手机需要7500,将自己的破手机抵押给新手机店,获得8折优惠;将破手机卖出,可获得1500,请问哪种实惠

/**
*李蕾买手机需要7900,将自己的破手机抵押给新手机店,获得8折优惠;将破手机卖出,可获得1500,请问哪种实惠
*/
import java.util.Scanner;

public class My7_6_6 {
	
	public static void main (String []args){
		
		//Scanner input = new Scanner(System.in);
		
		//System.out.println("请输入你的选择: 1、选择优惠    2、卖掉");
		//int num = input.nextInt();
		double pay1 = 7900*0.8;
		double pay2 = 7900 - 1500;
		
		
		if (pay1<pay2){
			
			System.out.println("选择优惠,一共需要花费"+pay1);
			
		}else{
		
			System.out.println("选择卖掉,一共需要花费"+pay2);
		}
		
		
		}
	}

8、

*税前工资需要交0.1的三险一金
*三险一金后工资大于5000 需要交税
*缴费比例为超过5000的0~3000       0.03
*          超过5000的3000~12000   0.1
*          超过5000的12000~25000  0.2
*          超过5000的25000~35000  0.25
*          超过5000的35000~55000  0.3
*          超过5000的55000~80000  0.35
*          超过8000的部分交税     0.45   

import java.util.Scanner;

/**
*税前工资需要交0.1的三险一金
*三险一金后工资大于5000 需要交税
*缴费比例为超过5000的0~3000       0.03
*          超过5000的3000~12000   0.1
*          超过5000的12000~25000  0.2
*          超过5000的25000~35000  0.25
*          超过5000的35000~55000  0.3
*          超过5000的55000~80000  0.35
*          超过8000的部分交税     0.45		  
*/
public class My7_6_10{
	
	public static void main(String [] args){
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("请输入你的工资:");
		double solory = input.nextDouble();
		
		//首先计算三险一金后工资
		double solory1 = solory - solory*0.1;
		//计算三险一金后多出5000多少
		double solory2 = solory - solory*0.1 - 5000;
		//判断三险一金后工资多出范围
		if(solory2 >0){
			
			if(solory2 <= 3000){
				//计算交税
				double solory3 = solory1 - solory2*0.03;
				System.out.println("到手工资为:"+solory3);
			}else if((solory2 > 3000) && ( solory2<=12000 )){
				//计算交税
				double solory3 = solory1 - solory2*0.1;
				System.out.println("到手工资为:"+solory3);
			}else if((solory2 <= 25000)&&(solory2 > 12000)){
				//计算交税
				double solory3 = solory1 - solory2*0.2;
				System.out.println("到手工资为:"+solory3);
			}else if((solory2 > 25000)&&(solory2 <= 35000)){
				//计算交税
				double solory3 = solory1 - solory2*0.25;
			    System.out.println("到手工资为:"+solory3);
			}else if((solory2 <= 55000)&&(solory2 > 35000)){
				//计算交税
				double solory3 = solory1 - solory2*0.3;
			    System.out.println("到手工资为:"+solory3);
			}else if((solory2 <= 80000)&&(solory2 > 55000)){
				//计算交税
				double solory3 = solory1 - solory2*0.35;
			    System.out.println("到手工资为:"+solory3);
			}else if(solory2 > 80000){
				//计算交税
				double solory3 = solory1 - solory2*0.45;
			    System.out.println("到手工资为:"+solory3);
			}
		}else{
			System.out.println("对不起,您不用交税!");
		}
	}
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
选择语句是在编程用来根据条件执行不同操作的一种语句,例如Javaif语句和switch语句。if语句通过判断一个条件的真假来执行不同的代码块。例如,如果条件为真,则执行特定的代码,而如果条件为假,则跳过该代码块继续执行下一段代码。switch语句根据一个表达式的值来选择执行不同的代码块。根据表达式的值,不同的case会被匹配并执行对应的代码块,而default子句则为未匹配任何case的值提供了一个备选的执行代码块。 循环语句是用来重复执行一个或一组语句的语句,通常用于处理需要重复操作的情况。Java有三种常见的循环语句:for循环、while循环和do-while循环。for循环在执行前先初始化一个变量,然后根据给定的条件判断是否继续循环,最后在每次循环结束后更新变量的值。while循环会在每次循环开始前先判断给定的条件是否为真,只有条件为真时才会执行循环体内的代码,并在每次循环结束后再次判断条件。do-while循环先执行一次循环体内的代码,然后再判断给定的条件是否为真,只有条件为真时才会继续执行循环。 选择语句和循环语句在编程非常常用,可以根据不同的条件和需要,选择执行不同的代码块,或者重复执行一组代码,提高代码的灵活性和重用性。同时,合理运用选择语句和循环语句,可以帮助程序在不同的情况下正确地执行不同的逻辑,提高程序的健壮性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值