首页 > 代码库 > 简单的java题分享交流

简单的java题分享交流

1、条件判断作业

作业一:

某朋友说,如果存款超过100万,则入手宝马X5,否则超过50万,则入手奥迪A6,否则超过20万,则入手比亚迪,否则超过10万则玩玩极品飞车游戏,请编写一个购车选择程序,根据输入的金额,提示可以购买的车型。

        

代码如下:

/**

*作用:根据您输入的资产判断您应该能买什么车

*作者:李林雄

*/

import java.util.Scanner;

public class Test3Buy{

         public static void main(String[] args){

                   //创建一个从键盘录入的工具

                   Scanner input = new Scanner(System.in);

                   //创建一个可以接收从键盘录入的数值

                   System.out.print("请输入您朋友的次产有多少单位为万:");

                   int money = input.nextInt();

                   if(money >= 0){

                            if(money >=100){

                                     System.out.println("您朋友很有钱,可以买一辆宝马X5");

                            }else if(money >= 50){

                                     System.out.println("您朋友比较有钱,可以买一辆奥迪A6");

                            }else if(money >= 20){

                                     System.out.println("您朋友有点钱,可以买一辆比亚迪F0");

                            }else if(money >= 10){

                                     System.out.println("您朋友存款不是很充足,只能玩玩极品飞车游戏");

                            }else{

                                     System.out.println("您朋友坐坐公交车就好了");

                            }

                   }else{

                            System.out.println("请输入正确的金额的");

                   }

         }

}

输出结果如下:

 

 

 

 

 

作业2:

从键盘录入学员的成绩x,根据学员的成绩x给予奖励,如果成绩x小于等于80分的不予奖励并罚一个iphone6s,成绩81<=x<=90则奖励一个iphone4,如果成绩91<x<=100则奖励一个iphone4,再奖励一个ipad

 

代码如下:

/**

*作用:根据学员成绩给予不同奖励

*作者:李林雄

*/

import java.util.Scanner;

public class Test4Iphone{

         public static void main(String[] args){

                   Scanner input = new Scanner(System.in);

                   System.out.print("请输入您的成绩");

                   int score = input.nextInt();

                   if(score <= 100 && score >0){

                            if(score >=91){

                                     System.out.println("奖励一个iphone9");

                                     System.out.println("再奖励一个ipad");

                            }else if(score >= 81){

                                     System.out.println("奖励一个iphone9");

                            }else{

                                     System.out.println("不予奖励并罚一个iphone6s");

                            }

                   }else{

                            System.out.println("输入的成绩无效");

                   }

                  

         }

}

 

 

 

作业3:

从键盘录入输入3 个数a,b,c,按从大到小进行输出(难度大)

 

代码如下:

/**

*作用:使用if else 从大到小排序三个数

*作者:李林雄

*/

//导入Scanner工具包

import java.util.Scanner;

public class Test2Sort{

         public static void main(String[] args){

                   //创建可以从键盘录入的工具

                   Scanner input = new Scanner(System.in);

                   System.out.println("请输入您的第一个数:");

                   //创建一个可以接收从键盘录入的变量

                   int a = input.nextInt();

                   System.out.println("请输入您的第二个数:");

                   int b = input.nextInt();

                   System.out.println("请输入您的第三个数:");

                   int c = input.nextInt();

                   if(a >= b && a >= c){

                            if(b >= c){

                                     System.out.println(a+">"+b+">"+c);

                            }else{

                                     System.out.println(a+">"+c+">"+b);

                            }

                   }else if(b >= a && b >= c){

                            if(a >= c){

                                     System.out.println(b+">"+a+">"+c);

                            }else{

                                     System.out.println(b+">"+c+">"+a);

                            }

                   }else if(c >= a && c >= b){

                            if(a >= b){

                                     System.out.println(c+">"+a+">"+b);

                            }else{

                                     System.out.println(c+">"+b+">"+a);

                            }

                   }

         }

 

}

输出结果:

 

 

 

作业4:计算个人所得税,关乎大家的生计

要求使用程序实现个人所得税计算器,实现从键盘录入当月工资收入,输出应缴个人所得税

我们此处简化五险一金,假设为0

 

代码:

/**

*作用:用于个人所得税的缴纳计算

*作者:李林雄

*/

import java.util.Scanner;

public class Test5Tax{

         public static void main(String[] args){

                   //创建一个新的键盘录入工具

                   Scanner input = new Scanner(System.in);

                   System.out.println("请输入您的收入工资为多少单位(元):");

                   //创建一个double类型的变量用于接受键盘输入的数值

                   double wage = input.nextInt();

                   System.out.println("请输入您要交税的方式输入1为:个人交税,输入2位为单位代缴税");

                   int mode = input.nextInt();

                   double tax;

                   //判断输入的工资是否正确以及交税的方式是否正确

                   if(wage >0 && (mode == 1 || mode == 2)){

                            //如果选择第1则从第一种交税方式

                            if(mode == 1){

                                     if((wage-3500)>80000){

                                               tax = (wage-3500)*0.45-13505;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>55000){

                                               tax = (wage-3500)*0.35-5505;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>35000){

                                               tax = (wage-3500)*0.3-2755;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>9000){

                                               tax = (wage-3500)*0.25-1055;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>4500){

                                               tax = (wage-3500)*0.2-555;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>1500){

                                               tax = (wage-3500)*0.1-105;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>0){

                                               tax = (wage-3500)*0.03;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else{

                                               System.out.println("您不需要缴纳个人所得税");

                                     }

                            }else{//如果选择第2则从第2种交税方式

                                     if((wage-3500)>57505){

                                               tax = (wage-3500)*0.45-13505;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>41255){

                                               tax = (wage-3500)*0.35-5505;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>27255){

                                               tax = (wage-3500)*0.3-2755;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>7755){

                                               tax = (wage-3500)*0.25-1055;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>4155){

                                               tax = (wage-3500)*0.2-555;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>1455){

                                               tax = (wage-3500)*0.1-105;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else if((wage-3500)>0){

                                               tax = (wage-3500)*0.03;

                                               System.out.println("您需要缴纳个人所得税为:"+tax+"元");

                                     }else{

                                               System.out.println("您不需要缴纳个人所得税");

                                     }

                            }

                   }

                   else{

                            System.out.println("请输入正确的收入,以及正确的缴费方式");

                   }

         }

}

输入结果如下:

 

 

进制转换作业

作业1:课堂练习

练习一:0b100,0100, 0x100 这些结果分别是多少?

结果分别为:4、64、256

验证代码如下:

代码:

/**

*作用:验证各进制的转化

*作者:李林雄

*/

public class Test7AU{

         public static void main(String[] args){

                   //将二进制转化为十进制

                   System.out.println("0b100的十进制结果为:"+(0b100));

                   //将八进制转化为十进制

                   System.out.println("0100的十进制结果为:"+(0100));

                   //将十六进制转化为十进制

                   System.out.println("0x100的十进制结果为:"+(0x100));

         }

}

输出结果为:

 

 

练习2:0b111,0123, 0x1ef 这些结果分别是多少?

0b111转化为10进制为:7计算方式为:1*1+2*1+4*1=7

0123转化为10进制为:1*3+8*2+1*64=83

0x1ef转化为10进制为:1*15+16*14+256*1=495

代码验证:

代码如下:

/**

*作用:进制的转化

*作者:李林雄

*/

public class Test8Zh{

         public static void main(String[] args){

                   System.out.println("0b111转化为十进制为:"+(0b111));

                   System.out.println("0123转化为十进制为:"+(0123));

                   System.out.println("0x1ef转化为十进制为:"+(0x1ef));

         }

}

 

 

练习3:

将10转换为2进制

转化过程如下:

 

计算的结果区余从下往上以此取出结果为:1010

代码验证:

代码如下:

/**

*作用:进制的转化

*作者:李林雄

*/

public class Test8Zh{

         public static void main(String[] args){

                   System.out.println("0b1010转化为十进制为:"+(0b1010));

         }

}

 

输出的结果为:

 

 

练习4:

10转换为8进制以及16进制:

10转化为8进制结果为:012 计算如下图:

 

10转化为16进制结果为0xa 计算如下图:由于10在16进制里面表示为a所以为0xa

 

代码验证:

代码如下:

/**

*作用:进制的转化

*作者:李林雄

*/

public class Test8Zh{

         public static void main(String[] args){

                   System.out.println("012转化为十进制为:"+(012));

                   System.out.println("0xa转化为十进制为:"+(0xa));

         }

}

输入结果如下:

 

位运算练习:

练习1:

int a = 8;

System.out.println(a>>2);

输出结果为:2

System.out.println(a<<2);

输出结果为:32

System.out.println(2&2);

输出结果为:2

System.out.println(2|2);

输出的结果为:2

System.out.println(2^2);

输出的结果为:0

练习2:

a=2;

System.out.println(a<<4);

输出结果为:32

System.out.println(2&1);

输出结果为:0

System.out.println(2|1);

输出结果为:3

System.out.println(2^1);

输出结果为:3

 

 

作业2:请用最有效率的方式写出计算2乘以8、2乘以32的运算方式

最有效率的方式为位运算:

代码如下:

public class Test6Compute{

       public static void main(String[] args){

              System.out.println("以下是最有效率的方式来计算2乘以8以及2乘以16");

              System.out.println("***********完美的开始线****************");

              System.out.println("通过位运算:2<<3 = "+(2<<3));

              System.out.println("***********完美的分割线****************");

              System.out.println("通过位运算:2<<4 = "+(2<<4));

              System.out.println("***********完美的结束线****************");

       }

}

代码的输出结果为:

 

 

作业3:进制转换练习

1、0b1111,0234,0x789 这些转换为十进制分别是多少

0b1111:结果为15 计算方式为:1*1+2*1+4*1+8*1=15

0234:结果为156 计算方式为4*1+8*3+64*2=156

0x789:结果为1929 计算方式为:9*1+16*8+256*7=1929

代码验证:

代码如下:

public class Test6Compute{

       public static void main(String[] args){

              System.out.println(0b1111);

              System.out.println(0234);

              System.out.println(0x789);

       }

}

输入结果为:

 

 

2、将20分别转换为二进制,八进制,十六进制

20转为2进制结果为:0b10100

20转为八进制为:024

20进制转化为16进制为:0x14

代码验证:

代码如下:

public class Test6Compute{

       public static void main(String[] args){

              System.out.println(0b10100);

              System.out.println(024);

              System.out.println(0x14);

       }

}

输出结果为:

 

简单的java题分享交流