113,639 views
4 votes
Write a method printShampooInstructions(), with int parameter numCycles, and void return type. If numCycles is less than 1, print "Too few.". If more than 4, print "Too many.". Else, print "N: Lather and rinse." numCycles times, where N is the cycle number, followed by "Done.". End with a newline. Example output with input 2: 1: Lather and rinse. 2: Lather and rinse. Done.

User Colas
by
3.6k points

2 Answers

6 votes

Answer: is in BOLD

import java.util.Scanner;

public class ShampooMethod {

/* Answer */

public static void printShampooInstructions(int numOfCycles) {

if(numOfCycles < 1) {

System.out.println("Too few.");

}

else if(numOfCycles > 4) {

System.out.println("Too many.");

}

else {

for(int i = 0; i < numOfCycles; ++i) {

System.out.println((i + 1) + ": Lather and rinse.");

}

System.out.println("Done.");

}

}

public static void main (String [] args) {

Scanner scnr = new Scanner(System.in);

int userCycles;

userCycles = scnr.nextInt();

printShampooInstructions(userCycles);

}

}

User Kaysha
by
3.4k points
3 votes

Answer:

// The method is defined with a void return type

// It takes a parameter of integer called numCycles

// It is declared static so that it can be called from a static method

public static void printShampooInstructions(int numCycles){

// if numCycles is less than 1, it display "Too few"

if (numCycles < 1){

System.out.println("Too few.");

}

// else if numCycles is less than 1, it display "Too many"

else if (numCycles > 4){

System.out.println("Too many.");

}

// else it uses for loop to print the number of times to display

// Lather and rinse

else {

for(int i = 1; i <= numCycles; i++){

System.out.println(i + ": Lather and rinse.");

}

System.out.println("Done");

}

}

Step-by-step explanation:

The code snippet is written in Java. The method is declared static so that it can be called from another static method. It has a return type of void. It takes an integer as parameter.

It display "Too few" if the passed integer is less than 1. Or it display "Too much" if the passed integer is more than 4. Else it uses for loop to display "Lather and rinse" based on the passed integer.

User Saturn
by
3.5k points