用java写一个程序?

用java写一个程序?,第1张

JAVA程序计算素数

设计JAVA application程序,计算出20000000~300000000之间所有的素数,并将找到的素数写入primefile.dat文件,

以下是一个使用Java语言编写的程序,可以计算出20000000~300000000之间所有的素数,并将找到的素数写入primefile.dat文件:

javaCopy code

import java.io.FileOutputStreamimport java.io.IOExceptionpublic class PrimeNumberCalculator { public static void main(String[] args) { int start = 20000000int end = 300000000String filename = "primefile.dat"try (FileOutputStream fileOutputStream = new FileOutputStream(filename)) { for (int i = starti <= endi++) { if (isPrime(i)) { fileOutputStream.write(String.valueOf(i).getBytes())fileOutputStream.write(System.lineSeparator().getBytes())} } } catch (IOException e) { e.printStackTrace()} } private static boolean isPrime(int n) { if (n <= 1) { return false} for (int i = 2i <= Math.sqrt(n)i++) { if (n % i == 0) { return false} } return true} }

该程序首先定义了起始数字和终止数字,以及文件名。接下来,局裂在main方法中,程序使用for循环迭代从start到end之间的每个数字。对于每个数字,程序调用isPrime方法来判断它是否为素数。如果是素数,程序将该数字写入文件中,每个数字占一行。

isPrime方法使用了一个简单的算法桐茄闭来判断一个数字是否为素数:如果数字小于或等于1,那么它不是素数。否则,程序从2开始,一直到该数字的平方根之间的每个数字进行除法运算。如果该数字能被任何一个这些数字整除,那么它不是素数。

请注意,在此程纳晌序中,我们使用了Java 7引入的"try-with-resources"语句来自动关闭文件输出流。这样可以确保即使在发生异常的情况下,文件输出流也会被正确关闭,以避免文件被损坏。

Java是一门计算机编程语言,面向对象的编程语言。Java可以开发后端,有乎戚spring,springmvc,springboot,springcould等等都是使用Java开发。Java也有前端框架,有Jquery,JavaScript,JSP,Servlet,统称JavaWEB开发。使用Java编写一个程序很简单,按照相关的轮顷链模板腊孙,加上业务代码就可以实现一套系统。

实现代码如下:

Student类:

public class Student {

private String name

private String sex

private int age

private double chinese

private double math

private double english

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public String getSex() {

return sex

}

public void setSex(String sex) {

this.sex = sex

}

public int getAge() {

return age

}

public void setAge(int age) {

this.age = age

}

public double getChinese() {

return chinese

}

public void setChinese(double chinese) {

this.chinese = chinese

}

public double getMath() {

return math

}

public void setMath(double math) {

this.math = math

}

public double getEnglish() {

return english

}

public void setEnglish(double english) {

this.english = english

}

}

-----------------------------------------------------------------

StudentTest类:腊誉(测试类)

import java.util.Scanner

public class StudentTest {

public static void main(String[] args) {

Student student = new Student()

Scanner sc = new Scanner(System.in)

System.out.println("请输入姓名:")

student.setName(sc.next())

System.out.println("请输入性别:")

student.setSex(sc.next())

System.out.println("请输入年龄:")

student.setAge(sc.nextInt())

System.out.println("请滚衫输入语文成绩、数学成绩、英语成绩:")

student.setChinese(sc.nextDouble())

student.setMath(sc.nextDouble())

student.setEnglish(sc.nextDouble())

Double count = student.getChinese()+ student.getMath()+student.getEnglish()

System.out.println("姓名:"+student.getName()+" 性别:"+student.getSex()+" 年龄:"+student.getAge())

System.out.println("总分:"+count+" 平均分:"+count/3)

}

}

运行结果为大局腔:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12324932.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存