Apresentar todos os números divisíveis por 4 que sejam maiores do que 0 e menores que 200. Caso o número não seja divisível, informe uma mensagem apropriada

12 respostas
L
Tenho esse código mas n consigo finalizar ele, alguém poderia me dar uma mão? sou iniciante e n estou conseguindo finalizar de acordo com o enunciado.  obg!

String total = “”;

int i;
for (i = 1; i < 200; i++) {
        if (i % 4 == 0) {
            total += i + ",";
        }
        System.out.println("O Resultado é : " + i);
    }
    
}

}

12 Respostas

Fefo80
System.out.println("O resultado é : " + total);
L

ok, obg por responder, mas eu teria outra duvida se vc poder responder. Como faço pra escrever a mensagem q o numero n é divisível?

staroski
System.out.println("O número não é divisível por 4");
RoinujNosde

Só complementando, vc coloca isso no else daquele if:

if (i % 4 == 0) {
    total += i + ",";
} else {
    //imprimir mensagem não divisível aqui
}
L

Só mais uma duvida se n for incomodar, deu certo mas agora ele esta dando a resposta certa e a que o numero n é divisível.
Como faço para imprimir a resposta q n é divisível quando realmente n for?
E desculpa por incomodar sou bem leigo sobre esse assunto.
Mas realmente n sei o que fazer . Espero retorno OBG!!!

int i;

String total = “”;

int valor = Entrada.leiaInt(" Digite um numero até 200 “);

for (i = 0; i < 200; i++) {

if (i % 4 == 0) {

total += i + “,”;

System.out.println( O resultado é : " + total);
} else {
            
            System.out.println("O número não é divisível por 4");
        }
       
    }

}

}

Fefo80

Copiar e colar os códigos apresentados não vai solucionar.

Você quer que a gente dê a questão pronta né?

De onde veio esse “valor” e o “leiaInt”?

Posta aqui o código completo da classe Entrada.

L

N só quero ajuda, eu criei esse valor e leiaInt para poder pedir um numero. esta errado?

Fefo80

Posta aqui o código da classe Entrada.

E use uma IDE ao invés do Word para escrever código.
:sweat_smile:

L

import javax.swing.*;

/**

  • Class Entrada - input class for input of simple input types
  • via simple dialog box.
  • eg. int, char, String, double or boolean.
  • @author Bruce Quig
  • @author Michael Kolling
  • @author Eugene Ageenko
  • @author Marcelo de G. Malheiros
  • @version 1.3
  • Modified (Aug 12, 2003): Portuguese version, added methods without parameters.
    */
