107k views
4 votes
Write a method printshampoolnstructions0, 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. Hint: Declare and use a loop variable

İmport java.util.Scanner;
public class ShampooMethod ( 5 Your solution goes here
public static void main (String [1 args) (
Scanner senr new Scanner(System.in);
int userCycles; 10
userCycles scnr.nextInt): printShaspooInstructions(userCycles);

User Oddaspa
by
6.5k points

1 Answer

5 votes

Answer:

// Scanner import to allow user enter input

import java.util.Scanner;

// Class definition

public class ShampooMethod {

// The beginning of program execution which is the main method

public static void main(String args[]) {

//Scanner object to receive user input via the keyboard

Scanner scan = new Scanner(System.in);

//The received input is assigned to userCycles variable

int userCycles = scan.nextInt();

//printShampooInstructions method is called with userCycles as argument

printShampooInstructions(userCycles);

}

// The printShampooInstructions is defined

public static void printShampooInstructions(int numCycles){

// Check if numCycles is less than 1 and output "too few"

if(numCycles < 1){

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

} else if (numCycles > 4){

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

} else{

// for-loop which repeat for the range of numCycles and display the instruction

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

if(i == numCycles){

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

} else{

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

}

}

}

}

}

Step-by-step explanation:

The code has been commented to be explanatory. First the Scanner class is imported, then the class is defined. Then, the main method is also declared. Inside the main method, the user input is accepted and assigned to userCycles. The userCycles is passed as argument to the printShampooInstructions.

The printShampooInstructions is created with a parameter of numCycle and void return type. If the numberCycle is less than 1; "Too few..." is outputted. If numCycle is greater than 4; "Too many" is displayed. Else the shampoo instuction "Lather and rinse" is repeated for the numCycle. During the last repetition, "Done" is attached to the shampoo instruction.

User Senica Gonzalez
by
6.6k points