Language Java


Unit 4: Lesson 1 - Coding Activity 3
Instructions
Write a program that requests the user input a word, then prints every other letter of the word starting with the first letter.
Hint - you will need to use the substring method inside a loop to get at every other character in the String. You can use the length method on the
String to work out when this loop should end.
Sample run:
Input a word:
domain
dmi

Language Java Unit 4 Lesson 1 Coding Activity 3 Instructions Write a program that requests the user input a word then prints every other letter of the word star class=

Respuesta :

import java.util.Scanner;

public class U4_L1_Activity_Three {

   

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       System.out.println("Input a word:");

       String word = scan.nextLine();

       int index = 0;

       while (index < word.length()){

           if (index%2 == 0){

               System.out.print(word.substring(index, index+1));

           }

           index+=1;

       }

       System.out.println("");

   }

   

}

I hope this helps!

The program is an illustration of while loops.

While loops are used to perform repetitive operations.

The program in Java where comments are used to explain each line is as follows:

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       //This prompts the user for a word

       System.out.println("Input a word:");

       //This gets user input

       String word = scan.nextLine();

       //This initializes the index of the string to 0

       int index = 0;

       //The following is repeated for every character in the string

       while (index < word.length()){

           //The following if condition checks for every other character in the string, starting from the first

           if (index%2 == 0){

               //This prints every other character

               System.out.print(word.substring(index, index+1));

           }

           index+=1;

       }

   }

}

Within the loop, every other character of the string is printed.

See attachment for sample run

Read more about similar programs at:

https://brainly.com/question/23798765

Ver imagen MrRoyal