public class Entrada {

// instance variables

static final String STRING_TITLE = Entre com uma string;

static final String CHAR_TITLE = Entre com um char;

static final String INT_TITLE = Entre com um int;

static final String BOOLEAN_TITLE = Selecione true ou false;

static final String DOUBLE_TITLE = Entre com um double;

static final String TRUE = true;

static final String FALSE = false;

static final String EMPTY_STRING = “”;
/**
 *  No constructor by default.
 */
private Entrada() {
}

/**
 ** String input from the user via a simple dialog.
 ** @return String input from the user.
 **/
public static String leiaString() {
    return leiaString("","");
}

/**
 ** String input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @return String input from the user.
 **/
public static String leiaString(String prompt) {
    return leiaString(prompt,"");
}

/**
 ** String input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @param initialValue input string that is initially displayed as selected by the user
 ** @return String input from the user.
 **/
public static String leiaString(String prompt, String initialValue) {
    Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
    Object[] options = {"OK"};

    boolean validResponse = false;

    String result = null;

    while (!validResponse) {
        final JOptionPane optionPane = new JOptionPane(commentArray,
                                                       JOptionPane.QUESTION_MESSAGE,
                                                       JOptionPane.OK_CANCEL_OPTION,
                                                       null,
                                                       options,
                                                       options[0]);

        optionPane.setWantsInput(true);
        optionPane.setInitialSelectionValue(initialValue);  // EA: added
        JDialog dialog = optionPane.createDialog(null, STRING_TITLE);

        dialog.pack();
        dialog.show();

        Object response = optionPane.getInputValue();

        if (response != JOptionPane.UNINITIALIZED_VALUE) {
            result = (String) response;
            if (result != null) // EA: added for completnes
                validResponse = true;
            else {
                commentArray[1] = "Entrada inv�lida: ";
                commentArray[2] = "Entre com uma string v�lida";
            }
        } else {
            commentArray[1] = "Precisa entrar com uma string";
            commentArray[2] = EMPTY_STRING;
        }
    }
    return result;
}

/**
 ** returns character input from the user via a simple dialog.
 ** @return the input character
 **/
public static char leiaChar() {
    return leiaChar("","");
}

/**
 ** returns character input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @return the input character
 **/
public static char leiaChar(String prompt) {
    return leiaChar(prompt,"");
}

/**
 ** returns character input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @param initialValue input value that is initially displayed as selected by the user
 ** @return the input character
 **/
public static char leiaChar(String prompt, char initialValue) {
    return leiaChar(prompt,Character.toString(initialValue));
}

/**
 ** returns character input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @param initialValue input string that is initially displayed as selected by the user
 ** @return the input character
 **/
public static char leiaChar(String prompt, String initialValue) {
    char response = (initialValue != null && initialValue.length() > 0) ? initialValue.charAt(0) : '-'; // EA: modified

    String result = null;

    Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
    Object[] options = {"OK"};

    boolean validResponse = false;

    while (!validResponse) {
        final JOptionPane optionPane = new JOptionPane(commentArray,
                                                       JOptionPane.QUESTION_MESSAGE,
                                                       JOptionPane.OK_CANCEL_OPTION,
                                                       null,
                                                       options,
                                                       options[0]);

        optionPane.setWantsInput(true);
        optionPane.setInitialSelectionValue(initialValue);  // EA: added
        JDialog dialog = optionPane.createDialog(null, CHAR_TITLE);

        dialog.pack();
        dialog.show();

        result = null; // EA: added for convinience;
        // EA: why character processed in another way that integer?
        // EA: meaning that with check for uinitialized case then assignment?

        Object input = optionPane.getInputValue();
        if (input != JOptionPane.UNINITIALIZED_VALUE) {
            result = (String) input;
            if (result != null) {
                if (result.length() == 1) {
                    response = result.charAt(0);
                    validResponse = true;
                } else {
                    commentArray[1] = "Entrada inv�lida: " + result;
                    commentArray[2] = "Entre com apenas um caracter";
                }
            } else {
                commentArray[1] = "Entrada inv�lida"; // EA: corrected, no point to print null-object. Question: when it is possible to have null objects?
                commentArray[2] = "Entre com apenas um caracter";
            }
        } else {
            commentArray[1] = "Precisa entrar com apenas um caracter";  //EA: error corrected, result removed
            commentArray[2] = EMPTY_STRING; //EA: cannot use result since it is not initialized
        }
    }
    return response;
}

/**
 ** boolean selection from the user via a simple dialog.
 ** @return boolean selection from the user
 **/
public static boolean leiaBoolean() {
    return leiaBoolean("", TRUE, FALSE);
}

/**
 ** boolean selection from the user via a simple dialog.
 ** @param  prompt message to appear in dialog
 ** @return boolean selection from the user
 **/
public static boolean leiaBoolean(String prompt) {
    return leiaBoolean(prompt, TRUE, FALSE);
}

/**
 ** boolean selection from the user via a simple dialog.
 ** @param  prompt message to appear in dialog
 ** @param  trueText message to appear on true "button"
 ** @param  falseText message to appear on "false" button
 ** @return boolean selection from the user
 **/
public static boolean leiaBoolean(String prompt, String trueText, String falseText) {
    Object[] commentArray = {prompt, EMPTY_STRING};
    boolean validResponse = false;
    int result = -1;

    while (!validResponse) {
        Object[] options = {trueText, falseText};
        result = JOptionPane.showOptionDialog(null,
                                              commentArray,
                                              BOOLEAN_TITLE,
                                              JOptionPane.YES_NO_OPTION,
                                              JOptionPane.QUESTION_MESSAGE,
                                              null, //don't use a custom Icon
                                              options, //the titles of buttons
                                              trueText); //the title of the default button, EA: CORRECTED from TRUE

        // check true or false buttons pressed
        if (result == JOptionPane.YES_OPTION || result == JOptionPane.NO_OPTION) // CORRECTED from 0:1
        {
            validResponse = true;
        } else {
            commentArray[1] = "Sele��o incorreta: escolha os bot�es true ou false";
        }
    }
    return (result == 0);
}

/**
 ** returns integer input from the user via a simple dialog.
 ** @return the input integer
 */
public static int leiaInt() {
    return leiaInt("","");
}

/**
 ** returns integer input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @return the input integer
 */
public static int leiaInt(String prompt) {
    return leiaInt(prompt,"");
}

/**
 ** returns integer input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @param initialValue input value that is initially displayed as selected by the user
 ** @return the input integer
 **/
public static int leiaInt(String prompt, int initialValue) {
    return leiaInt(prompt,Integer.toString(initialValue));
}

/**
 ** returns integer input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @param initialValue input string that is initially displayed as selected by the user
 ** @return the input integer
 **/
public static int leiaInt(String prompt, String initialValue) {
    Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};
    Object[] options = {"OK"};

