Numeros primos

1 resposta
P

Pessoal não consigo achar o erro existente aqui…
Na verdade ele não dá erro… ele apenas não diz se o numero é primo ou não se for abaixo de dex
eis o código:

import java.io.BufferedReader;

import javax.swing.JOptionPane;

/*
  • Primos.java
  • Created on 15 de Novembro de 2006, 15:26
    */

/**
*

  • @author User
    */
    public class Primos extends javax.swing.JFrame {

    /** Creates new form Primos */
    
    public Primos() {
    
    initComponents();
    
    }
    
    static boolean result;
    
    static BufferedReader num;
    
    static int x;
    
    static String a;
    

    /** 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.
      
      */
      
      // <editor-fold defaultstate=“collapsed” desc=" Generated Code ">
      
      private void initComponents() {
      
      tfnum = new javax.swing.JTextField();
      
      jLabel1 = new javax.swing.JLabel();
      
      jButton1 = new javax.swing.JButton();
      

      getContentPane().setLayout(null);

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      
      setTitle(N\u00fameros Primos);
      
      getContentPane().add(tfnum);
      
      tfnum.setBounds(50, 40, 110, 19);
      
      jLabel1.setText(Digite um n\u00famero:);
      
      getContentPane().add(jLabel1);
      
      jLabel1.setBounds(50, 20, 120, 15);
      
      jButton1.setText(Calcular);
      
      jButton1.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      jButton1ActionPerformed(evt);
      
      }
      
      });
      

      getContentPane().add(jButton1);
      jButton1.setBounds(60, 70, 90, 25);

      java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
      
      setBounds((screenSize.width-224)/2, (screenSize.height-155)/2, 224, 155);
      
      }// </editor-fold>
      
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    int num, result;
    
    num = Integer.parseInt(tfnum.getText());
    
    result = num%2;
    
    if (result == 0){
    
    JOptionPane.showMessageDialog(null, “Este número não é primo”);
    
    }else {
    
    int raiz = (int)Math.sqrt(num) + 1;
    
    for(int i=3;i<raiz;i+=2) {
    
    if(num % i == 0) {
    
    break;
    
    }
    
    JOptionPane.showMessageDialog(null, Este Número é primo);
    
    }
    
    }
    
    }
    

    private int resutl;

    /**

    • @param args the command line arguments
      */
      public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      new Primos().setVisible(true);
      }
      });
      }
    private String getText(String string) {
    
    return null;
    
    }
    
    // Variables declaration - do not modify
    
    private javax.swing.JButton jButton1;
    
    private javax.swing.JLabel jLabel1;
    
    private javax.swing.JTextField tfnum;
    
    // End of variables declaration
    

}

Alguem poderia me ajudar?
Obrigado

1 Resposta

M

vc colocou a mensagem de quando o numero é primo detro do for então ele da o break e sai fora do for deixando de mostrar o JOptionPane.

private void jButton1ActionPerformed&#40;java.awt.event.ActionEvent evt&#41; &#123; // TODO add your handling code here&#58; int num, result; num = Integer.parseInt&#40;tfnum.getText&#40;&#41;&#41;; result = num%2; if &#40;result == 0&#41;&#123; JOptionPane.showMessageDialog&#40;null, &quot;Este número não é primo&quot;&#41;; &#125; else &#123; int raiz = &#40;int&#41;Math.sqrt&#40;num&#41; + 1; for&#40;int i=3;i&lt;raiz;i+=2&#41; &#123; if&#40;num % i == 0&#41; &#123; break; &#125; &#125; JOptionPane.showMessageDialog&#40;null, &quot;Este Número é primo&quot;&#41;; &#125; &#125;

Criado 16 de novembro de 2006
Ultima resposta 17 de nov. de 2006
Respostas 1
Participantes 2