Java 打印带系数的数组,但不包含所有0
我在尝试使testperfect方法输出的数组正确打印时遇到问题,我知道我需要更改我的打印语句,但不确定如何更改(此语句在最后一个方法printFactors中),我需要它打印testperfect方法中的因子,但我不希望它打印0。我必须使用一个数组,数组大小为100Java 打印带系数的数组,但不包含所有0,java,arrays,sorting,Java,Arrays,Sorting,我在尝试使testperfect方法输出的数组正确打印时遇到问题,我知道我需要更改我的打印语句,但不确定如何更改(此语句在最后一个方法printFactors中),我需要它打印testperfect方法中的因子,但我不希望它打印0。我必须使用一个数组,数组大小为100 import java.util.Scanner; public class name_perfect { public static void main ( String args [] ) { int gN; in
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");
}
}
}
import java.util.Scanner;
公共类名
{
公共静态void main(字符串参数[])
{
int-gN;
int gP=getPerfect();
int[]数组=新的int[100];
布尔tP=testPerfect(gP,数组);
打印因子(gP、数组、tP);
//System.out.println(array.toString(array));
}
public static int getNum()//询问要测试多少个数字
{
扫描仪输入=新扫描仪(System.in);
System.out.print(“您想测试多少个数字?”);
int count=input.nextInt();
int-perfect=1;
布尔值vN=validateEnum(计数,完美);
而(!vN)
{
System.out.print(“您想测试多少个数字?”);
count=input.nextInt();
vN=验证枚举(计数,完美);
}
返回计数;
}
public static boolean validateEnum(int count,int perfect)//检查输入的数字是否有效
{
如果((计数),则可以有两种解决方案
1.使用任何排序技术或使用集合框架排序方法对该数组进行排序,然后,
遍历数组,如果元素为“0”,则不要打印该元素
for(int i=0;i<array.length;i++){
if(array[i]==0)
continue;
else
System.out.println(array[i]);
}
ArrayList最适合这样做,因为
无需初始化需要多少元素。它使用灵活。添加元素时它会扩展。无需担心大小。我认为下面的程序适合您的情况
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);
}
}
编辑
我解决了您的问题。下面是另一个示例,它只使用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);
}
}
import java.util.*;
公共类TestPerfect{
公共静态void main(字符串a[]{
ArrayList perfectNos=新的ArrayList();
System.out.println(“输入完整数字”);
扫描仪sc=新的扫描仪(System.in);
int per=sc.nextInt();
int perflag=0;
整数和=0;
对于(int i=1;i您有什么问题?请问一个问题。@RohitJain问题在代码上方我无法使用arraylist,正在尝试用布尔值实现排序,这导致了一些问题。您可以详细说明您想要打印的内容吗最后一个方法需要打印输入的数字以进行测试,然后如果它是完美的,就打印出来例如:如果一个数字是完美的输出=“28:14 7 4 2 1”如果一个数字是不完美的,那么输出将是“9:不完美”它必须从数组中读取因子,这就是为什么我需要删除所有0的编辑:毕竟不需要对数组进行排序,只需删除所有0。数组还必须设置为有100个变量,就像它一样。我不能使用arrayList,我不需要整个程序。我只需要知道如何操作当前使用的数组我想去掉所有的0,或者有一个打印语句,从中打印,但不输出0。我知道原因,为什么不能使用ArrayList
?这是一个任务,如果我可以做我想做的任何事情,它会容易得多,并且不会有所有的方法,或者我想做的事情在某种程度上是不可能的将我制作的第一个数组移动到另一个数组中,而不让它传输所有0?
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
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);
}
}
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");
}