Javafree
Página Inicial do Fórum > Java Básico

código java não aceita numeros acima de 100



Criar novo tópico   Responder tópico


  1. lucas kania
    Posts:3


    Comment Arrow

    Publicado em: 25/07/2016 23:50:19

    Olá eu sou novo na linguagem java eu estou fazendo um programa simples que calcula o imc mais tive um problrma quando executava o codigo. Na janela eu colocava os numeros 80 de peso e 1.80 de altura mais o codigo não acietava numeros maiores que 100 o que eu faço? CODIGO DO PROGRAMA ABAIXO
    DIGO D

    private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {
    int p = Integer.parseInt(txtGo.getText());
    int a = Integer.parseInt(txtTam.getText());
    int s = p / a;
    lblSo.setText(s);
    }



  1. staroski
    Posts:4599


    Comment Arrow

    Publicado em: 26/07/2016 10:20:17

    Posta o código completo da sua classe pois no código acima a unica coisa que talvez esteja errada é você usar números inteiros, mesmo assim, não há nada no código acima que limite os números até 100
    _________________

    Ricardo Artur Staroski, OCP-JP
    Blumenau - SC
    Faço seu trabalho Java,
    clique aqui

    Acesse http://www.guj.com.br/



  1. lucas kania
    Posts:3


    Comment Arrow

    Publicado em: 26/07/2016 10:49:29

    /*
    * 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.
    */
    package com.Imc.java;

    /**
    *
    * @author lucas kania
    */
    public class TrabalhoImc extends javax.swing.JFrame {

    /**
    * Creates new form TrabalhoImc
    */
    public TrabalhoImc() {
    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() {

    txtPeso = new javax.swing.JSpinner();
    txtAlt = new javax.swing.JSpinner();
    lblRes = new javax.swing.JLabel();
    lblS = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    btnCalc = new javax.swing.JButton();
    jLabel3 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    txtGo = new javax.swing.JTextField();
    txtTam = new javax.swing.JTextField();
    lblSo = new javax.swing.JLabel();

    txtPeso.setModel(new javax.swing.SpinnerNumberModel(0, null, 1500, 1));
    txtPeso.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

    txtAlt.setModel(new javax.swing.SpinnerNumberModel(0, null, 1500, 1));
    txtAlt.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

    lblRes.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    lblRes.setForeground(new java.awt.Color(255, 0, 0));
    lblRes.setText("0";

    lblS.setText("0";

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(204, 204, 204));

    jLabel1.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(153, 153, 153));
    jLabel1.setText("Peso";

    jLabel2.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
    jLabel2.setForeground(new java.awt.Color(153, 153, 153));
    jLabel2.setText("Altura";

    btnCalc.setFont(new java.awt.Font("Verdana", 3, 1); // NOI18N
    btnCalc.setForeground(new java.awt.Color(51, 51, 51));
    btnCalc.setText("Calcular";
    btnCalc.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    btnCalcActionPerformed(evt);
    }
    });

    jLabel3.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
    jLabel3.setForeground(new java.awt.Color(153, 153, 153));
    jLabel3.setText("Resultado";

    jLabel5.setText("jLabel5";

    lblSo.setText("0";

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel1)
    .addGap(18, 18, 1
    .addComponent(txtGo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(btnCalc)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jLabel5)
    .addGap(100, 100, 100))
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel2)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    .addComponent(txtTam, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel3)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    .addComponent(lblSo, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(0, 276, Short.MAX_VALUE))))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(21, 21, 21)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel1)
    .addComponent(btnCalc)
    .addComponent(txtGo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel5)))
    .addGap(18, 18, 1
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel2)
    .addComponent(txtTam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(43, 43, 43)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel3)
    .addComponent(lblSo))
    .addGap(40, 40, 40)
    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
    .addContainerGap())
    );

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

    private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    int p = Integer.parseInt(txtGo.getText());
    int a = Integer.parseInt(txtTam.getText());
    int s = p / a;
    lblSo.setText(s);

    }

    /**
    * @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://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
    */
    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(TrabalhoImc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(TrabalhoImc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(TrabalhoImc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(TrabalhoImc.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 TrabalhoImc().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnCalc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel lblRes;
    private javax.swing.JLabel lblS;
    private javax.swing.JLabel lblSo;
    private javax.swing.JSpinner txtAlt;
    private javax.swing.JTextField txtGo;
    private javax.swing.JSpinner txtPeso;
    private javax.swing.JTextField txtTam;
    // End of variables declaration
    }



  1. staroski
    Posts:4599


    Comment Arrow

    Publicado em: 26/07/2016 13:16:50

    O único problema do seu código é um erro de compilação na linha abaixo:


    Fora isso, ele executa normalmente e permite digitar números maiores que 100.
    _________________

    Ricardo Artur Staroski, OCP-JP
    Blumenau - SC
    Faço seu trabalho Java,
    clique aqui

    Acesse http://www.guj.com.br/



  1. Relacionados