Java基础(三)(代码实现)

Java基础(三)(代码实现),第1张

Java基础(三)(代码实现) 一.年薪计算器

(1)通过键盘输入用户的月薪,每年是几个月的薪水

(2)输出用户的年薪

(3)输出一行字“如果年薪超过十万,恭喜你超过90%的国人”,“如果年薪超过20万,恭喜你超过98%的国人”

(4)直到键盘输入“break”退出程序

(5)输入中途,键盘输入“next”,则这个用户退出计算不显示恭喜,算下一个用户的1年薪

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        System.out.println("我的年薪计算器");
        Scanner sc = new Scanner(System.in);
        int yearsalary;
        while (true) {
            System.out.println("请输入月薪:");
            int monthsalary = sc.nextInt();
            System.out.println("请输入一年多少个月资薪:");
            int month = sc.nextInt();
            yearsalary = monthsalary * month;
            String command = sc.nextLine();
            if ("exit".equals(command)) {
                System.out.println("退出软件");
                break;
            }
            if ("next".equals(command)) {
                System.out.println("请重新输入新的数据:");
                continue;
            }
            if (yearsalary > 100000 && yearsalary < 200000)
                System.out.println("恭喜你,超过了90%的中国人!");
            if (yearsalary > 200000)
                System.out.println("恭喜你超过了98%的中国人!");
            System.out.println("年薪是:" + yearsalary);
        }

    }
}

运行结果: 

结果发现在输完月薪和月份后没有让输出“exit”或者“next”或者其他,就直接执行下面代码

原因:“nextline”为输入下面一行字符串,我们在输完月份时输回车键,则“nextline”的内容就变成了空字符串,自动执行下面代码

解决方法:1.在输入字符串代码前加一行"sc.nexlline();"。

2.用“next()”取代“nextline()”。

接下来可以正常运行

 二.处理迟到问题

(1)输入参数:员工名称,月薪

(2)处理逻辑:迟到1-10分钟,警告;

                           迟到11-20分钟,罚款100元;

                            迟到21-30分钟,罚款200元;

                            迟到30分钟以上,扣除半日工资;

                            迟到1小时以上,按旷工计算,扣除三日工资。

(3)输出罚款金额。

import java.util.Scanner;

public class test {
    public static int late(int later){
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        int fakuan=0;
        int monthsalary=sc.nextInt();
        if(later<10)
            System.out.println("警告一次");
        else if(later<20)
            fakuan=100;
        else if(later<30)
            fakuan=200;
        else if(later<60)
            fakuan=monthsalary/(30*2);
        else
            fakuan=(monthsalary/30)*3;
        System.out.println("罚款金额为:"+fakuan);
        return fakuan;
    }
   public static void main(String[] args){
        late(25);
       }
   }
三.方法的重载

方法的重载是指一个类中可以定义多个方法名相同,但参数不同的方法。调用时会根据不同的参数自动匹配对应的方法。

public class test {
    public static int add(int a,int b){
       int c=a+b;
       System.out.println(c);
       return c;
    }
    public static double add(double x,double y,double z){
        double m=x+y+z;
        System.out.println(m);
        return m;
    }
   public static void main(String[] args){
        add(1,2);
        add(1.2,2.2,3.3);
       }
   }

 当作用相似时,可以用相同的方法名

但是变量名不能相同

public class test {
   public static void main(String[] args){
        int c=20;
        int c=1;
        System.out.println(c);
       }
   }

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5719404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存