java - 如何添加奖金?

标签 java file

我做了一个代码,里面读取了一个文件,里面有一些员工,薪水,以及他们的排名,根据他们的排名,我们如何将奖金百分比加到他们的薪水上......

 String phrases;  
 int salary=0;
 try {
    FileReader in = new FileReader("bonus.txt"); 
    BufferedReader readFile = new BufferedReader(in);   
    while ((phrases = readFile.readLine()) != null) {   
       System.out.println(phrases);
       double bonus;

       if(phrases.contains("1")){
          bonus=salary/0.03;
          System.out.println("Bonus: " + bonus);
       }else if(phrases.contains("2")){
          bonus=salary/0.08;
          System.out.println("Bonus: " + bonus);
       }else if(phrases.contains("3")){
          bonus=salary/0.20;
          System.out.println("Bonus: " + bonus);
       }

       // System.out.println();
    }
    readFile.close();
    in.close(); 
 }catch (IOException e) {        
    System.out.println("Problem reading file.");
    System.err.println("IOException: " + e.getMessage());
 } 

输出:

 Jame   900000  1
 Bonus: 0.0
 Jane   60000   2
 Bonus: 0.0
 Don    866000  3
 Bonus: 0.0

我不知道为什么

最佳答案

如果您有如下所示的 employeeBonus.txt 文件。

Jame    900000  2
Jane    60000   1
Don     866000  3

我认为您将拥有三个标记作为字符串,因此您可以使用 stringtokenizer 类来获得薪水和等级。

文件第一行是

Jame    900000  2

编码字符串的结果是

Jame%20%20%20%20900000%092

我终于发现文本文件的内容通过 URL 编码混合了空格和制表符。

那么,这个类型的用法如下,

StringTokenizer stTok = new StringTokenizer(phrase, " \t");

它从第三个和第二个 token 中获取薪水和奖金值(value)的标识符。

name = stTok.nextToken(); //first token
salary = Integer.valueOf(stTok.nextToken()).intValue(); //second token
grade = stTok.nextToken();

[源代码]

package com.tobee.tests.inout;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class CheckBounsFromFile {
    public static void main(String[] args) {
        String name, phrase, grade;
        double bonus = 0;
        int salary = 0;
        BufferedReader readFile = null;
        try {
            readFile = new BufferedReader(new FileReader("resource/aa/employeeBonus.txt"));

            while ((phrase = readFile.readLine()) != null) {
                //System.out.println(phrase);

                StringTokenizer stTok = new StringTokenizer(phrase, " \t");

                name = stTok.nextToken();

                salary = Integer.valueOf(stTok.nextToken()).intValue();

                grade = stTok.nextToken();

                if(grade!= null && !grade.equals(""))
                {
                    if (grade.equals("1")) {
                        bonus = salary / 0.03;
                    } else if (grade.equals("2")) {
                        bonus = salary / 0.08;

                    } else if (grade.equals("3")) {
                        bonus = salary / 0.20;
                    }

                    System.out.printf("name[%s]salary[%d]Bonus[%f] \n",name, salary, bonus);
                }

            }

        } catch (IOException e) {
            System.out.println("Problem reading file.");
            System.err.println("IOException: " + e.getMessage());
        }
        finally
        {
            try {
                readFile.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

[结果]

name[Jame]salary[900000]Bonus[30000000.000000] 
name[Jane]salary[60000]Bonus[750000.000000] 
name[Don]salary[866000]Bonus[4330000.000000] 

祝你有美好的一天。

关于java - 如何添加奖金?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50383176/

相关文章:

java - MTOM 不以 block 的形式写入数据

javascript - 如何使用 JavaScript 在没有提交按钮的情况下提交 "file"输入?

file - 什么是 .meta 文件,为什么 Unity 会为我的所有 Assets 创建它们?

c# - os.access 在 IronPython 中为 os.F_OK 模式返回无用值

java - Keymap、InputMap、ActionMap、KeyListener — 选择重载

java - 越界了,为什么?

java - 构造函数中 "this"对象和 CurrentThread 之间

java - 如何在 Java 的 ExecutorService 中检索和处理异常

java - Android Junit 测试卡在 "Launching: Creating source locator..."

android - 如何在 Android 中使用 AES 加密 SD 卡中的文件?