this method takes in an int - however I keep getting an error message, does anyone know why?
//finds the factors of a number that was entered
public void findFactors(int t)
{
System.out.println("factors of " +t+ " are:");
for(int i =0; i<t+1; i++)
{
if(t%i == 0)
{
System.out.println(i);
}
}
}
The problem is that t%i is undefined when i is 0, as you can't divide by zero nor find the remainder.
Instead of starting your loop from 0, you should start from 1.
Change
for(int i =0; i<t+1; i++)
to
for(int i =1; i<t+1; i++)
(You could also consider starting testing from 2, as 1 will be a factor for all integers)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.