import java.io.BufferedReader;
為企業(yè)提供網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)營(yíng)銷推廣、競(jìng)價(jià)托管、品牌運(yùn)營(yíng)等營(yíng)銷獲客服務(wù)。成都創(chuàng)新互聯(lián)擁有網(wǎng)絡(luò)營(yíng)銷運(yùn)營(yíng)團(tuán)隊(duì),以豐富的互聯(lián)網(wǎng)營(yíng)銷經(jīng)驗(yàn)助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營(yíng)銷獲客難題,做到“讓獲客更簡(jiǎn)單”。自創(chuàng)立至今,成功用技術(shù)實(shí)力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營(yíng)銷”三大難題,同時(shí)降低了營(yíng)銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!
import java.io.InputStreamReader;
public class Demo {
public static void main(String args[]) {
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(System.in));
int i = 0;
int j = 0;
String temp = null;
try {
System.out.println("請(qǐng)輸入第一個(gè)數(shù)字: ");
temp = br.readLine();
i = Integer.parseInt(temp);
System.out.println("請(qǐng)輸入第二個(gè)數(shù)字: ");
temp = br.readLine();
j = Integer.parseInt(temp);
System.out.println(i + "/" + j + "=" + (i/j));
}catch(Exception e) {
e.printStackTrace();
}finally {
System.out.println("程序結(jié)束!");
}
}
}
程序我測(cè)試過了,無問題,不過我是用UTF-8的編碼形式寫的,如果出現(xiàn)編碼問題的錯(cuò)誤,你按照我寫的重新自己寫一遍,就OK了,祝你學(xué)習(xí)順利!!!!
一共三個(gè)類:ScoreArray.java、StudentScoreArray.java和Test1.java,具體為:
public class ScoreArray {
private int[] scores;
private int scoreCount;
public int[] getScores() {
return scores;
}
public int getScoreCount() {
return scoreCount;
}
//構(gòu)造函數(shù)
public ScoreArray(int[] scores) {
this.scores = scores;
for (int score : scores) {
if (score = 0 score = 100) {
this.scoreCount++;
}
}
}
//求最大值
public int getMax() {
int[] scores = this.scores;
int temp;
for (int i = 0; i scores.length; i++) {
for (int j = 0; j scores.length - 1 - i; j++) {
if (scores[j] scores[j + 1]) {
temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
return scores[scores.length - 1];
}
//求最小值
public int getMin() {
int[] scores = this.scores;
int temp;
for (int i = 0; i scores.length; i++) {
for (int j = 0; j scores.length - 1 - i; j++) {
if (scores[j] scores[j + 1]) {
temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
return scores[0];
}
//求均值
public double getAvg() {
int sum = 0;
for (int score : this.scores) {
sum += score;
}
return new BigDecimal(sum).divide(
new BigDecimal(this.scores.length),
2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
//排序
public void sort() {
int temp;
for (int i = 0; i this.scores.length; i++) {
for (int j = 0; j this.scores.length - 1 - i; j++) {
if (this.scores[j] this.scores[j + 1]) {
temp = this.scores[j];
this.scores[j] = this.scores[j + 1];
this.scores[j + 1] = temp;
}
}
}
}
//靜態(tài)說明類
public static void explain() {
System.out.println("本類[ScoreArray]實(shí)現(xiàn)了數(shù)組的:求最值[getMax()]、求均值[getAvg()]和排序[sort()]方法");
}
}
public class StudentScoreArray extends ScoreArray {
public StudentScoreArray(int[] scores) {
super(scores);
}
//統(tǒng)計(jì)
public void statistic() {
super.sort();
MapInteger, Integer map = new LinkedHashMap();
for (int i : super.getScores()) {
if (map.containsKey(i)) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
map.forEach((k, v) - System.out.println("分?jǐn)?shù)為[" + k + "]的人數(shù)為:[" + v + "]"));
}
//靜態(tài)說明類
public static void explain() {
System.out.println("本類[StudentScoreArray]實(shí)現(xiàn)了數(shù)組的:求最值[getMax()]、求均值[getAvg()]、排序[sort()]和分布統(tǒng)計(jì)[statistic()]方法");
}
}
public class Test1 {
public static void main(String[] args) {
int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};
ScoreArray scoreArray = new ScoreArray(scores);
ScoreArray.explain();
System.out.print("數(shù)組內(nèi)容:[");
for (int i : scoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("有效值個(gè)數(shù):" + scoreArray.getScoreCount());
System.out.println("最大值:" + scoreArray.getMax());
System.out.println("最小值:" + scoreArray.getMin());
System.out.println("平均值:" + scoreArray.getAvg());
scoreArray.sort();
System.out.print("排序后數(shù)組內(nèi)容:[");
for (int i : scoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("");
System.out.println("========華麗的分割線========");
System.out.println("");
StudentScoreArray studentScoreArray = new StudentScoreArray(scores);
StudentScoreArray.explain();
System.out.print("數(shù)組內(nèi)容:[");
for (int i : studentScoreArray.getScores()) {
System.out.print(i + ",");
}
System.out.println("]");
System.out.println("有效值個(gè)數(shù):" + studentScoreArray.getScoreCount());
System.out.println("最大值:" + studentScoreArray.getMax());
System.out.println("最小值:" + studentScoreArray.getMin());
System.out.println("平均值:" + studentScoreArray.getAvg());
studentScoreArray.sort();
System.out.print("排序后數(shù)組內(nèi)容:[");
for (int i : studentScoreArray.getScores()) {
System.out.print(i + " ");
}
System.out.println("]");
System.out.println("分?jǐn)?shù)分布統(tǒng)計(jì):");
studentScoreArray.statistic();
}
}
其中對(duì)StudentScoreArray類我要特別說明一下:
統(tǒng)計(jì)分布情況時(shí),使用了Map,map是一種key-value的數(shù)據(jù)結(jié)構(gòu),其有個(gè)特點(diǎn)被我所利用:一個(gè)map中只能同時(shí)存在一個(gè)key,所以我以分?jǐn)?shù)為key,以數(shù)量為value,遍歷分?jǐn)?shù)數(shù)組時(shí),如果是第一次遇到這個(gè)key(分?jǐn)?shù)),則將其value(數(shù)量)置為1;如果已經(jīng)不是第一次遇見了,則將其value(數(shù)量)置為value + 1(數(shù)量 + 1)。另外需要遍歷這個(gè)map實(shí)現(xiàn)統(tǒng)計(jì)結(jié)果的打印,我這里使用了java8以后才支持的Lambda表達(dá)式,所以你要運(yùn)行這個(gè)程序必須要使用jdk1.8以上的版本。如果你覺得這樣不妥,可以網(wǎng)上再搜一下map的遍歷方式。
運(yùn)行結(jié)果:
啊 代碼又一坨的的擠在一起了,百度知道這個(gè)真是無語(yǔ),我把幾個(gè)關(guān)鍵的地方截個(gè)圖給你康康:
ScoreArray.java
成員變量和構(gòu)造函數(shù)
忘了說了,構(gòu)造的同時(shí)還統(tǒng)計(jì)了有效分?jǐn)?shù)(0~100)的數(shù)量
求最大值
冒泡排序后取最大值
求最小值
冒泡排序后取最小值
求均值
利用java 的BigDecimal類解決除法運(yùn)算精度的問題,四舍五入并保留了兩位小數(shù)
排序
就是冒泡排序,從小到大
靜態(tài)的文本說明
StudentScoreArray.java:
繼承
分?jǐn)?shù)分布統(tǒng)計(jì)
注意我說的map那里
靜態(tài)說明文本
Test1.java:
測(cè)試用例都使用的數(shù)組:int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};
public
class
Person
{
//(1)
設(shè)計(jì)一個(gè)類
人員,包含三個(gè)成員變量:姓名、年齡、性別。
private
String
name;
private
int
age;
private
String
sex;
public
String
getName()
{
return
name;
}
public
void
setName(String
name)
{
this.name
=
name;
}
public
int
getAge()
{
return
age;
}
public
void
setAge(int
age)
{
this.age
=
age;
}
public
String
getSex()
{
return
sex;
}
public
void
setSex(String
sex)
{
this.sex
=
sex;
}
}
//(2)
設(shè)計(jì)員工和顧客兩個(gè)類為
人員類的子類,員工類包含一個(gè)成員變量:職稱級(jí)別;顧客包含一個(gè)成員變量:賬戶余額。
class
worker
extends
Person{
private
String
Title_Level;
public
String
getTitle_Level()
{
return
Title_Level;
}
public
void
setTitle_Level(String
title_Level)
{
Title_Level
=
title_Level;
}
}
class
Customer
extends
Person{
private
int
Account_balance;
public
int
getAccount_balance()
{
return
Account_balance;
}
public
void
setAccount_balance(int
account_balance)
{
Account_balance
=
account_balance;
}
}
//(3)
設(shè)計(jì)一個(gè)類
取款機(jī)包含兩個(gè)成員方法:取款,查詢賬戶。
interface
ATM{
void
Teller(Customer
cus);
void
Checkaccount(Customer
cus);
}
//4)
實(shí)現(xiàn)取款機(jī)的取款方法,
//當(dāng)顧客來取錢但是賬戶余額不足時(shí)拋出異常“余額不足”并顯示,
//無論取款是否發(fā)生異常,最終取款方法需要執(zhí)行“退卡”操作。
class
ATMimpl
implements
ATM{
public
void
Checkaccount(Customer
cus)
{
}
public
void
Teller(Customer
cus)
{
}
}
public?static?void?main(String?args[])?{
Scanner?scanner?=?new?Scanner(System.in);
System.out.println("請(qǐng)輸入一個(gè)開始數(shù)字:");
int?a?=?0;
try?{
a?=?scanner.nextInt();
}?catch?(Exception?e)?{
System.out.println("輸數(shù)字不合法");????????
return;
}
System.out.println("請(qǐng)輸入一個(gè)結(jié)束數(shù)字:");
int?b?=?0;????
try?{
b?=?scanner.nextInt();
}?catch?(Exception?e)?{
System.out.println("輸數(shù)字不合法");????????
return;
}????
int?sum?=?0;????
if?(a??b)?{????????
for?(int?i?=?b;?i?=?a;?i++)?{
sum?=?sum?+?i;
}
}?else?{??????
for?(int?i?=?a;?i?=?b;?i++)?{
sum?=?sum?+?i;
}
}
System.out.println(sum);
}
新聞標(biāo)題:java面向?qū)ο缶幊檀a 面向?qū)ο蟪绦蚓幊?/a>
當(dāng)前URL:http://chinadenli.net/article14/hgdoge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站營(yíng)銷、外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)