java编程作业(JAVA编程作业)

2023-11-14 14:42:28 数码极客 bianji01

 

JAVA编程作业

importjava.math.BigDecimal;

importjava.util.Random;

publicclassIncrease{

publicstATicbooleanisPrime(inta){

booleanFlag=true;

if(a<2){//素数不小于2

returnfalse;

}else{

for(inti=2;i<=Math.sqrt(a);i++){

if(a%i==0){//若能被整除,则说明不是素数,返回false

flag=false;

break;//跳出循环

returnflag;

publicstaticvoidtest1(){

inti,n,k=0;

System.out.println("1-1000内素数");

for(n=3;n<=1000;n++){//3~1000的所有数

while(i

if(n%i==0)break;//若能整除说明n不是素数,跳出当前循环

if(i==n){//如果i==n则说明n不能被2~n-1整除,是素数

k++;//统计输出数的个数

System.out.print(i+"\t");

if(k%6==0)//每输出5个则换行

System.out.println();

publicstaticvoidtest2()

Randomr=newRandom();

System.out.println(r.nextInt(301)-100);

publicstaticinttest3(intm,intn){

//辗转相除法

intr;

if(m

r=m%n;

}while(r!=0);

returnm;

publicstaticdoubletest4(intn){

doublee=1f;

doubletotal=1.0;

for(inti=0;i

total/=i+1;

e+=total;

BigDecimalb=newBigDecimal(e);

e=b.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();//表明四舍五入,保留四位小数

returne;

publicstaticvoidmain(String[]args){

//第一题测试

System.out.println(isPrime(131));

test1();

//第二题测试

test2();

//第三题测试

System.out.println("最大公约数为:"+test3(1302,19924));

//第四题测试

System.out.println("e="+test4(100));

运行结果:

1-1000内素数

357111317

192329313741

434753596167

717379838997

101103107109113127

131137139149151157

163167173179181191

193197199211223227

229233239241251257

263269271277281283

293307311313317331

337347349353359367

373379383389397401

409419421431433439

443449457461463467

479487491499503509

521523541547557563

569571577587593599

601607613617619631

641643647653659661

673677683691701709

719727733739743751

757761769773787797

809811821823827829

839853857859863877

881883887907911919

929937941947953967

971977983991997106

最大公约数为:2

e=2.7183

更多追问追答

只有第一题吗?

所有的都有了啊。。。你看main函数!!

能分开写吗?

而且这运行有5个错误

嗯,这是怎么回事?

上面的错误是你自己改我的代码的弄的。四条那个是空格,你看我的代码哪有空格。下面的错误是文件名和类名不一致。。。

java编程作业

public class Customer {private String memberName;private int memberNo;public Customer(String memberName,int memberNo){this.memberName = memberName;this.memberNo = memberNo;public String getMemberName() {return memberName;public void setMemberName(String memberName) {this.memberName = memberName;public int getMemberNo() {return memberNo;public void setMemberNo(int memberNo) {this.memberNo = memberNo;----------------------------------分界线---------------------------------------------------import java.util.ArrayList;public class CustManager {private static ArrayList list = new ArrayList();public void addCust(String memberName,int memberNo){list.add(new Customer(memberName, memberNo));public int searchMemberByName(String memberName){if(memberName == null || memberName.equals("")){return 1;for(Customer c : list){if(memberName.equals(c.getMemberName())){return c.getMemberNo();return 0;----------------------------------分界线---------------------------------------------------public class Test {public static void main(String[] args) {CustManager custManager = new CustManager();custManager.addCust("张三", 1);custManager.addCust("李四", 2);custManager.addCust("王五", 3);custManager.addCust("马六", 4);System.out.println(custManager.searchMemberByName("马六"));自己看书了做吧,很简单的这个多简单写好得怎么发给你呢需要数据库的吗?还是用swing做图形界面的?

声明:易趣百科所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系315127732@qq.com
广告位招租
横幅广告