Java程序协助
我被指派做以下工作Java程序协助,java,Java,我被指派做以下工作 为员工生成id号-每个id号必须满足以下要求: A.它一定是一个素数 B它不能与以前生成的ID号匹配 C它的长度必须正好是5位数 存储员工数据–对于此功能,应用程序必须允许您输入新员工的全名及其分配的ID号。这些必须存储在应用程序中 到目前为止,我不确定我是否做了正确的事情,但我有一些代码,我开始下面 import java.util.Scanner; public static void main(String[] args) { // TODO Auto-gen
import java.util.Scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
primenum();
}
public static void primenum() {
int max = 20000;
System.out.println("Generate Prime num" + max);
for (int i = 10000; i < max; i++) {
boolean isPrimeNumber = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrimeNumber = false;
}
}
// print the number if prime
if (isPrimeNumber) {
System.out.println(i + " ");
}
}
}
public static void ID() {
String[] emp = new String[10];
emp[0] = "John";
emp[1] = "Mary";
emp[2] = "James";
emp[3] = "chris";
emp[4] = "charles";
}
import java.util.Scanner;
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
质数();
}
公共静态void primenum(){
int max=20000;
System.out.println(“生成素数”+max);
对于(int i=10000;i
我刚刚创建了一个数组,其中包含一些名称。。但我想得到的主要目标是,生成的下一个素数存储在与名称关联的emp[]
中。。因此,对于例如emp[0]
的john,我希望他能够接收primenum()
方法的下一个素数。。我不知道如何做到这一点,我希望得到所有的帮助。欢迎来到SO
现在关于你的问题,我不太确定你想要什么,但我猜你想以某种方式将素数与你的员工姓名联系起来。如果是这样的话,下面可能会对你有所帮助
创建一个自定义对象,负责employee对象和质数(ID)之间的关系,如下所示:
public class EmployeeNameAndId {
private Employee employee;
private Integer id;
// constructors, getters, setters here
}
public class MainClass {
public static void main(String[] args) {
List<EmployeeNameAndId> l = new ArrayList<>();
l.add(new EmployeeNameAndId("John", generatePrimeId());
l.add(new EmployeeNameAndId("Mary", generatePrimeId());
l.add(new EmployeeNameAndId("James", generatePrimeId());
l.add(new EmployeeNameAndId("Chris", generatePrimeId());
}
}
现在,您需要在主类中使用此对象,如下所示:
public class EmployeeNameAndId {
private Employee employee;
private Integer id;
// constructors, getters, setters here
}
public class MainClass {
public static void main(String[] args) {
List<EmployeeNameAndId> l = new ArrayList<>();
l.add(new EmployeeNameAndId("John", generatePrimeId());
l.add(new EmployeeNameAndId("Mary", generatePrimeId());
l.add(new EmployeeNameAndId("James", generatePrimeId());
l.add(new EmployeeNameAndId("Chris", generatePrimeId());
}
}
public类MainClass{
公共静态void main(字符串[]args){
列表l=新的ArrayList();
l、 添加(new employeeNameId(“John”,generatePrimeId());
l、 添加(新员工名称(“玛丽”,generatePrimeId());
l、 添加(new employeeNameId(“James”,generatePrimeId());
l、 添加(新员工名称(“Chris”,generatePrimeId());
}
}
我希望这能帮助你,但如果这不是你想要的,那么请告诉我,我会尽力帮助你
祝你好运,学习愉快!欢迎来到SO
现在关于你的问题,我不太确定你想要什么,但我猜你想以某种方式将素数与你的员工姓名联系起来。如果是这样的话,下面可能会对你有所帮助
创建一个自定义对象,负责employee对象和质数(ID)之间的关系,如下所示:
public class EmployeeNameAndId {
private Employee employee;
private Integer id;
// constructors, getters, setters here
}
public class MainClass {
public static void main(String[] args) {
List<EmployeeNameAndId> l = new ArrayList<>();
l.add(new EmployeeNameAndId("John", generatePrimeId());
l.add(new EmployeeNameAndId("Mary", generatePrimeId());
l.add(new EmployeeNameAndId("James", generatePrimeId());
l.add(new EmployeeNameAndId("Chris", generatePrimeId());
}
}
现在,您需要在主类中使用此对象,如下所示:
public class EmployeeNameAndId {
private Employee employee;
private Integer id;
// constructors, getters, setters here
}
public class MainClass {
public static void main(String[] args) {
List<EmployeeNameAndId> l = new ArrayList<>();
l.add(new EmployeeNameAndId("John", generatePrimeId());
l.add(new EmployeeNameAndId("Mary", generatePrimeId());
l.add(new EmployeeNameAndId("James", generatePrimeId());
l.add(new EmployeeNameAndId("Chris", generatePrimeId());
}
}
public类MainClass{
公共静态void main(字符串[]args){
列表l=新的ArrayList();
l、 添加(new employeeNameId(“John”,generatePrimeId());
l、 添加(新员工名称(“玛丽”,generatePrimeId());
l、 添加(new employeeNameId(“James”,generatePrimeId());
l、 添加(新员工名称(“Chris”,generatePrimeId());
}
}
我希望这能帮助你,但如果这不是你想要的,那么请告诉我,我会尽力帮助你
祝您好运,学习愉快!使用此解决方案:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class PrimeNumber {
public static void main(String[] args) {
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("John", nextPrime(1));
map.put("Mary", nextPrime(map.get("John")));
map.put("James", nextPrime(map.get("Mary")));
map.put("chris", nextPrime(map.get("James")));
map.put("charles", nextPrime(map.get("chris")));
for(Entry<String, Integer> e:map.entrySet()) {
System.out.println(e.getKey()+": "+e.getValue());
}
}
public static int nextPrime(int input){
int counter;
input++;
while(true){
int l = (int) Math.sqrt(input);
counter = 0;
for(int i = 2; i <= l; i ++){
if(input % i == 0) counter++;
}
if(counter == 0)
return input;
else{
input++;
continue;
}
}
}
}
import java.util.HashMap;
导入java.util.Map;
导入java.util.Map.Entry;
公共类素数{
公共静态void main(字符串[]args){
Map Map=newhashmap();
地图放置(“约翰”,下一次(1));
map.put(“玛丽”),nexttime(map.get(“约翰”));
map.put(“詹姆斯”,nexttime(map.get(“玛丽”));
map.put(“chris”,nexttime(map.get(“James”));
map.put(“charles”,nexttime(map.get(“chris”));
对于(条目e:map.entrySet()){
System.out.println(e.getKey()+“:”+e.getValue());
}
}
公共静态int nextPrime(int输入){
整数计数器;
输入++;
while(true){
intl=(int)Math.sqrt(输入);
计数器=0;
对于(int i=2;i使用此解决方案:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class PrimeNumber {
public static void main(String[] args) {
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("John", nextPrime(1));
map.put("Mary", nextPrime(map.get("John")));
map.put("James", nextPrime(map.get("Mary")));
map.put("chris", nextPrime(map.get("James")));
map.put("charles", nextPrime(map.get("chris")));
for(Entry<String, Integer> e:map.entrySet()) {
System.out.println(e.getKey()+": "+e.getValue());
}
}
public static int nextPrime(int input){
int counter;
input++;
while(true){
int l = (int) Math.sqrt(input);
counter = 0;
for(int i = 2; i <= l; i ++){
if(input % i == 0) counter++;
}
if(counter == 0)
return input;
else{
input++;
continue;
}
}
}
}
import java.util.HashMap;
导入java.util.Map;
导入java.util.Map.Entry;
公共类素数{
公共静态void main(字符串[]args){
Map Map=newhashmap();
地图放置(“约翰”,下一次(1));
map.put(“玛丽”),nexttime(map.get(“约翰”));
map.put(“詹姆斯”,nexttime(map.get(“玛丽”));
map.put(“chris”,nexttime(map.get(“James”));
map.put(“charles”,nexttime(map.get(“chris”));
对于(条目e:map.entrySet()){
System.out.println(e.getKey()+“:”+e.getValue());
}
}
公共静态int nextPrime(int输入){
整数计数器;
输入++;
while(true){
intl=(int)Math.sqrt(输入);
计数器=0;
对于(int i=2;i)来说,每次程序运行时计算素数都有点低效。我会提前计算它们并将它们保存在一个文件中。然后重新读取它们。只有8363符合要求。旁注:为了更好地查找素数:“存储员工数据–对于此功能,应用程序必须允许您输入新员工的全名及其分配的ID号。这些信息必须存储在应用程序中。“-这是否意味着持久性?就像在DB或fata文件中一样?每次程序运行时计算素数有点效率低下。我会提前计算素数并将其保存在文件中。然后重新读取。只有8363符合要求。旁注:为了更好地查找素数:“s