//
目前創(chuàng)新互聯(lián)建站已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、華容網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
import?java.util.Scanner;
//
public?class?Test2014?{
public?static?void?main(String[]?args)?{
Scanner?sc?=?new?Scanner(System.in);
System.out.println("輸入學(xué)生人數(shù):");
int?n?=?sc.nextInt();
int?sum?=?0;
for(int?i?=?1;i?=?n;++i){
System.out.println("輸入第"+i+"個(gè)學(xué)生成績(jī):");
sum?+=?sc.nextInt();
}
System.out.println("總成績(jī)是:"+sum+"?"+"平均成績(jī)是:"+(double)sum/n);
}
}
正好我閑著,給你寫一個(gè)吧。
我寫的這個(gè)評(píng)委分?jǐn)?shù)是在代碼里固定到數(shù)組里了,如果你需要運(yùn)行時(shí)手動(dòng)輸入評(píng)分,可以將oldScores里的數(shù)據(jù)改成手動(dòng)輸入就行了(這個(gè)不用我再寫了吧,如果不會(huì)再追問(wèn),再告訴你)。
你先新建一個(gè)類,將下面的main方法全部復(fù)制進(jìn)去就能運(yùn)行了,自己看一下吧。
/**?主方法?*/
public?static?void?main(String[]?args)
{
/**?保存原始評(píng)分的數(shù)組(如果你需要運(yùn)行時(shí)手動(dòng)輸入分?jǐn)?shù),將?oldScores中的數(shù)據(jù)改成手動(dòng)輸入就行了?*/
double[]?oldScores?=?{15,?77,?55,?88,?79,?98,?67,?89,?68,?88};
/**?最終將用來(lái)保存排序后的數(shù)組?*/
double[]?scores?=?new?double[oldScores.length];
double?temp;
/**?平均分?*/
double?avg?=?0;
int?k;
/**?將原始評(píng)分放入最終排序數(shù)組?*/
for?(int?i?=?0;?i??oldScores.length;?i++)
{
scores[i]?=?oldScores[i];
}
/**?開(kāi)始排序?*/
for?(int?i?=?0;?i??scores.length?-?1;?i++)
{
k?=?i;
for?(int?j?=?i?+?1;?j??scores.length;?j++)
{
if?(scores[k]??scores[j])
{
k?=?j;
}
}
if?(i?!=?k)
{
temp?=?scores[k];
scores[k]?=?scores[i];
scores[i]?=?temp;
}
}
/**?計(jì)算去掉最高分和最低分之后的和?*/
double?sum?=?0;
/**?記錄計(jì)算平均分的分?jǐn)?shù)個(gè)數(shù)?*/
double?num?=?0;
for?(int?i?=?1;?i??scores.length?-?1;?i++)
{
num++;
sum?+=?scores[i];
}
/**?計(jì)算平均分?*/
avg?=?sum?/?num;
/**?最公平的肯定不是在scores數(shù)組兩端?*/
double?zgp?=?0;
double?cha?=?0;
/**?標(biāo)記與平均值差值最小的分?jǐn)?shù)位置?*/
int?flag?=?0;
/**?開(kāi)始尋找最公平評(píng)分?*/
for?(int?i?=?1;?i??scores.length?-?1;?i++)
{
/**?為cha賦初始值,注意比較差值要使用絕對(duì)值比較?*/
if?(i?==?1)
{
cha?=?Math.abs(scores[i]?-?avg);
}
double?cha1?=?Math.abs(scores[i]?-?avg);
if?(cha1??cha)
{
cha?=?cha1;
flag?=?i;
}
}
zgp?=?scores[flag];
/**?由于最不公平的分?jǐn)?shù)肯定在scores數(shù)組的第一個(gè)或者是最后一個(gè)?*/
double?bgp?=?0;
if?(Math.abs(scores[0]?-?avg)??Math.abs(scores[scores.length?-?1]?-?avg))
{
bgp?=?scores[0];
}
else
{
bgp?=?scores[scores.length?-?1];
}
/**?全部計(jì)算完成,下面開(kāi)始輸出結(jié)果?*/
System.out.println("原始評(píng)委分?jǐn)?shù)如下:");
for?(int?i?=?0;?i??oldScores.length;?i++)
{
System.out.print(oldScores[i]?+?",?");
}
System.out.println();
System.out.println("排序后分?jǐn)?shù)如下:");
for?(int?i?=?0;?i??scores.length;?i++)
{
System.out.print(scores[i]?+?",?");
}
System.out.println();
System.out.println("去掉最高分和最低分后平均分:"?+?avg);
System.out.println("最公平分?jǐn)?shù):"?+?zgp);
System.out.println("最不公平分?jǐn)?shù):"?+?bgp);
}
1、題目描述:
/*java編程:輸入某個(gè)班學(xué)生的成績(jī),輸入-1時(shí),表示輸入結(jié)束。計(jì)算該班的學(xué)生人數(shù)、最高分、最低分和平均分*/
分析:
根據(jù)題目可知,①需要連續(xù)輸入數(shù)據(jù),并將連續(xù)輸入的數(shù)據(jù)保存,②記錄輸入的數(shù)據(jù)個(gè)數(shù)③需要有輸入數(shù)據(jù)內(nèi)容判斷,如果輸入-1則停止輸入,執(zhí)行計(jì)算。
2、代碼如下
public?static?void?main(String[]?args)?{
ArrayListFloat?gList=new?ArrayList();//定義浮點(diǎn)型動(dòng)態(tài)數(shù)組,用作記錄成績(jī),也可以取數(shù)組長(zhǎng)度作為人數(shù)
Scanner?input?=new?Scanner(System.in);
float?grade=0;
?for?(?;grade!=-1;??)?{//當(dāng)輸入-1時(shí)結(jié)束
?System.out.println("請(qǐng)輸入學(xué)生成績(jī)(-1結(jié)束輸入):");
?grade=input.nextFloat();
?if?(grade!=-1)?{//避免最后一次錄入-1
?gList.add(grade);
}
}
//當(dāng)輸入-1時(shí)停止記錄數(shù)據(jù)
?
?
//求最高分
//最低分
float?max=0,sum=0,aveg=0;
float?min=gList.get(0);//取第一項(xiàng)作為最低分初始值
for?(int?i?=?0;?i??gList.size();?i++)?{
if?(maxgList.get(i))?{
max=gList.get(i);
}
if?(mingList.get(i))?{
min=gList.get(i);
}
sum+=gList.get(i);//求總分
}
//平均分
aveg=sum/gList.size();
System.out.println("共有?"+gList.size()+"人,"+"最高分:"+max
+",最低分:"+min+",平均分:"+aveg);
}
3、運(yùn)行效果
以下方法實(shí)現(xiàn)了用戶界面登陸
import java.awt.*;
import java.awt.event.*;
public class DengLuJieMian extends Frame implements ActionListener
{
Label username=new Label("用戶名:");//使用文本創(chuàng)建一個(gè)用戶名標(biāo)簽
TextField t1=new TextField();//創(chuàng)建一個(gè)文本框?qū)ο?/p>
Label password=new Label("密碼:");//創(chuàng)建一個(gè)密碼標(biāo)簽
TextField t2=new TextField();
Button b1=new Button("登陸");//創(chuàng)建登陸按鈕
Button b2=new Button("取消");//創(chuàng)建取消按鈕
public DengLuJieMian()
{
this.setTitle("學(xué)生信息管理系統(tǒng)");//設(shè)置窗口標(biāo)題
this.setLayout(null);//設(shè)置窗口布局管理器
username.setBounds(50,40,60,20);//設(shè)置姓名標(biāo)簽的初始位置
this.add(username);// 將姓名標(biāo)簽組件添加到容器
t1.setBounds(120,40,80,20);// 設(shè)置文本框的初始位置
this.add(t1);// 將文本框組件添加到容器
password.setBounds(50,100,60,20);//密碼標(biāo)簽的初始位置
this.add(password);//將密碼標(biāo)簽組件添加到容器
t2.setBounds(120,100,80,20);//設(shè)置密碼標(biāo)簽的初始位置
this.add(t2);//將密碼標(biāo)簽組件添加到容器
b1.setBounds(50,150,60,20);//設(shè)置登陸按鈕的初始位置
this.add(b1);//將登陸按鈕組件添加到容器
b2.setBounds(120,150,60,20);//設(shè)置取消按鈕的初始位置
this.add(b2);// 將取消按鈕組件添加到容器
b1.addActionListener(this);//給登陸按鈕添加監(jiān)聽(tīng)器
b2.addActionListener(this);// 給取消按鈕添加監(jiān)聽(tīng)器
this.setVisible(true);//設(shè)置窗口的可見(jiàn)性
this.setSize(300,200);//設(shè)置窗口的大小
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});//通過(guò)內(nèi)部類重寫關(guān)閉窗體的方法
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)//處理登陸事件
{
String name=t1.getText();
String pass=t2.getText();
if(name!=nullpass.equals("000123"))//判斷語(yǔ)句
{
new StudentJieMian();
}
}
}
public static void main(String args[])//主函數(shù)
{
new DengLuJieMian();
}
}
以下方法實(shí)現(xiàn)了學(xué)生界面設(shè)計(jì)
import java.awt.*;
import java.awt.event.*;
class StudentJieMian extends Frame implements ActionListener
{
MenuBar m=new MenuBar();//創(chuàng)建菜單欄
Menu m1=new Menu("信息");//創(chuàng)建菜單“信息”
MenuItem m11=new MenuItem("插入");//創(chuàng)建“插入”的菜單項(xiàng)
MenuItem m12=new MenuItem("查詢");
Menu m2=new Menu("成績(jī)");//創(chuàng)建菜單“成績(jī)”
MenuItem m21=new MenuItem("查詢");
public StudentJieMian()
{
this.setTitle("學(xué)生界面");//設(shè)置窗口標(biāo)題
this.setLayout(new CardLayout());//設(shè)置窗口布局管理器
this.setMenuBar(m);//將菜單欄組件添加到容器
m.add(m1);//將信息菜單放入菜單欄
m.add(m2);
m1.add(m11);//將“插入”菜單項(xiàng)添加到“信息”菜單
m1.add(m12); //將“查詢”菜單項(xiàng)添加到“信息”菜單
m2.add(m21); //將“查詢”菜單項(xiàng)添加到“成績(jī)”菜單
m11.addActionListener(this); //給“插入”菜單項(xiàng)添加監(jiān)聽(tīng)器
m12.addActionListener(this); //給“查詢”菜單項(xiàng)添加監(jiān)聽(tīng)器
m21.addActionListener(this); //給“查詢”菜單項(xiàng)添加監(jiān)聽(tīng)器
this.setVisible(true); //設(shè)置窗口的可見(jiàn)性
this.setSize(300,200); //設(shè)置窗口的大小
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//關(guān)閉窗口
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==m11) //處理“添加信息”事件
{
new AddStudent();
}
if(e.getSource()==m12) //處理“查詢信息”事件
{
new SelectStudent();
}
if(e.getSource()==m21) //處理“查詢成績(jī)”事件
{
new ChengJiStudent();
}
}
public static void main(String args[])
{ new StudentJieMian(); //創(chuàng)建一個(gè)對(duì)象 }
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
/**
* 我的計(jì)算器。Cheshi 繼承于 JFrame,是計(jì)算器的界面
c*/
public class Cheshi extends JFrame {
private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
private JTextField textbox = new JTextField("0");
private CalculatorCore core = new CalculatorCore();
private ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
String label = b.getText();
String result = core.process(label);
textbox.setText(result);
}
};
public Cheshi(String title) throws HeadlessException {
super(title); // 調(diào)用父類構(gòu)造方法
setupFrame(); // 調(diào)整窗體屬性
setupControls(); // 創(chuàng)建控件
}
private void setupControls() {
setupDisplayPanel(); // 創(chuàng)建文本面板
setupButtonsPanel(); // 創(chuàng)建按鈕面板
}
// 創(chuàng)建按鈕面板并添加按鈕
private void setupButtonsPanel() {
JPanel panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(4, 5, 3, 3));
createButtons(panel, new String[]{
"7", "8", "9", "+", "C",
"4", "5", "6", "-", "CE",
"1", "2", "3", "*", "", // 空字符串表示這個(gè)位置沒(méi)有按鈕
"0", ".", "=", "/", ""
});
this.add(panel, BorderLayout.CENTER);
}
/**
* 在指定的面板上創(chuàng)建按鈕
*
* @param panel 要?jiǎng)?chuàng)建按鈕的面板
* @param labels 按鈕文字
*/
private void createButtons(JPanel panel, String[] labels) {
for (String label : labels) {
// 如果 label 為空,則表示創(chuàng)建一個(gè)空面板。否則創(chuàng)建一個(gè)按鈕。
if (label.equals("")) {
panel.add(new JPanel());
} else {
JButton b = new JButton(label);
b.addActionListener(listener); // 為按鈕添加偵聽(tīng)器
panel.add(b);
}
}
}
// 設(shè)置顯示面板,用一個(gè)文本框來(lái)作為計(jì)算器的顯示部分。
private void setupDisplayPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(border);
setupTextbox();
panel.add(textbox, BorderLayout.CENTER);
this.add(panel, BorderLayout.NORTH);
}
// 調(diào)整文本框
private void setupTextbox() {
textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右對(duì)齊
textbox.setEditable(false); // 文本框只讀
textbox.setBackground(Color.white); // 文本框背景色為白色
}
// 調(diào)整窗體
private void setupFrame() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 當(dāng)窗體關(guān)閉時(shí)程序結(jié)束
this.setLocation(100, 50); // 設(shè)置窗體顯示在桌面上的位置
this.setSize(300, 200); // 設(shè)置窗體大小
this.setResizable(false); // 窗體大小固定
}
// 程序入口
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Cheshi frame = new Cheshi("我的計(jì)算器");
frame.setVisible(true); // 在桌面上顯示窗體
}
}
/**
* 計(jì)算器核心邏輯。這個(gè)邏輯只能處理 1~2 個(gè)數(shù)的運(yùn)算。
*/
class CalculatorCore {
private String displayText = "0"; // 要顯示的文本
boolean reset = true;
private BigDecimal number1, number2;
private String operator;
private HashMapString, Operator operators = new HashMapString, Operator();
private HashMapString, Processor processors = new HashMapString, Processor();
CalculatorCore() {
setupOperators();
setupProcessors();
}
// 為每種命令添加處理方式
private void setupProcessors() {
processors.put("[0-9]", new Processor() {
public void calculate(String command) {
numberClicked(command);
}
});
processors.put("\\.", new Processor() {
public void calculate(String command) {
dotClicked();
}
});
processors.put("=", new Processor() {
public void calculate(String command) {
equalsClicked();
}
});
processors.put("[+\\-*/]", new Processor() {
public void calculate(String command) {
operatorClicked(command);
}
});
processors.put("C", new Processor() {
public void calculate(String command) {
clearClicked();
}
});
processors.put("CE", new Processor() {
public void calculate(String command) {
clearErrorClicked();
}
});
}
// 為每種 operator 添加處理方式
private void setupOperators() {
operators.put("+", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.add(number2);
}
});
operators.put("-", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.subtract(number2);
}
});
operators.put("*", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.multiply(number2);
}
});
operators.put("/", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.divide(number2, 30, RoundingMode.HALF_UP);
}
});
}
// 根據(jù)命令處理。這里的命令實(shí)際上就是按鈕文本。
public String process(String command) {
for (String pattern : processors.keySet()) {
if (command.matches(pattern)) {
processors.get(pattern).calculate(command);
break;
}
}
return displayText;
}
// 當(dāng)按下 CE 時(shí)
private void clearErrorClicked() {
if (operator == null) {
number1 = null;
} else {
number2 = null;
}
displayText = "0";
reset = true;
}
// 當(dāng)按下 C 時(shí),將計(jì)算器置為初始狀態(tài)。
private void clearClicked() {
number1 = null;
number2 = null;
operator = null;
displayText = "0";
reset = true;
}
// 當(dāng)按下 = 時(shí)
private void equalsClicked() {
calculateResult();
number1 = null;
number2 = null;
operator = null;
reset = true;
}
// 計(jì)算結(jié)果
private void calculateResult() {
number2 = new BigDecimal(displayText);
Operator oper = operators.get(operator);
if (oper != null) {
BigDecimal result = oper.process(number1, number2);
displayText = result.toString();
}
}
// 當(dāng)按下 +-*/ 時(shí)(這里也可以擴(kuò)展成其他中間操作符)
private void operatorClicked(String command) {
if (operator != null) {
calculateResult();
}
number1 = new BigDecimal(displayText);
operator = command;
reset = true;
}
// 當(dāng)按下 . 時(shí)
private void dotClicked() {
if (displayText.indexOf(".") == -1) {
displayText += ".";
} else if (reset) {
displayText = "0.";
}
reset = false;
}
// 當(dāng)按下 0-9 時(shí)
private void numberClicked(String command) {
if (reset) {
displayText = command;
} else {
displayText += command;
}
reset = false;
}
// 運(yùn)算符處理接口
interface Operator {
BigDecimal process(BigDecimal number1, BigDecimal number2);
}
// 按鈕處理接口
interface Processor {
void calculate(String command);
}
}
java編程實(shí)現(xiàn)分?jǐn)?shù)的加減乘除運(yùn)算的步驟如下:
1、打開(kāi)eclipse,創(chuàng)建一個(gè)Java工程,在此工程里新建一個(gè)類;
2、在新建的類中,添加4個(gè)運(yùn)算類;
3、在主方法中調(diào)用對(duì)應(yīng)的方法即可完成分?jǐn)?shù)的加減乘除運(yùn)算了。
具體實(shí)現(xiàn)代碼如下:
public?class?Demo?{
public?static?void?main(String[]?args)?{
System.out.println(jia(1,?2));
System.out.println(jian(1,?2));
System.out.println(cheng(1,?2));
System.out.println(chu(1,?2));
}
//加法運(yùn)算
private?static?float?jia(float?x,float?y)?{
return?x?+?y;
}
//減法運(yùn)算
private?static?float?jian(float?x,float?y)?{
return?x?-?y;
}
//乘法運(yùn)算
private?static?float?cheng(float?x,float?y)?{
return?x?*?y;
}
//除法運(yùn)算
private?static?float?chu(float?x,float?y)?{
return?x?/?y;
}
}
新聞名稱:java計(jì)分代碼,分?jǐn)?shù)的源碼怎么計(jì)算
標(biāo)題鏈接:http://chinadenli.net/article16/hechdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、品牌網(wǎng)站制作、標(biāo)簽優(yōu)化、小程序開(kāi)發(fā)、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)