【问题标题】:how to show all the output如何显示所有输出
【发布时间】:2020-03-26 04:11:16
【问题描述】:

我想让该程序计算 10 名员工的平均总数。

我应该对代码进行哪些编辑,以便保存我在 while 循环中所做的所有 10 个输入。

程序只显示一个输出,然后进入循环,但我希望它显示 10 名员工的 10 个输出,以便我可以计算平均毛和总毛。

import java.util.Scanner;
public class ss 
{
 public static void main(String[] args)
 {
  String emp;
  char repeat = 'y';
  double hours = 0, rate = 0, gross = 0, overtime = 0,STtax = 0, FEDtax = 0, union = 0, net = 0, Tgross = 0, Agross = 0;
  int count = 0;
  Scanner kb = new Scanner(System.in);

  while(repeat == 'y' || repeat == 'Y')
   {
    System.out.printf("please enter the employee name\n ");
    emp = kb.next();
    System.out.printf("please enter worked hours\n ");
    hours = kb.nextDouble();
    if(hours >= 0 && hours <= 60)
    {
     System.out.printf("please enter the pay rate\n ");
     rate = kb.nextDouble();
     if(rate > 0 && rate <= 50)
     {
      if(hours <= 40)
       {
        gross = (rate * hours);
        STtax = gross * 0.06;
        FEDtax = gross * 0.12;
        union = gross * 0.01;
        net = gross - (STtax + FEDtax + union);

        System.out.printf("hi %s\n", emp);
        System.out.printf("worked hours %6.2f\n", hours);
        System.out.printf("your pay rate %2.4f\n", rate);
        System.out.printf("Gross income %6.2f\n", gross);
        System.out.printf("State tax %6.2f\n", STtax);
        System.out.printf("Federal tax %6.2f\n", FEDtax);
        System.out.printf("Union fees %6.2f\n", union);
        System.out.printf("NET %6.2f\n", net);
       }
        else
         {
          overtime = (hours - 40) * rate * 1.5;
          gross = (rate * 40) + overtime;
          STtax = gross * 0.06;
          FEDtax = gross * 0.12;
          union = gross * 0.01;
          net = gross - (STtax + FEDtax + union);

          System.out.printf("hi %s\n", emp);
          System.out.printf("worked hours %6.2f\n", hours);
          System.out.printf("your pay rate %2.4f\n", rate);
          System.out.printf("OverTime is %2.2f\n", overtime);
          System.out.printf("Gross income %6.2f\n", gross);
          System.out.printf("State tax %6.2f\n", STtax);
          System.out.printf("Federal tax %6.2f\n", FEDtax);
          System.out.printf("Union fees %6.2f\n", union);
          System.out.printf("NET %6.2f\n", net);

         }   
      System.out.printf("please enter Y to add another employee\n ");
      repeat = kb.next().charAt(0);
     } 
      else
       System.out.printf("Pay Rate can only be between 0 and 50\n");
    }
     else
      System.out.printf("Hours can only be between 0 and 60\n");

   }  
 }
}

【问题讨论】:

  • 问题是什么?如果您希望我们为您提供代码,抱歉 Stackoverflow 位置不正确。stackoverflow.com/help/asking 请查看帮助。
  • 这里的问题是什么?这似乎是你的功课。将您的问题编辑为问题。输入你使用的代码,并清楚地说明你遇到了什么问题。
  • Stackoverflow 不是完成作业的地方。也展示你的一些努力。
  • 我明白你的意思,很抱歉我忘记添加代码,我编辑了帖子并添加了我的代码感谢@KarthikeyanVaithilingam 的澄清

标签: java


【解决方案1】:

我能够解决它,如果有人想要,这里是解决方案

import java.util.Scanner;
public class HomeWork3
{
 public static void main(String[] args)
 {
  String emp;
  double hours = 0, rate = 0, gross = 0, overtime = 0,STtax = 0, FEDtax = 0, union = 0, net = 0, Tgross = 0, Agross = 0;
  int num = 0;
  Scanner kb = new Scanner(System.in);

  while(num < 10)
   {
    System.out.printf("please enter the employee name\n ");
    emp = kb.next();
    System.out.printf("please enter worked hours\n ");
    hours = kb.nextDouble();
    if(hours >= 0 && hours <= 60)
    {
     System.out.printf("please enter the pay rate\n ");
     rate = kb.nextDouble();
     if(rate > 0 && rate <= 50)
     {
      if(hours <= 40)
       overtime = 0;
       else
        overtime = (hours - 40) * rate * 1.5;

       gross = (rate * 40) + overtime;
       STtax = gross * 0.06;
       FEDtax = gross * 0.12;
       union = gross * 0.01;
       net = gross - (STtax + FEDtax + union);
       System.out.printf("hi %s\n", emp);
       System.out.printf("worked hours %6.2f\n", hours);
       System.out.printf("your pay rate $%2.4f\n", rate);
       System.out.printf("OverTime is %2.2f\n", overtime);
       System.out.printf("Gross income $%6.2f\n", gross);
       System.out.printf("State tax $%6.2f\n", STtax);
       System.out.printf("Federal tax $%6.2f\n", FEDtax);
       System.out.printf("Union fees $%6.2f\n", union);
       System.out.printf("NET $%6.2f\n", net);


      num = num +1; 
     } 
      else
       System.out.printf("Pay Rate can only be between 0 and 50\n");
     Tgross = Tgross + gross;
     Agross = Tgross / num;
     System.out.printf("Total gross for all entered employees is $%6.3f\n", Tgross);
     System.out.printf("Average gross for all entered employees is $%6.4f\n", Agross);
     System.out.printf("number of entered employees is %d\n\n", num);  
    }
     else
      System.out.printf("Hours can only be between 0 and 60\n");
   }  
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2015-01-05
    • 2021-04-30
    • 2021-08-23
    • 2016-05-03
    • 1970-01-01
    • 2022-06-30
    相关资源
    最近更新 更多