boolean divisibleby10(int x)的问题 [英] Issue with boolean divisibleby10(int x)

查看:85
本文介绍了boolean divisibleby10(int x)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我可以整除10个声明。我应该检查一个数组中的数字是否可以被10整除。但由于某种原因,它出现了,因为错误的数字可以被10整除。我实际上把它设置为一个int,但后来不得不改变它int到布尔值。



我尝试过:



< pre lang =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 < /跨度> [大小];

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

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( + c.divisibleby10(i)+ 可被10整除的数字);
}
}
public boolean divisibleby10( int x)
{
return x% 10 == 0 ?真假;
}
/ * *
* @param args命令行参数
* /

public static void main( String [] args)
{
Journal5A c = Journal5A();
随机r = Random();


c.printArray();
}
}

解决方案

尝试

  import  java.util.Random; 
public class Journal5A
{

public int [] createArray( int size)
{

Random rnd = new Random();
int [] array = new int < /跨度> [大小];

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

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可被10 \ n,myArray [i])整除;
++ count;
}
else
System.out.printf( < span class =code-string>%d不能被10 \ n
,myArray [i])整除;
}
System.out.printf( 有%d个数字可被10整除\ n,伯爵);
}
public boolean divisibleby10( int x)
{
return x% 10 = = 0 ;
}
public static void main( String [] args)
{
Journal5A c = new Journal5A();
c.printArray();
}
}


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:

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();
    }
}

解决方案

Try

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();
  }
}


这篇关于boolean divisibleby10(int x)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