需求1:某朋友说,如果存款超过100万,则入手宝马X5,否则超过50万,则入手奥迪A6,否则超过20万,则入手比亚迪,否则超过10万则玩玩极品飞车游戏,请为他编写一个购车选择程序,根据输入的金额,提示可以购买的车型。
public class Test{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入存款(万):");
double money = scan.nextDouble();
if (money >= 100){
System.out.println("入手宝马X5");
}else if(money >= 50){
System.out.println("入手奥迪A6");
}else if(money >= 20){
System.out.println("入手比亚迪");
}else if(money > 10){
System.out.println("玩玩极品飞车游戏");
}else{
System.out.println("您余额不足!");
}
}
}
需求2:计算个人所得税,关乎大家的生计。要求使用程序实现个人所得税计算器,实现从键盘录入当月工资收入,输出应缴个人所得税。
工资个税的计算公式为:
应纳税额=(工资薪金所得 -“五险一金”-扣除数)×适用税率-速算扣除数
扣除标准:个税按3500元/月的起征标准算
我们此处简化五险一金,假设为0,则
如果某人的工资收入为5000元,他应纳个人所得税为:(5000—3500)×3%—0=45(元)。
税率表如下:
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入您本月工资:");
int salary = input.nextInt();
int taxSalary = salary-3500; //应税工资
double rate=0,reduce=0; //税率,扣除数
if(taxSalary<0){
System.out.println("您享受0税率");
}else if(taxSalary<=1500){
rate = 0.03;
}else if(taxSalary<=4500){
rate = 0.1;
reduce = 105;
}else if(taxSalary<=9000){
rate = 0.2;
reduce = 555;
}else if(taxSalary<=35000){
rate = 0.25;
reduce = 1005;
}else if(taxSalary<=55000){
rate = 0.3;
reduce = 2755;
}else if(taxSalary<=80000){
rate = 0.35;
reduce = 5505;
}else{
rate = 0.45;
reduce = 13505;
}
System.out.println("您应交税:"+(taxSalary*rate-reduce));
}
}
需求3:求出1-100之间偶数和。
public class Test{
public static void main(String[] args){
//方法一
int sum = 0;
for(int i=2; i<=100; i+=2){
sum += i;
}
System.out.println("偶数和为:"+sum);
//方法二
int count= 0;
for(int j=1; j<=100; j++){
if(j%2 == 0){
count += j;
}
}
System.out.println("偶数和为:"+count);
}
}
需求4:计算从1到100个值以内所有能被3或者17整除的数的和并输出。
public class Test{
public static void main(String[] args){
int sum = 0;
for(int i=1; i<=100; i++){
if(i%3 == 0 || i%17 == 0){
sum += i;
}
}
System.out.println("能被3或者17整除的数的和为:"+sum);
}
}
需求5:3000米长的绳子,每天减一半。问多少天这个绳子会小于5米?不考虑小数。
public class Test{
public static void main(String[] args){
//方法一
int day = 0;
int len = 3000;
while(len>5){
len /= 2;
day++;
}
System.out.println("第" + day + "天这个绳子会小于5米");
//方法二
int d = 0;
for(int i=3000; i>5; i/=2){
d++;
}
System.out.println("第" + d + "天这个绳子会小于5米");
}
}
需求6:求2~100之内的素数。【素数 : 只能被1或本身整除的数】
public class Test{
public static void main(String[] args){
System.out.println("2到100之内的素数有:");
for(int i=2; i<=100; i++){
boolean flag = true;
for(int j=2; j<i; j++){
if(i%j == 0){
flag = false;
break;
}
}
if(flag){
System.out.print(i+" ");
}
}
System.out.println();
}
}
需求7:模拟ATM取款机的三次密码校验,当天达到3次输入密码错误,则提示“账号被冻结”,其他情况则提示还有多少次输入机会。
效果图如下:
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
for(int i=2; i>=0; i--){
System.out.println("请输入密码:");
String password = scan.next();
if(password.equals("123456")){
System.out.println("随便取款吧!");
break;
}else if(i!=0){
System.out.println("您还有" + i + "次机会");
System.out.println("是否继续录入密码?继续请输入y,退出请输入其它任意键");
String str = scan.next();
if(!str.equals("y")){
System.out.println("感谢您使用**银行系统,欢迎下次再来");
break;
}
}else if(i==0){
System.out.println("账号已被冻结,请携带身份证前往柜台办理");
}
}
}
}
需求8:要求循环录入2个班的学员成绩,假设每个班都有3个学员,依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int num = 0;
int sum = 0;
for(int i=1; i<=2; i++){
for(int j=1; j<=3; j++){
System.out.println("请输入第" + i + "个班第" + j + "个学员的成绩");
int score = scan.nextInt();
if(score>90){
num++;
sum += score;
}
}
}
System.out.println("超过90分的学员人数:" + num);
System.out.println("超过90分的学员平均分:" + (sum/num));
}
}
需求9:编写自助银行服务。
import java.util.Scanner;
public class bank{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
boolean flag = true;
do{
System.out.println("===========欢迎使用ATM自助银行服务===========");
System.out.println("1:开户 2:存款 3:取款 4:转账 5:查询余额 6:修改密码 0:退出");
System.out.println("=============================================");
System.out.println("请输入操作编号:");
int num = scan.nextInt();
switch(num){
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;
case 0:
System.out.println("---执行退出功能---");
flag = false;
break;
default:
System.out.println("输入有误,请重新输入:");
break;
}
}while(flag);
}
}
需求10:九九乘法表
public class table{
public static void main(String[] args){
for(int i=1; i<=9; i++){
for(int j=1; j<=i; j++){
System.out.print(j+"x"+i+"="+(i*j)+"\t");
}
System.out.println();
}
System.out.println();
for(int n=1; n<=9; n++){
for(int m=1; m<=9; m++){
if(m>=n){
System.out.print(n+"x"+m+"="+(m*n)+"\t");
}else{
System.out.print("\t");
}
}
System.out.println();
}
}
}
万年历:
import java.util.Scanner;
public class wnl{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入年份:");
int year = scan.nextInt();
System.out.println("请输入月份:");
int month = scan.nextInt();
//计算开始年份到输入年份的总天数
int allDayOfYear = 0;
for(int i=1990; i<year; i++){
if((i%4==0 && i%100!=0)|| (i%400==0)){
allDayOfYear += 366;
}else{
allDayOfYear += 365;
}
}
//计算1月到输入月的总天数
int allDayOfMonth = 0;
for(int i=1; i<month; i++){
switch(i){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
allDayOfMonth += 31;
break;
case 4: case 6: case 9: case 11:
allDayOfMonth += 30;
break;
case 2:
if((i%4==0 && i%100!=0)|| (i%400==0)){
allDayOfMonth += 29;
}else{
allDayOfMonth += 28;
}
break;
}
}
//计算总天数
int allDay = allDayOfYear + allDayOfMonth + 1;
//计算星期
int week = allDay%7;
if(allDay%7 == 0){
week = 7;
}
//计算当月的天数
int day = 0;
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day += 31;
break;
case 4: case 6: case 9: case 11:
day += 30;
break;
case 2:
if((year%4==0 && year%100!=0)|| (year%400==0)){
day += 29;
}else{
day += 28;
}
break;
}
//打印日历
System.out.println("---" + year + "年" + month + "月---"+ "\n");
System.out.println("一\t二\t三\t四\t五\t六\t日\t");
//打印日期前的空格
int kong = 0;
for(int i=1; i<week; i++){
kong++;
System.out.print("\t");
}
//打印日期的空格
for(int i=1; i<=day; i++){
kong++;
System.out.print(i+"\t");
if(kong%7 == 0){
System.out.println("\t");
}
}
}
}