Can anyone help me get my method to not print out the number count after it prints out the number of alarms the user inputs?

1439 views java
7

The challenge for my classwork was to write code that prints out the number of Alarms equal to the user's input. I print out the number of alarms but it also adds the original user input when I run it. Is there anyway I can remove the number it prints(int count) from the return?

import java.util.*;
public class classw2 {

public static void main(String[] args) {
    Scanner scan = new Scanner (System.in);
    System.out.println("How many times would you like it to print?");
    int count = scan.nextInt();
    System.out.println(alarm(count));

}
public static int alarm (int count)
{
    if (count < 1)
        System.out.println("Use another number");



        for(int counter = 0; counter < count; counter++)
        {
            System.out.println("Alarm!");
        }//for loop
    return count;
}//new method
}

answered question

Replace System.out.println(alarm(count)) with just alarm(count) ?

You can invoke your method as void instead of printing its return value, i.e. instead of System.out.println(alarm(count));, just alarm(count);.

1 Answer

7

You can try that:

import java.util.*;
public class classw2 {

public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println("How many times would you like it to print?");
int count = scan.nextInt();
alarm(count);

}
public static void alarm (int count)
{
    if (count < 1)
        System.out.println("Use another number");

        for(int counter = 0; counter < count; counter++)
        {
            System.out.println("Alarm!");
        }
    }
}

posted this

Have an answer?

JD

Please login first before posting an answer.