使用循環(huán)結(jié)構(gòu),通過Scanner類從控制臺輸入每個人的捐款金額,并累加每個人的捐款金額,計算捐款總數(shù)和捐款人數(shù),如果捐款總數(shù)達到10萬元,則計算平均每人捐款金額。

蕪湖縣網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
下面是通過輸入進行統(tǒng)計,實際過程中還能是通過數(shù)據(jù)庫進行操作,或者表格收集數(shù)據(jù)進行導入關(guān)系數(shù)據(jù)庫進行統(tǒng)計。還可以通過接入微信或者支付寶小程序收款實時統(tǒng)計。
已經(jīng)有人給過隨機數(shù)示例,這里給出前端輸入的示例:
import java.util.Scanner;
public class CharityDonation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = 0; // 捐款人數(shù)
int total = 0; // 募捐總數(shù)
int avg = 0; // 平均每人捐款金額
int donation; // 每個人的捐款金額
while (total 100000 num 1000) {
System.out.print("請輸入第" + (num + 1) + "個人的捐款金額:");
donation = scanner.nextInt();
total += donation;
num++;
} if (total = 100000) {
avg = total / num;
System.out.println("捐款總數(shù)已達到10萬元,捐款人數(shù)為" + num + ",平均每人捐款金額為" + avg + "元。");
} else {
System.out.println("征集慈善募捐失敗,募捐總數(shù)未達到10萬元。");
}
scanner.close();
}
}
下面是一個Java代碼示例,實現(xiàn)了以上要求:
```java
// 定義Person類,具有身高/體重/年齡等屬性
class Person {
int height;
int weight;
int age;
// 構(gòu)造函數(shù)
public Person(int height, int weight, int age) {
this.height = height;
this.weight = weight;
this.age = age;
}
// 輸出身高、體重、年齡信息的方法
public void display() {
System.out.println("身高:" + height + "cm");
System.out.println("體重:" + weight + "kg");
System.out.println("年齡:" + age + "歲");
}
// 打招呼的方法
public void say() {
System.out.println("大家好,我是一名普通人。");
}
}
// 由Person類派生出Usa類,增加愛好屬性,覆蓋父類方法
class Usa extends Person {
String hobby;
// 構(gòu)造函數(shù)
public Usa(int height, int weight, int age, String hobby) {
// 調(diào)用父類的構(gòu)造函數(shù)
super(height, weight, age);
this.hobby = hobby;
}
// 覆蓋父類的display方法,增加輸出愛好信息
public void display() {
super.display();
System.out.println("愛好:" + hobby);
}
// 覆蓋父類的say方法,輸出美式打招呼的信息
public void say() {
System.out.println("Hi, everyone! I am an American. Nice to meet you all!");
}
}
// 由Person類派生出China類,增加國籍屬性,覆蓋父類方法
class China extends Person {
String nationality;
// 構(gòu)造函數(shù)
public China(int height, int weight, int age, String nationality) {
// 調(diào)用父類的構(gòu)造函數(shù)
super(height, weight, age);
this.nationality = nationality;
}
// 覆蓋父類的display方法,增加輸出國籍信息
public void display() {
super.display();
System.out.println("國籍:" + nationality);
}
// 覆蓋父類的say方法,輸出中式打招呼的信息
public void say() {
System.out.println("大家好,我是一名中國人。很高興認識大家!");
}
}
// 由China類派生出Guangxi類,增加語言屬性,覆蓋父類方法
class Guangxi extends China {
String dialect;
// 構(gòu)造函數(shù)
public Guangxi(int height, int weight, int age, String nationality, String dialect) {
// 調(diào)用父類的構(gòu)造函數(shù)
super(height, weight, age, nationality);
this.dialect = dialect;
}
// 覆蓋父類的display方法,增加輸出方言信息
public void display() {
super.display();
System.out.println("方言:" + dialect);
}
// 覆蓋父類的say方法,輸出關(guān)西方言打招呼的信息
public void say() {
System.out.println("嗨,同志們好啊! 我系一個桂平鐵花郎,興縣塘豆腐買八兩!");// 這里是廣西壯話
}
}
public class Main {
public static void main(String[] args) {
// 創(chuàng)建一個Person對象
Person person = new Person(180, 75, 25);
System.out.print("Person的信息:");
person.display();
System.out.print("Person的招呼:");
person.say();
// 創(chuàng)建一個Usa對象
Usa usa = new Usa(175, 70, 30, "Basketball");
System.out.print("Usa的信息:");
usa.display();
System.out.print("Usa的招呼:");
usa.say();
// 創(chuàng)建一個China對象
China china = new China(170, 65, 28, "Chinese");
System.out.print("China的信息:");
china.display();
System.out.print("China的招呼:");
china.say();
// 創(chuàng)建一個Guangxi對象
Guangxi guangxi = new Guangxi(165, 60, 26, "Chinese", "桂林話");
System.out.print("Guangxi的信息:");
guangxi.display();
System.out.print("Guangxi的招呼:");
guangxi.say();
}
}
```
在上面的代碼中,我們定義了一個`Person`類,它具有身高/體重/年齡三個屬性,并實現(xiàn)了對這些屬性進行輸出和打招呼的方法。然后,我們創(chuàng)建了基于`Person`類的`Usa`類和`China`類,它們分別增加了愛好和國籍兩個屬性,并覆蓋了父類的輸出和打招呼方法。最后,我們還創(chuàng)建了基于`China`類的`Guangxi`類,它增加了方言屬性,并覆蓋了父類的輸出和打招呼方法。在主函數(shù)中,我們創(chuàng)建了不同類的對象,并輸出了它們的信息和打招呼語句。
下面是一個存儲班級三十名學生基本信息的 Java 代碼示例:
import java.util.ArrayList;
public class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
public class Classroom {
private ArrayListStudent students;
public Classroom() {
this.students = new ArrayListStudent();
}
public void addStudent(Student student) {
students.add(student);
}
public ArrayListStudent getStudents() {
return students;
}
}
public class Main {
public static void main(String[] args) {
Classroom classroom = new Classroom();
// 添加 30 名學生的基本信息
classroom.addStudent(new Student("Tom", 18, "Male"));
classroom.addStudent(new Student("Alice", 19, "Female"));
// ...
// 此處省略 28 名學生的信息
// 獲取所有學生的信息
ArrayListStudent students = classroom.getStudents();
for (Student student : students) {
System.out.println("Name: " + student.getName());
System.out.println("Age: " + student.getAge());
System.out.println("Gender: " + student.getGender());
}
}
}
該代碼定義了兩個類:Student 類表示一個學生,包含了學生的姓名、年齡和性別等信息;Classroom 類表示一個班級,包含了一個學生的列表,并提供了添加學生和獲取學生列表的方法。
在 Main 類的 main 方法中,我們首先實例化一個 Classroom 對象,然后依次添加 30 名學生的信息。最后,我們調(diào)用 getStudents 方法獲取所
importjava.awt.*;importjava.awt.event.*;classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4;Buttonbutton1,button2,button3,button4,button5;TextAreatext;Panelpanel1,panel2;staticfloatsum=0.0f;ShopFrame(Strings){super(s);setLayout(newBorderLayout());label1=newLabel("面紙:3元",Label.LEFT);label2=newLabel("鋼筆:5元",Label.LEFT);label3=newLabel("書:10元",Label.LEFT);label4=newLabel("襪子:8元",Label.LEFT);button1=newButton("加入購物車");button2=newButton("加入購物車");button3=newButton("加入購物車");button4=newButton("加入購物車");button5=newButton("查看購物車");text=newTextArea("商品有:"+"\n",5,10);text.setEditable(false);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);panel1=newPanel();panel2=newPanel();panel1.add(label1);panel1.add(button1);panel1.add(label2);panel1.add(button2);panel1.add(label3);panel1.add(button3);panel1.add(label4);panel1.add(button4);panel2.setLayout(newBorderLayout());panel2.add(button5,BorderLayout.NORTH);panel2.add(text,BorderLayout.SOUTH);this.add(panel1,BorderLayout.CENTER);this.add(panel2,BorderLayout.SOUTH);setBounds(100,100,350,250);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一個面紙、");sum=sum+3;}elseif(e.getSource()==button2){text.append("一只鋼筆、");sum=sum+5;}elseif(e.getSource()==button3){text.append("一本書、");sum=sum+10;}elseif(e.getSource()==button4){text.append("一雙襪子、");sum=sum+8;}elseif(e.getSource()==button5){text.append("\n"+"總價為:"+"\n"+sum);}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("購物車");}}我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個。
網(wǎng)頁題目:java基礎(chǔ)代碼示例 java編程常用代碼
網(wǎng)頁URL:http://www.chinadenli.net/article32/dddjepc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站內(nèi)鏈、虛擬主機、搜索引擎優(yōu)化、全網(wǎng)營銷推廣、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)