How to use an existing variable as a counter

Asked

Viewed 922 times

0

My goal is to take the letter of the alphabet that the user typed and show, as many times as he type, the letters predecessor and successors of this, understand?
Look what I did:

static int v;
static String[] Alfa = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
        "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

public static void main(String[] args) {
    String letra;
    Scanner s = new Scanner(System.in);

    System.out.print("Digite uma letra: ");
    letra = s.nextLine();
    System.out.print("Quantas letras deseja mostrar?");
    v = s.nextInt();

    for(int i = 1; i < Alfa.length; i++) {
        if(letra == Alfa[i]) {
            for(int i; i <= 1;) {
                System.out.print(Alfa[i--] + " ");
            }

            System.out.print(Alfa[i]);

            for(int i; i <= v;) {
                System.out.print(" " + Alfa[i++]);
            }
        }
    }
}  

It turns out I have to use the value of the Alpha[i] that’s inside if we’re for’s, but how do I do that?

  • makes a for out to find out which vector position is the letter q he typed, dps a for v vezes showing i++ and i--

2 answers

2

It may be interesting to make an error handling in case the user enters with a valve that will exceed the size of the String, for example choose the letter Y and show 20 elements. I didn’t do it because it wasn’t mentioned. In general it would look like this:

public static void main(String[] args) {
    String letra;
    Scanner s = new Scanner(System.in);

    System.out.print("Digite uma letra: ");
    letra = s.nextLine();
    System.out.println(letra);
    System.out.print("Quantas letras deseja mostrar?");
    v = s.nextInt();

    for(int i = 0; i < Alfa.length; i++) {
        if(letra.equals(Alfa[i])) {
            for(int j = v; j > 0; j--) {
                System.out.print(Alfa[i-j] + " ");

            }

            System.out.print(Alfa[i]);

            for(int j = 1; j <= v; j++) {
                System.out.print(" " + Alfa[i+j]);
            }
        }
    }
}

Input example:

D
2

Exit:

B C D E F

0

As I suggested in the commentary:

static int v;
static String[] Alfa = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
        "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

public static void main(String[] args) {
    String letra;
    Scanner s = new Scanner(System.in);

    System.out.print("Digite uma letra: ");
    letra = s.nextLine();
    System.out.print("Quantas letras deseja mostrar?");
    v = s.nextInt();

    for(int i = 0; i < Alfa.length; i++) {
        if(letra == Alfa[i]) {
            break;
        }
    }

    for(int j = 1; j <= i; j++) {
        if (i+j < Alfa.length) {
            System.out.print(Alfa[i+j] + " ");
        }
        if (i-j >= 0) {
            System.out.print(Alfa[i-j] + " ");
        }
    }
}  

Browser other questions tagged

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