How to print selected items?

Asked

Viewed 41 times

1

while(resposta == "sim"){
            System.out.println("Informe o Numero do Produto desejado");
            System.out.println("15 - Parafuso, 20 - Porca, 30 - Arruela");
            cod_peca = scn.nextInt();

                switch(cod_peca){
                    case 15:
                        System.out.println("Voce selecionou Parafuso");
                        break;
                    case 20:
                        System.out.println("Voce selecionou Porca");
                        break;
                    case 30:
                        System.out.println("Voce selecionou Arruela");
                        break;
                }
            System.out.println("Informe a Quantidade do Produto");
            qtdeProd = scn.nextInt();
        }
System.out.println("Numero do Produto comprado: "+cod_peca);
  • What specific problem are you encountering? Try to be a little more specific about what’s not working, and how I wish it was working

  • I’m sorry, I came in here today and I’m posting here for the first time. Half lost still kkk anyway, I am to read 3 products and that the person select only the products selected and that print it. I do not know, the option would be to switch case or if Else. could give me a help?

  • Possible duplicate of Unexecuted entry

  • One thing is that comparison of String is done with equals: "sim".equals(resposta)

  • vlw guys! It worked

1 answer

0

There is only one small problem that causes the Compiler to not Compile. The operator == is only used to compare numbers in Java (eg.: 2 == 4, 5 == 5; already 3 == "ola" no), but in other languages like Python it is used to compare números and strings.

To compare strings with números or strings with strings we don’t use == in Java, but we use equals resposta.equals("sim");

Do this and the program will run.

  • Thanks! It worked out!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.