Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm having an issue with my divisible by 10 statement. I'm supposed to have it check if a number in an array is divisible by 10. But for some reason it comes out as false numbers are divisible by 10. I actually had it set as an int but then had to change it from an int to a boolean.

What I have tried:

Java
package journal.pkg5a;

/**
 *
 * @author stephenwessels
 */
import javax.swing.*;
import java.util.Random;
public class Journal5A 
{
    
public int[] createArray(int size) 
{

    Random rnd = new Random();
    int[] array = new int[size];
    
    for(int i = 0; i < array.length; i++) 
        array[i] = rnd.nextInt(101);
        return array; 
}

public void printArray() 
{
    Journal5A c = new Journal5A();
    int[] myArray = c.createArray(10);
    

    for(int i = 0; i < myArray.length; i++) 
    {
        System.out.println(myArray[i]);
        System.out.println("There is " + c.divisibleby10(i) + " numbers that are divisible by 10");
    }
}
  public boolean divisibleby10(int x) 
  {
    return x % 10 == 0 ? true : false;
  }   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Journal5A c = new Journal5A();
        Random r = new Random();
        

        c.printArray();
    }
}
Posted
Updated 15-Feb-18 22:01pm
v2
Comments
PIEBALDconsult 15-Feb-18 23:43pm    
Return true. Done.

Check you order of operations.
Jon McKee 15-Feb-18 23:46pm    
I'm a little confused by your program. It seems like you're trying to count the numbers that are divisible by 10 with a function that simply checks to see if a single number is divisible by 10. Also you don't need the ternary ?: operator. x % 10 == 0 is a boolean condition already because of the equality operator ==.

1 solution

Try
Java
import java.util.Random;
public class Journal5A
{

  public int[] createArray(int size)
  {

    Random rnd = new Random();
    int[] array = new int[size];

    for(int i = 0; i < array.length; i++)
      array[i] = rnd.nextInt(101);
    return array;
  }

  public void printArray()
  {
    int[] myArray = createArray(10);

    int count = 0;

    for(int i = 0; i < myArray.length; i++)
    {
        boolean isDivisible = divisibleby10(myArray[i]);
        if ( isDivisible )
        {
          System.out.printf("%d is divisible by 10\n", myArray[i]);
          ++count;
        }
        else
          System.out.printf("%d is not divisible by 10\n", myArray[i]);
    }
    System.out.printf("There are %d numbers that are divisible by 10\n", count);
  }
  public boolean divisibleby10(int x)
  {
    return x % 10 == 0;
  }
  public static void main(String[] args)
  {
    Journal5A c = new Journal5A();
    c.printArray();
  }
}
 
Share this answer
 
Comments
Barais_19 18-Feb-18 20:07pm    
Thanks! Got it all sorted out now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900