java - 打印带有不全 0 的因子的数组

标签 java arrays sorting

我在尝试获取从 testperfect 方法输出的数组以正确打印时遇到问题,我知道我需要更改我的打印语句,但不确定如何(此语句位于最后一个方法 printFactors 中)我需要它打印数组 testperfect 中的因子,但我不希望它打印 0。我必须使用一个数组,该数组的大小为 100。

import java.util.Scanner;
public class name_perfect
{
 public static void main ( String args [] )
 {
  int gN;
  int gP = getPerfect();
  int [] array = new int[100];
  boolean tP = testPerfect(gP, array);
  printFactors(gP, array, tP);
  //System.out.println(Arrays.toString(array));
}


public static int getNum() //asks for how many numbers to test
{
 Scanner input = new Scanner ( System.in );
 System.out.print( "How many numbers would you like to test? " );
 int count = input.nextInt();
 int perfect = 1;
 boolean vN = validateNum(count, perfect);
 while(!vN)
 {
  System.out.print (" How many numbers would you like to test? ");
  count = input.nextInt();
  vN = validateNum(count, perfect);
 }
 return count;
 } 

public static boolean validateNum( int count, int perfect  ) //Checks if numbers input are valid
{
 if (( count <= 0) || ( perfect <= 0))

 { 
  System.out.print( "Non-positive numbers are not allowed.\n");
 }



 else 
 {
  return true;
 }
 return false;


}
public static int getPerfect() //asks for the numbers to test
{
 Scanner input = new Scanner ( System.in );
 int perfect = -1;
 int count = getNum();
 System.out.print("Please enter a perfect number: " );
 perfect = input.nextInt(); 
 boolean vN = validateNum(perfect, count);
 while (!vN) 
 {
  System.out.print("Please enter a perfect number: ");
  perfect = input.nextInt();
  vN=validateNum(perfect, count);
 }
 return perfect;
 }


public static boolean testPerfect( int perfect, int[] array ) //tests the numbers that were input 

{
 //testPerfect(perfect, array);
 int limit = perfect;
 int index = 0;
 for ( int i = 1; i < limit ; i++)
 {
  if ( perfect % i == 0)
   { array[i]=i;}
 }

 array[index] = perfect;

 int sum = 0;
 for ( int i = 1; i < limit; i++)
 {
  sum = sum + array[i];
 }

 if ( sum == perfect)
 {
  //Something has to change the array here.
  return true;  
 }

 else
 {
 return false;
 }


}

public static void printFactors(int perfect, int [] array, boolean tP )
 {
 if ( tP == true)
 {
 System.out.println (perfect + ":" + (Arrays.toString(array)));
 }
 else
 {
 System.out.println (perfect + ":" + "NOT PERFECT");
 }

}




}

最佳答案

为此,您可以有两种解决方案。

1.使用任何排序技术或使用集合框架排序方法对该数组进行排序,然后, 遍历数组,如果元素为“0”,则不打印它。

    for(int i=0;i<array.length;i++){
       if(array[i]==0)
         continue;
    else
       System.out.println(array[i]);
    }

2.使用ArrayList。使用起来很灵活。将完美数字添加到此 ArrayList 中并打印出来。它将仅包含添加的元素。

 for(int i=0;arrayList.size();i++)
 {
     System.out.println(arrayList.get(i));
 }

ArrayList 是最适合的,因为,

无需初始化您想要的元素数量。使用起来很灵活。当您添加元素时它会扩展。无需担心尺寸。

关于java - 打印带有不全 0 的因子的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13466831/

相关文章:

java - 为什么JavaFX不支持.wav格式?

java - 可变参数与方法重载

c - 从指针到指向原始数组的指针数组操作原始数组

arrays - Lua中如何迭代表?

java - 如何拆分字符串数组?

c - C中的慢基数排序

java - 在 Java 8 中使用 Java 7 比较器

java - 是否可以停止某个线程的运行?

C - 按特定列对 3d 字符串数组进行排序

java - Java中什么时候用Comparator,什么时候用Comparable?