samedi 3 décembre 2016

Display the user inputed number of random lengths of sides of a triangle

Hi guys i'm trying to write a program to calculate user imputed amount of triangles side lengths and my code will do one but not the imputed from user amount of triangles .Any ideas where i am going wrong ?The end part of the code is that i have to classify the sides as scalene etc.. but haven't got that far yet .By the way this is using a GUI .Any help would be awesome as i have been trying to figure it out for days.

import java.util.Random;

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

/** * * @author */ public classGui extends javax.swing.JFrame {

Random rand= new Random();
         int min=3 ;
         int max=10;
         int longest,shorter1,shorter2;
         int i;
         int input1;



/**
 * Creates new form U3A3_gerald_smithGui
 */
public U3A3_gerald_smithGui() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    titleLabel = new javax.swing.JLabel();
    questionLabel = new javax.swing.JLabel();
    userInput = new javax.swing.JTextField();
    calculateButton = new javax.swing.JButton();
    jScrollPane2 = new javax.swing.JScrollPane();
    displayText2 = new javax.swing.JTextArea();
    jLabel1 = new javax.swing.JLabel();
    goButton = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    displayText1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    titleLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    titleLabel.setText("Triangles");

    questionLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    questionLabel.setText("How many triangles would you like?");

    userInput.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(102, 255, 204)));
    userInput.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            userInputActionPerformed(evt);
        }
    });

    calculateButton.setText("Calculate");

    displayText2.setColumns(20);
    displayText2.setRows(5);
    displayText2.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(102, 255, 204)));
    jScrollPane2.setViewportView(displayText2);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel1.setText("What kind of triangles are they? ");

    goButton.setText("Go");
    goButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            goButtonActionPerformed(evt);
        }
    });

    displayText1.setColumns(20);
    displayText1.setRows(5);
    jScrollPane1.setViewportView(displayText1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 80, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(calculateButton)
                            .addGap(102, 102, 102))))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(28, 28, 28)))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(146, 146, 146)
                    .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(76, 76, 76)
                    .addComponent(questionLabel))
                .addGroup(layout.createSequentialGroup()
                    .addGap(156, 156, 156)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(goButton)
                        .addComponent(userInput, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(questionLabel)
            .addGap(22, 22, 22)
            .addComponent(userInput, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(105, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addComponent(goButton)
                    .addGap(37, 37, 37)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(calculateButton)
                    .addGap(41, 41, 41)
                    .addComponent(jLabel1)
                    .addGap(42, 42, 42))))
    );

    pack();
}// </editor-fold>                        

private void userInputActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
}                                         

         @SuppressWarnings("empty-statement")
private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
      String input = (String)userInput.getText();
      int input1 = Integer.parseInt(input);
      int side1=rand.nextInt(max-min+1)+min;
      int side2=rand.nextInt(max-min+1)+min;
      int side3=rand.nextInt(max-min+1)+min;
      String outputString="The sides are,"+"\n";
      for(int i=1;i<=input1;i++);{
      outputString=outputString+side1+","+side2+","+side3;
}

        displayText1.setText(outputString);



     if (side1>=side2&&side1>=side3){
          longest=side1;
          shorter1=side2;
          shorter2=side3;
     }else if(side2>=side3&&side2>=side1){
          longest=side2;
          shorter1=side1;
          shorter2=side3;
      } else{
          longest=side3;
          shorter1=side1;
          shorter2=side2;

      }












}                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://ift.tt/1cNmMj1 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(U3A3_gerald_smithGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(U3A3_gerald_smithGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(U3A3_gerald_smithGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(U3A3_gerald_smithGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new U3A3_gerald_smithGui().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton calculateButton;
private javax.swing.JTextArea displayText1;
private javax.swing.JTextArea displayText2;
private javax.swing.JButton goButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JLabel questionLabel;
private javax.swing.JLabel titleLabel;
private javax.swing.JTextField userInput;
// End of variables declaration                   

}




Aucun commentaire:

Enregistrer un commentaire