mercredi 15 avril 2020

How to make the if statement be repeated to specific array not all the program

The program works fine if all the data is entered correctly. but as you see below i made 2 if statements for the hours to be between 0 and 60 and rate to be between 0 and 50, when i make the hours or the rate outside their range the program simply exits the for loop and restarts from the while loop.

i made that program before without using the arrays and the program used to return to the specific if statement that have the wrong entered value and the older values for every thing was still saved and can be used in the final process without any mistake.

see here in the comments for the working code which i did not use arrays method

import java.util.Scanner;
public class homework3_Arrays
{
 public static void main(String[] args)
 {
  int emp_num, flag=0;
  Scanner kb = new Scanner(System.in);
  while(flag == 0)
  {
  System.out.printf("Please enter employees number\n");
   String[]emp1 = new String[emp_num];
   String[]emp2 = new String[emp_num];
   String[]emp3 = new String[emp_num];
   double[]hours = new double[emp_num];
   double[]rate = new double[emp_num];
   double[]gross = new double[emp_num];
   double[]overtime = new double[emp_num];
   double[]STtax = new double[emp_num];
   double[]FEDtax = new double[emp_num];
   double[]union = new double[emp_num];
   double[]net = new double[emp_num];
   double[]Tgross = new double[emp_num];
   double[]Agross = new double[emp_num];

   for(int i = 0; i < emp_num; i++)
  {


     System.out.printf("please enter the employee first name\n ");
     emp1[i] = kb.next();
     System.out.printf("please enter the employee middle initial\n ");
     emp2[i] = kb.next();
     System.out.printf("please enter the employee last name\n ");
     emp3[i] = kb.next();
     System.out.printf("please enter worked hours\n ");
     hours[i] = kb.nextDouble();
     if(hours[i] >= 0 && hours[i] <= 60)
     {
      System.out.printf("please enter the pay rate\n ");
      rate[i] = kb.nextDouble();
      if(rate[i] > 0 && rate[i] <= 50)
      {
       if(hours[i] <= 40)
        overtime[i] = 0;
        else
         overtime[i] = (hours[i] - 40) * rate[i] * 1.5;

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

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

Aucun commentaire:

Enregistrer un commentaire