    boolean validResponse = false;

    int response = 0;
    while (!validResponse) {
        final JOptionPane optionPane = new JOptionPane(commentArray,
                                                       JOptionPane.QUESTION_MESSAGE,
                                                       JOptionPane.OK_CANCEL_OPTION,
                                                       null,
                                                       options,
                                                       options[0]);

        optionPane.setWantsInput(true);
        optionPane.setInitialSelectionValue(initialValue);  // EA: added
        JDialog dialog = optionPane.createDialog(null, INT_TITLE);

        dialog.pack();
        dialog.show();

        // EA: rewritten as in leiaChar function
        // EA: added or corrected non-portable check for uninitialized value situation
        Object input = optionPane.getInputValue();
        if (input == JOptionPane.UNINITIALIZED_VALUE) {
            commentArray[1] = "Precisa entrar com um valor inteiro"; // EA: explanatory text added
            commentArray[2] = EMPTY_STRING;
        } else {
            String result = (String) input;
            if (result == null) { // EA: added for completnes, but is this situation possible?
                commentArray[1] = "Valor inteiro inv�lido:";
                commentArray[2] = "Entre com um valor inteiro v�lido";
            } else {
                try {
                    //workaround for BlueJ bug - misses first exception after compilation
                    //response = Integer.parseInt(result); // EA: ?
                    response = Integer.parseInt(result);
                    validResponse = true;
                } catch (NumberFormatException exception) {
                    commentArray[1] = "Valor inteiro inv�lido: " + result;
                    commentArray[2] = "Entre com um valor inteiro v�lido";
                    initialValue = result; // EA: added
                }
            }
        }
    }
    return response;
}

/**
 ** returns double input from the user via a simple dialog.
 ** @return the input double
 **/
public static double leiaDouble() {
    return leiaDouble("","");
}

/**
 ** returns double input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @return the input double
 **/
public static double leiaDouble(String prompt) {
    return leiaDouble(prompt,"");
}

/**
 ** returns double input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @param initialValue input value that is initially displayed as selected by the user
 ** @return the input double
 **/
public static double leiaDouble(String prompt, double initialValue) {
    return leiaDouble(prompt,Double.toString(initialValue));
}

