Make a program that receives as parameters a string and a character to be searched for and enter the index of the last occurrence of the character

Asked

Viewed 25 times

-1

I’m stuck in this part and I can’t think what else to do to progress.
I have little programming time, what can I do from here?

inserir a descrição da imagem aqui

2 answers

0


A good practice in Stackoverflow is to send code using the site’s own text editor (as in the code below), not in image! About your question:

class Main {
  public static void main(String[] args) {
    String palavra = "abacate";
    char buscar = 'c';
    int indice = -1;
    for(int i=0;i<palavra.length();i++) {
      char atual = palavra.charAt(i);
      if(atual == buscar) indice = i;
    }
    if(indice != -1) System.out.println("Indice: "+indice);
    else System.out.println("Char nao encontrado!");
  }
}
  • I adapted my program with your tips and I got it now, and I will also follow your tip on how to send the code, thank you so much for both!

0

Try after you close the is the following code: System.out.println("O último número é: " + n);

Browser other questions tagged

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