205k views
4 votes
3. Describe what is happening in this code

import java.util.Scanner;

public class WordFinder {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println ("Please enter a word");
String usrWord = s.nextLine();
String alph = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

for(int i = alph.length () - 1; i >= 0; i--){
System.out.println(alph.charAt (i) + usrWord);

}
}
}

User Cbartosiak
by
4.1k points

2 Answers

4 votes

Answer:

lol

Step-by-step explanation:

User Bartosz Bierkowski
by
4.1k points
3 votes

Answer:

Prints out the word a user has inputted once for each letter of the alphabet string, with the letter (starting from "Z") and the word together.

Step-by-step explanation:

Hi there! I program in C# and javascript mainly but I'm pretty sure I can see what's happening here. :)

Scanner is used to get user input.

We import the library at the top so that we can use it.

Inside main in the WordFinder class we create a new instance of the Scanner object so we can use it to get input. We assign this to the variable "s".

On the next line we use System.out.printIn to print a message that asks the user to enter a word.

On the next line we use s.nextLine() ("s" to reference the Scanner) to get the input from the user and we assign the string to the String type variable called "usrWord".

On the next line we assign a string that is all capital letters of the alphabet to a String type variable called "alph".

Under this, we create a for loop

"i" is the length of the alph string minus one (so 25, it doesn't need to be 26 as java uses zero-based indexing which is just a fancy way to say counting starts at 0 instead of 1)

The for loop says that while "i" (starting from 25) is more than or equal to 0, to minus 1 from "i".

For each time it does this, it cycles through each letter of the alphabet string backwards (because we're starting at 26 printing the letter and the word as one string each time.

(charAt is a method that returns the character at the index number in the string it is given btw)

Example output of the code, if the user inputs the word "cat", it would print:

Zcat

Ycat

Xcat

Wcat

Vcat

(and so on, you get the picture)

Let me know if you need any of that clearing up! :)

Hope that helps!

User Tomasz Mularczyk
by
3.6k points