/**
 ** returns double input from the user via a simple dialog.
 ** @param prompt the message string to be displayed inside dialog
 ** @param initialValue input string that is initially displayed as selected by the user
 ** @return the input double
 **/
public static double leiaDouble(String prompt, String initialValue) {
    Object[] options = {"OK"};
    Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING};

    boolean validResponse = false;

    double response = 0.0;

    while (!validResponse) {
        final JOptionPane optionPane = new JOptionPane(commentArray,
                                                       JOptionPane.QUESTION_MESSAGE,
                                                       JOptionPane.OK_CANCEL_OPTION,
                                                       null,
                                                       options,
                                                       options[0]);

        optionPane.setWantsInput(true);
        optionPane.setInitialSelectionValue(initialValue);  // EA: added
        JDialog dialog = optionPane.createDialog(null, DOUBLE_TITLE);

        dialog.pack();
        dialog.show();

        Object input = optionPane.getInputValue();
        if (input == JOptionPane.UNINITIALIZED_VALUE) {
            commentArray[1] = "Precisa entrar com um valor fracion�rio"; // EA: explanatory text added
            commentArray[2] = EMPTY_STRING;
        } else {
            String result = (String) input;
            if (result == null) { // EA: added for completnes, but is this situation possible?
                commentArray[1] = "valor fracion�rio inv�lido:";
                commentArray[2] = "Entre com um valor fracion�rio v�lido";
            } else {
                // convert String to double
                try {
                    // workaround for BlueJ bug - misses first exception after recompilation?
                    response = Double.valueOf(result).doubleValue();
                    response = Double.valueOf(result).doubleValue();
                    validResponse = true;
                } catch (NumberFormatException exception) {
                    // EA: case with uninitialized value is moved up
                    commentArray[1] = "Valor fracion�rio inv�lido: " + result;
                    commentArray[2] = "Entre com um valor fracion�rio v�lido";
                    initialValue = result;    // EA: corrected
                }
            }
        }
    }
    return response;
}
public static void escrever(String mens){
    JOptionPane.showMessageDialog(null, mens);
}
public static void escrever(boolean mens){
    JOptionPane.showMessageDialog(null, "" + mens);
}
public static void escrever(char mens){
    JOptionPane.showMessageDialog(null, "" + mens);
}
public static void escrever(int mens){
    JOptionPane.showMessageDialog(null, "" + mens);
}
public static void escrever(double mens){
    JOptionPane.showMessageDialog(null, "" + mens);
}
}

<

Seria esse
Fefo80

Que volta pra uma coisa tão simples. Pegou essa classe onde?

Depende do que você quiser fazer. Atualmente esse código não serve pra nada.

L

Bom minha ideia era fazer um código que mostre o numero q eu digitar, e falar se é ou n divisível por 4 mostrando todos os números dividido por 4

Fefo80

Então vamos entender…

O que seu código atualmente faz?

Ele corre uma lista de números, dentro de um laço de repetição FOR, de 0 a 200, para identificar quais são ou não divisíveis por 4.

Isso é o que você escreveu na mensagem exatamente acima? Não.

Então o que você precisa fazer:

  1. Usar uma IDE
  2. Tirar o laço de repetição
  3. Testar o único valor coletado

Adicionalmente, por propósitos educacionais, eu te recomendo NÃO usar essa classe Entrada e fazer a entrada coletando o system.in via uma classe chamada Scanner

Antes do teste do 3, você mostra na tela (PRINTLN) qual o número digitado. E dentro desse teste do item 3 (vai ser um IF), se ele for divisível por 4, ele mostra (PRINTLN) a confirmação, mas se não for (ELSE), ele mostra (PRINTLN) que não é divisível.

Tenta aí e posta o código completo da sua main().

Criado 11 de julho de 2021
Ultima resposta 11 de jul. de 2021
Respostas 12
Participantes 4