實現(xiàn)代碼如下:
成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的柳北網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
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("請輸入語文成績、數(shù)學成績、英語成績:");
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);
}
}
運行結(jié)果為:
按照題目要求編寫的Java程序如下
注意 請使用你的真實姓名和班級替換Test類中
創(chuàng)建Student對象stu時用的"張三"和"20計算機應(yīng)用01班"
import java.util.Scanner;
class Student{
private String name,classname;
private int starnum,scorenum;
private int[] scores;
public void setStarNum(int n){
this.starnum=n;
}
public Student(String name,String classname,int scorenum){
this.name=name;
this.classname=classname;
this.scorenum=scorenum;
}
public String getName(){
return this.name;
}
public void printStar(){
for(int i=0;istarnum;i++){
for(int j=0;j2*i+1;j++){
System.out.print("*");
}
System.out.println();
}
}
public void setScore(){
Scanner sc=new Scanner(System.in);
scores=new int[scorenum];
System.out.print("請輸入各科成績:");
for(int i=0;iscorenum;i++){
scores[i]=sc.nextInt();
}
}
public void showInfo(){
System.out.print(name+"同學,你所在的班級是"+classname+",你各科考試成績分別為:");
for(int i=0;iscorenum;i++){
if(i==scorenum-1)
System.out.print(scores[i]);
else
System.out.print(scores[i]+",");
}
System.out.println();
}
public float getAvg(){
float sum=0;
for(int i=0;iscorenum;i++){
sum=sum+scores[i];
}
return sum/scorenum;
}
}
public class Test{
public static void main(String[] args){
Student stu=new Student("張三","20計算機應(yīng)用01班",5);
stu.setStarNum(4);
stu.printStar();
stu.setScore();
stu.showInfo();
if(stu.getAvg()60){
System.out.println(stu.getName()+"是不合格學生");
}else{
System.out.println(stu.getName()+"是個合格學生");
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class PrimeNumberFinder {
public static void main(String[] args) {
// 設(shè)置范圍
int start = 20000000;
int end = 300000000;
// 用于寫入文件的 BufferedWriter
BufferedWriter writer = null;
try {
// 初始化 BufferedWriter
writer = new BufferedWriter(new FileWriter("primefile.dat"));
// 遍歷范圍內(nèi)的所有數(shù)字
for (int i = start; i = end; i++) {
// 如果這個數(shù)字是素數(shù),寫入文件
if (isPrime(i)) {
writer.write(String.valueOf(i));
writer.newLine();
}
}
} catch (IOException e) {
// 如果出現(xiàn) IOException,輸出錯誤信息
e.printStackTrace();
} finally {
// 最后關(guān)閉 BufferedWriter
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 判斷一個數(shù)字是否為素數(shù)
public static boolean isPrime(int n) {
// 對于小于 2 的數(shù)字,直接返回 false
if (n 2) {
return false;
}
// 從 2 開始遍歷到 n-1,如果能被 n 整除,則返回 false
for (int i = 2; i n; i++) {
if (n % i == 0) {
return false;
}
}
// 如果執(zhí)行到這里,說明沒有小于 n 的數(shù)字能被 n 整除,返回 true
return true;
}