我已经做了一个代码,它读取一个文件包含了一些雇员,工资,和他们的排名,根据他们的排名,我们怎样才能把奖金百分比加到他们的工资中…
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我不知道为什么
发布于 2018-05-17 04:44:07
如果您有如下所示的employeeBonus.txt文件。
Jame 900000 2
Jane 60000 1
Don 866000 3我认为您将有三个令牌作为字符串,因此,您可以使用字符串标记器类来获得薪水和等级。
在文件的第一行是
Jame 900000 2编码字符串的结果是
Jame%20%20%20%20900000%092我终于发现,通过URL编码,文本文件的内容与空格和选项卡字符混合在一起。
因此,这种类型的用法如下,
StringTokenizer stTok = new StringTokenizer(phrase, " \t");它从第三和第二令牌获取工资和奖金值的标识符。
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] 祝您今天愉快。
发布于 2018-05-17 04:29:15
使用等于方法检查条件,但是短语变量包含不同的值,而不是1、2、3,这就是为什么得到奖金0的原因。
if(phrases.contains("1")){
bonus=salary/0.03;
}else if(phrases.contains("2")){
bonus=salary/0.08;
}else if(phrases.contains("3")){
bonus=salary/0.20;
}或者您可以使用以下方法获得最后一个参数:
phrases.substring(phrases.length()-1, phrases.length())您可以使用contains或拆分方法获得第三个参数。请查看本教程:split.htm
,还有一件事你的薪水总是零(0)。请更正一下
我已经在这里贴出了完整的代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class SubClass{
public static void main(String[] args) {
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);
phrases = phrases.trim().replaceAll("[ ]{2,}", " ");
String splitStr [] = phrases.split(" ");
double bonus;
salary = Integer.parseInt(splitStr[1]);
if(splitStr[2].contains("1")){
bonus=salary/0.03;
System.out.println("Bonus: " + bonus);
}else if(splitStr[2].contains("2")){
bonus=salary/0.08;
System.out.println("Bonus: " + bonus);
}else if(splitStr[2].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());
}
}
}发布于 2018-05-17 04:43:22
其他答案似乎不符合以下事实:您的salary变量始终是0,因此,您的bonus计算(依赖于薪资值的)将始终为0。
假设这个:Jame 900000 1是文本文件中的一个示例行,那么您的代码就会出现各种问题。
(phrases.equals("1")。如果短语将等于您正在处理的当前行中的文本:Jame 900000 1,则此语句(以及其他两条语句相同)将永远不会返回true,因此将永远不会计算奖金。salary值。您需要替换以下内容:
while ((phrases = readFile.readLine()) != null) {
System.out.println(phrases);
if(phrases.equals("1")){就像这样:
while ((phrases = readFile.readLine()) != null) {
System.out.println(phrases);
String[] employeeData = phrases.split("\\t"); //This assumes that your data is split by tabs.
salary = Double.parse(employeeData[1]);
if("1".equals(employeeData[2])) {
bonus = salary * 0.03;
}
...https://stackoverflow.com/questions/50383176
复制相似问题