简体   繁体   English

不带全零的因子打印数组

[英]printing array with factors without all the 0s

i am having an issue trying to get the array that is output from testperfect method to print correctly, i know i need to change my print statement but am unsure how(this statement is in the last method printFactors) i need it to print the factors in the array testperfect makes but i do not want it to print the 0s. 我在尝试获取从testperfect方法输出的数组以正确打印时遇到问题,我知道我需要更改我的打印语句,但是不确定如何(此语句在最后一个方法printFactors中)我需要它来打印因子在数组testperfect使,但我不希望它打印0。 I have to use an array and the array has have 100 for its size. 我必须使用一个数组,该数组的大小为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");
 }

}




}

For this you can have two solutions. 为此,您可以有两种解决方案。

1.Use any Sorting technique or Use the collection framework sorting method and sort this array and then, Iterate through the array, and if the element is '0', dont print that. 1.使用任何排序技术或使用收集框架排序方法对数组进行排序,然后遍历数组,如果元素为“ 0”,则不要打印该数组。

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

2.Use an ArrayList. 2.使用ArrayList It is flexible to use. 使用灵活。 Add the perfect numbers to this ArrayList and print that. 将完美的数字添加到此ArrayList并打印出来。 It will contain only the elements added. 它将仅包含添加的元素。

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

ArrayList is most preferable for this because, 为此,ArrayList是最可取的,因为,

There is no need to initialise how many elements you want. 无需初始化您想要多少个元素。 It is flexible to use. 使用灵活。 It will expand when you add the elements. 添加元素时,它将展开。 No need to worry about the size. 无需担心大小。

I think the below program works fine for your case. 我认为以下程序适合您的情况。

import java.util.ArrayList;
import java.util.Arrays;

public class TestArray {

    private static void print(Integer[] array)
    {
        for(Integer el:array)
            System.out.print(el + " ");
        System.out.println("");
    }


    public static void main(String[] args) {

        // Constructing an Integer array. You can use input-reader to fill the array
        Integer[] array =  new Integer[]{0,1,123,456,-89,0,-1,567,0,23,231,987,0,987654,0};

        // printing the unsorted content 
        print(array);

        // sorting the array
        Arrays.sort(array);

        // printing the sorted content, with '0' values in it 
        print(array);

        //Converts the integer[] to ArrayList, so that i can easily remove elements with value '0' 
        ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array));

        // prints the arraylist
        System.out.println(arrayList);

        //removes the elements with value '0' 
        while(arrayList.contains(new Integer(0)))
            arrayList.remove(new Integer(0));
        System.out.println(arrayList);

        // converting back to Integer[]
        array = arrayList.toArray(new Integer[]{});

        // prints to see that the final array is sorted and conatins no '0' values
        print(array);
    }
}

Please test and let me know the status :) 请测试并让我知道状态:)

And the output 和输出

0 1 123 456 -89 0 -1 567 0 23 231 987 0 987654 0 
-89 -1 0 0 0 0 0 1 23 123 231 456 567 987 987654 
[-89, -1, 0, 0, 0, 0, 0, 1, 23, 123, 231, 456, 567, 987, 987654]
[-89, -1, 1, 23, 123, 231, 456, 567, 987, 987654]
-89 -1 1 23 123 231 456 567 987 987654 

EDIT 编辑

I got your issue solved. 我解决了你的问题。 Here is another example which just uses the int[] and removes the 0 elements from it. 这是另一个仅使用int[]并从中删除0元素的示例。

import java.util.Arrays;

public class TestArray {

    private static void print(int[] array)
    {
        for(int el:array)
            System.out.print(el + " ");
        System.out.println("");
    }


    public static void main(String[] args) {

        // Constructing an Integer array. You can use input-reader to fill the array
        int[] array =  new int[]{0,1,123,456,-89,0,-1,567,0,23,231,987,0,987654,0};
//      int[] array =  new int[]{1,123,456,-89,-1,567,23,231,987,987654};
//      int[] array =  new int[]{1,123,456,-89,-1,567,23,231,987,987654,0};
//      int[] array =  new int[]{1,123,456,567,23,231,987,987654};

        // printing the unsorted content 
        print(array);

        // sorting the array
        Arrays.sort(array);

        // printing the sorted content, with '0' values in it 
        print(array);

        // finds the starting and ending position of '0' elements in the sorted array
        int startPos = -1;
        int endPos = -1;
        for(int i=0;i<array.length;i++)
        {
            if(array[i]==0 && startPos == -1) startPos = i;
            if(array[i]==0) endPos = i;
        }
        System.out.println("startPos : " + startPos + " endPos : " + endPos);

        int[] newArray = null;
        if(startPos!=-1) // there are '0' elements in the array
        {
            // creates another array with new length. 
            int newArrayLength = array.length - (endPos-startPos+1);
            System.out.println("array.length : "+array.length);
            System.out.println("newArrayLength : "+newArrayLength);

            newArray = new int[newArrayLength];
            // copy the contents from original array till start of first '0' value
            System.arraycopy(array, 0, newArray, 0, startPos);
            // copy the remaining contents from original array after end of last '0' value
            System.arraycopy(array, endPos+1, newArray, startPos , newArrayLength-startPos);
        }
        else // no '0' values present in array
        {
            // just copy the original array to new array
            int newArrayLength = array.length ;
            newArray = new int[newArrayLength];
            System.arraycopy(array, 0, newArray, 0 , newArrayLength);
        }

        // prints to see that the final array is sorted and conatins no '0' values
        print(newArray);
    }
}

and the output 和输出

0 1 123 456 -89 0 -1 567 0 23 231 987 0 987654 0 
-89 -1 0 0 0 0 0 1 23 123 231 456 567 987 987654 
startPos : 2 endPos : 6
array.length : 15
newArrayLength : 10
-89 -1 1 23 123 231 456 567 987 987654 
  import java.util.*;
  public class TestPerfect {
public static void main(String a[]){
    ArrayList<Integer> perfectNos = new ArrayList<Integer>();
    System.out.println("Enter the perfect number");
    Scanner sc = new Scanner(System.in);
    int per = sc.nextInt();
    int perflag = 0;
    int sum = 0;
    for(int i=1;i<per;i++){
        if(per % i == 0)
            sum =sum+i;
    }
    if(sum == per){
        for(int i=1;i<per;i++)
            if(per % i == 0){
                perfectNos.add(i);
            }
        System.out.print(per+":");
        for(int i=0;i<perfectNos.size();i++)
            System.out.print(perfectNos.get(i)+" ");
    }
    else
        System.out.println(per+": NOT PERFECT");
}

} }

This will solve your problem. 这样可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM