Home > database >  how can I add grades in each subject Then divide into 4 subject then print the average in GUI Swing
how can I add grades in each subject Then divide into 4 subject then print the average in GUI Swing

Time:07-05

how can I add grades in each subject then print the average in GUI Swing java``

 private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       
  double grades;
 grades= Double.parseDouble(comprogGrds .getText()  dsGrds .getText()  cwGrds.getText()  multGrds.getText())/4;
 grades= Double.parseDouble(comprogGrds.getText());
 grades= Double.parseDouble(dsGrds.getText());
 grades= Double.parseDouble(cwGrds.getText());
 grades= Double.parseDouble(multGrds.getText());
        
        if(grades>=90){
            average.setText(Double.toString(grades));
        }else if(grades>=78 && grades<=79){
          average.setText(Double.toString(grades));
        }else if(grades>=74 && grades <=75){
           average.setText(Double.toString(grades));
        }
                
    }       

CodePudding user response:

You just need to parse all your string grade, and do the calculation

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           
      double grades;
     grades= Double.parseDouble(comprogGrds.getText())  Double.parseDouble(dsGrds.getText())  Double.parseDouble(cwGrds.getText())  Double.parseDouble(multGrds.getText()))/4;
    
            
            if(grades>=90){
                average.setText(Double.toString(grades));
            }else if(grades>=78 && grades<=79){
              average.setText(Double.toString(grades));
            }else if(grades>=74 && grades <=75){
               average.setText(Double.toString(grades));
            }
                    
        } 

CodePudding user response:

If you are using java 1.8 or higher, then this is quite simple:

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       
 double grades = Arrays
            .stream(new double[] { comprogGrds.getText(), dsGrds.getText(), cwGrds.getText(), multGrds.getText() })
            .average().getAsDouble();    

        if(grades>=90){
            average.setText(Double.toString(grades));
        }else if(grades>=78 && grades<=79){
          average.setText(Double.toString(grades));
        }else if(grades>=74 && grades <=75){
           average.setText(Double.toString(grades));
        }
                
    }     
  • Related