具體代碼如下:
創(chuàng)新互聯(lián)建站主營尼元陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件開發(fā)公司,尼元陽h5成都小程序開發(fā)搭建,尼元陽網(wǎng)站營銷推廣歡迎尼元陽等地區(qū)企業(yè)咨詢
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator ?extends JFrame implements ActionListener ?{
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//對圖形組件實(shí)例化
jf=new JFrame("任靜的計(jì)算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件監(jiān)聽
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件處理
public void actionPerformed(ActionEvent e) {
//在這里完成事件處理 ?使計(jì)算器可以運(yùn)行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋體",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
最簡單的java代碼肯定就是這個了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個代碼了。如果是零基礎(chǔ)的新手朋友們可以來我們的java實(shí)驗(yàn)班試聽,有免費(fèi)的試聽課程幫助學(xué)習(xí)java必備基礎(chǔ)知識,有助教老師為零基礎(chǔ)的人提供個人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評團(tuán)進(jìn)行專業(yè)測試,幫助測評學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費(fèi)幫助來報名體驗(yàn)實(shí)驗(yàn)班的新手快速入門java,更好的學(xué)習(xí)java!
具體如下:
連連看的小源碼
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數(shù)組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕
JLabel fractionLable=new JLabel("0"); //分?jǐn)?shù)標(biāo)簽
JButton firstButton,secondButton; //
分別記錄兩次62616964757a686964616fe59b9ee7ad9431333335326239被選中的按鈕
int grid[][] = new int[8][7];//儲存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標(biāo)
int i,j,k,n;//消除方法控制
代碼(code)是程序員用開發(fā)工具所支持的語言寫出來的源文件,是一組由字符、符號或信號碼元以離散形式表示信息的明確的規(guī)則體系。
對于字符和Unicode數(shù)據(jù)的位模式的定義,此模式代表特定字母、數(shù)字或符號(例如 0x20 代表一個空格,而 0x74 代表字符“t”)。一些數(shù)據(jù)類型每個字符使用一個字節(jié);每個字節(jié)可以具有 256 個不同的位模式中的一個模式。
在計(jì)算機(jī)中,字符由不同的位模式(ON 或 OFF)表示。每個字節(jié)有 8 位,這 8 位可以有 256 種不同的 ON 和 OFF 組合模式。對于使用 1 個字節(jié)存儲每個字符的程序,通過給每個位模式指派字符可表示最多 256 個不同的字符。2 個字節(jié)有 16 位,這 16 位可以有 65,536 種唯一的 ON 和 OFF 組合模式。使用 2 個字節(jié)表示每個字符的程序可表示最多 65,536 個字符。
單字節(jié)代碼頁是字符定義,這些字符映射到每個字節(jié)可能有的 256 種位模式中的每一種。代碼頁定義大小寫字符、數(shù)字、符號以及 !、@、#、% 等特殊字符的位模式。每種歐洲語言(如德語和西班牙語)都有各自的單字節(jié)代碼頁。
雖然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代碼頁中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代碼頁中卻不同。如果在運(yùn)行不同代碼頁的計(jì)算機(jī)間交換數(shù)據(jù),必須將所有字符數(shù)據(jù)由發(fā)送計(jì)算機(jī)的代碼頁轉(zhuǎn)換為接收計(jì)算機(jī)的代碼頁。如果源數(shù)據(jù)中的擴(kuò)展字符在接收計(jì)算機(jī)的代碼頁中未定義,那么數(shù)據(jù)將丟失。
如果某個數(shù)據(jù)庫為來自許多不同國家的客戶端提供服務(wù),則很難為該數(shù)據(jù)庫選擇這樣一種代碼頁,使其包括所有客戶端計(jì)算機(jī)所需的全部擴(kuò)展字符。而且,在代碼頁間不停地轉(zhuǎn)換需要花費(fèi)大量的處理時間。
使用循環(huán)結(jié)構(gòu),通過Scanner類從控制臺輸入每個人的捐款金額,并累加每個人的捐款金額,計(jì)算捐款總數(shù)和捐款人數(shù),如果捐款總數(shù)達(dá)到10萬元,則計(jì)算平均每人捐款金額。
下面是通過輸入進(jìn)行統(tǒng)計(jì),實(shí)際過程中還能是通過數(shù)據(jù)庫進(jìn)行操作,或者表格收集數(shù)據(jù)進(jìn)行導(dǎo)入關(guān)系數(shù)據(jù)庫進(jìn)行統(tǒng)計(jì)。還可以通過接入微信或者支付寶小程序收款實(shí)時統(tǒng)計(jì)。
已經(jīng)有人給過隨機(jī)數(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ù)已達(dá)到10萬元,捐款人數(shù)為" + num + ",平均每人捐款金額為" + avg + "元。");
} else {
System.out.println("征集慈善募捐失敗,募捐總數(shù)未達(dá)到10萬元。");
}
scanner.close();
}
}
網(wǎng)站名稱:java小代碼 java編程代碼
文章來源:http://muchs.cn/article32/ddcghsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、品牌網(wǎng)站建設(shè)、定制開發(fā)、App開發(fā)、面包屑導(dǎo)航、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)