如何使用java增加2%,5%等工资 [英] how to increment salary by 2%, 5%, etc. using java

查看:290
本文介绍了如何使用java增加2%,5%等工资的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何构建一个程序

将要求用户输入他们的工资和你的程序可以

计算工资增量

将根据下表获得。

使用if ---- else选择控制语句。



工资增量



N 800 - N1000 2%



N 101 - N2000 5%



N 201 - N4000 8%



N 400> 10%

我试过这段代码,但我想知道我们是否正确。谢谢



How to construct a program
that will require the user to input
their salary and your program can
calculate the increment in salary that
will obtain based on the table below.
Using if----else selection control statement.

Salary Increment

N 800 - N1000 2%

N 101 - N2000 5%

N 201 - N4000 8%

N 400> 10%
i have tried this code but i wants to know weither i am correct or not. Thanks

import java.util.Scanner;
public class Question2
{
 public static void main(String [] args)throws IOException
 {
  float salary;
  double increment;
 Scanner scanner = new Scanner(new InputStreamReader(System.in));


  System.out.print("Enter Your Salary:");

  salary = scanner.nextFloat();

  if ((salary >=800) && (salary <=1000))

  {
   increment=(2 / salary)*100;
   System.out.println("\nPercentage increment is: " + increment + "\n");
  }
  else if ((salary >=101) && (salary <=2000))
  {
   increment=(5 / salary)*100;
   System.out.println("\nPercentage increment is: " + increment + "\n");
  }
  else if ((salary >=201) && (salary <=4000))
  {
   increment=(8 / salary)*100;
   System.out.println("\nPercentage increment is: " + increment + "\n");
  }
  else if (salary > 400 )
  {
   increment=(10 / salary)*100;
   System.out.println("\nPercentage increment is: " + increment + "\n");
  }
  else
  System.out.println("\nNo More Increment For This Salary\n");

    }
}

推荐答案

您应该检查代码背后的逻辑。你是否意识到薪资范围重叠?

正如 Sudhakar Shinde 已经指出的那样, 900 的薪水将满足您的所有条件(由于否则,如果链,只会执行第一个条件的声明。
You should check the logic behind your code. Do you realize salary ranges overlap?
As Sudhakar Shinde already pointed out, a salary of 900 would satisfy all your conditions (while due to the else if chain, only first condition''s statement would be executed.


这篇关于如何使用java增加2%,5%等工资的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