从数组中提取素数并从数组中删除并显示新数组... [英] extract prime number from array and delete from the array and display the new array...

查看:134
本文介绍了从数组中提取素数并从数组中删除并显示新数组...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它有array.From该数组提取素数并从数组中删除并显示新数组。我正在尝试但得到相同的答案,即数组的值...请帮助

______________________________________________________________________________

代码:

====== ================================================== =====

import java.util.Scanner;

class Prime

{

public static void main(String args [])

{

Scanner s = new Scanner(System.in);

int i,j,size;

布尔状态;

System.out.print(输入数组大小=);

size = s.nextInt();

int arr [] = new int [size];

System.out.println(在数组中输入元素......);

for(i = 0; i< arr.length; i ++)>

{

arr [i] = s.nextInt();

}

for(i = 0; i< arr.length; i ++)>

{

status = true;

for(j = 2; j< arr [i]; j ++)>

{

if(arr [i]%j == 0)

{

status = false;

休息;

}

}

if(status)

{

System.out.println(Prime Number Found =+ arr [i]);

}

}



}

}

$ b ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++ 5,2,3是素数



但是应该删除5,2,3并且新数组应该显示为10,8

I have a program that has array.From that array extract prime number and remove from the array and display the new array. I am trying but getting the same answer i.e values of array...please help
______________________________________________________________________________
code :
=============================================================
import java.util.Scanner;
class Prime
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int i,j,size;
boolean status;
System.out.print("Enter size of array=");
size=s.nextInt();
int arr[]=new int[size];
System.out.println("Enter Elements in array...");
for(i=0;i<arr.length;i++)>
{
arr[i]=s.nextInt();
}
for( i=0;i<arr.length;i++)>
{
status=true;
for(j=2;j<arr[i];j++)>
{
if(arr[i]%j==0)
{
status=false;
break;
}
}
if(status)
{
System.out.println("Prime Number Found="+arr[i]);
}
}

}
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
suppose i enter 5 elements : 5,2,10,8,3
answer=5,2,3 are prime Numbers

But 5,2,3 should be deleted and new array should be displayed like 10,8

推荐答案

为什么不尝试使用关键字'如何找到素数java'进行一点搜索。



为了便于删除值构成数组中的任何位置,你应该考虑使用动态数组,如链表。



如果这是学校工作,你可能需要创建自己的:

链接列表 [ ^ ]



您可以同时查看Java集合:
Java Collections教程 [ ^ ]
Why not try a little search with the keywords 'how to find prime numbers java'.

To make it easy to delete values form any position in an array, you should look into using dynamic arrays such as linked lists.

If this is school work, you might need to create your own:
Linked Lists[^]

You can aslo look into Java collections:
Java Collections Tutorial[^]


这篇关于从数组中提取素数并从数组中删除并显示新数组...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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