




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上第六章 Java輸入輸出1、說明程序功能:import java.io.*;public class CharWrite public static void main(String args) try FileWriter fw=new FileWriter("charset.txt "); for ( int i=32;i<126;i+) fw.write(i); fw.close(); catch (IOException e) 答:向文件charset.txt中輸入ASCII碼從32到126共94個字符。2、先用隨機函數(shù)產(chǎn)生出任意的2
2、0個整數(shù),再按由小到大的順序排序,然后將結(jié)果寫入一個文件中,最后從該文件中讀出后顯示出來。請分別用順序文件和隨機文件的讀寫形式進行編程測試。代碼:import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;im
3、port java.io.OutputStream;import java.util.Random;public class RandNum public static void main(String args) Random rand = new Random();/實例化一個產(chǎn)生隨機數(shù)的類 int num = new int20;/定義一個整型數(shù)組,用來存儲產(chǎn)生的隨機數(shù) int r = 0;/隨機數(shù) for(int i = 0;i<num.length;i+) r = rand.nextInt(1000);/產(chǎn)生一個1000以內(nèi)的隨機數(shù) /放入數(shù)組 numi = r; int k;
4、 /進行排序 for(int i = 0;i<num.length;i+) for(int j = 0;j<i;j+) if(numi < numj) k = numi; numi = numj; numj = k; RandNum rn = new RandNum(); /寫入文件 rn.WriteFile("src/123",num); /讀取文件 int x = rn.ReadFile("src/123"); /讀取的數(shù)組 for(int i = 0;i<num.length;i+) System.out.print(xi
5、+ "t"); if(i%5 = 4) System.out.println(); /* * 讀取文件的方法 * param path */ public int ReadFile(String path) int a = null; try / 實例化一個File輸入流對象InputStream is = new FileInputStream(path);/ 實例化一個Buffer輸入流對象BufferedInputStream bs = new BufferedInputStream(is);int length = bs.available();/讀取第一個為數(shù)組大
6、小int k = bs.read();/用來計數(shù)int count = 0;/實例化數(shù)組a = new intk;String str = ""/中間變量for(int i = 0;i<length;i+)/繼續(xù)讀取k = bs.read();/若讀取的不是換行if(k != 13 && k != 10 && k != -1)/讀取到空格while(k != 32)str += (char)k;k = bs.read();/把字符串里的數(shù)字轉(zhuǎn)換成整型放入數(shù)組中acount = Integer.parseInt(str);count+;s
7、tr = ""/清空字符串is.close();bs.close();System.out.println("讀取成功"); catch (FileNotFoundException e)System.out.println("找不到指定文件,請確認文件路徑"); catch (IOException e) e.printStackTrace(); return a; /* * 寫入文件 * param path 路徑 * param a 數(shù)組 */ public void WriteFile(String path,int a)tr
8、y / 實例化一個File輸出流OutputStream os = new FileOutputStream(path);/ 實例化一個Buffered輸出流BufferedOutputStream bos = new BufferedOutputStream(os);/寫入數(shù)組大小bos.write(a.length);/寫入換行符bos.write(13);bos.write(10);/循環(huán)寫入數(shù)組的值for(int i = 0;i<a.length;i+)bos.write(Integer.toString(ai).getBytes();/寫入1個空格bos.write(32);b
9、os.flush();bos.close();os.close();System.out.println("寫入成功"); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); 截圖3、統(tǒng)計一個文本文件中單詞的個數(shù)。文本文件的名字從命令行中獲得。代碼import java.io.FileReader;import java.io.IOException;import java.util.StringTokenizer;public cla
10、ss test public static void main(String args) throws IOException FileReader fileReader=new FileReader(args0); StringBuffer stringBuffer=new StringBuffer(); char b=new char506; while(fileReader.read(b,0,505)!=-1) stringBuffer.append(b); StringTokenizer stringTokenizer=new StringTokenizer(stringBuffer.
11、toString(); System.out.println("文本文件中單詞的個數(shù)為:"+stringTokenizer.countTokens(); 結(jié)果截圖第七章 Java圖形用戶界面1、閱讀下面的程序,回答問題。 import java.awt.*; import javax.swing.*; public class T extends JFrame public T ( ) super("GridLayout"); Container con=this.getContentPane(); con.setLayout(new GridLayou
12、t(2,3); con.add(new JButton("a"); con.add(new JButton("b"); con.add(new JButton("c"); con.add(new JButton("d"); con.add(new JButton("e"); con.add(new JButton("f"); setSize(200, 80); setVisible(true); public static void main(String args) new
13、 T(); 畫圖表示程序運行后的圖形界面。如果程序通過實現(xiàn)某個接口處理按鈕的動作事件,則該接口名為何?接口中的方法頭聲明如何?答:實現(xiàn)的接口是ActionListener,該接口的方法聲明是public void actionPerformed(ActionEvent e)2、編寫一個簡單的計算器,要求圖形用戶界面如下圖所示。代碼import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionL
14、istener;import java.util.HashSet;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class jisuanqi public static void main(String args) new MyCalculator(); class MyCalculator private double result=0.0; private String string=""
15、; private JTextField jtext = new JTextField("",15); String str=new String"Clear","+","0","1","2","3","-","*",".","4","5","6","/","%","=",&qu
16、ot;7","8","9" public MyCalculator() JFrame frame = new JFrame("calculator"); frame.setBounds(400,200,450,250); JPanel jp = new JPanel(new GridLayout(3,6,10,10); frame.setLayout(new BorderLayout(); frame.add(jtext,BorderLayout.NORTH); frame.add(jp,BorderLayout.CENTE
17、R); final HashSet hashSet=new HashSet(); class MyEvent implements ActionListener public void actionPerformed(ActionEvent e) for(Object k:hashSet) if(e.getSource()=k) JButton jb=(JButton)k; if(jb.getText()="Clear") jtext.setText(""); else jtext.setText(jtext.getText()+jb.getText()
18、; if(jb.getText()="=") char c=jtext.getText().toCharArray(); int i; for(i=0;i<c.length;i+) if(ci='+'|ci='-'|ci='*'|ci='/'|ci='%') break; String astr=String.copyValueOf(c, 0, i); String bstr=String.copyValueOf(c,i+1,c.length-i-2); MyCompute mycompu
19、te = new MyCompute(); result=pute(Double.parseDouble(astr),Double.parseDouble(bstr),ci); jtext.setText(""+result); for(int i=0;i<18;i+) JButton b=new JButton(stri); jp.add(b); b.addActionListener(new MyEvent(); hashSet.add(b); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.
20、setVisible(true); class MyCompute private double result=0.0; public MyCompute() public double compute(double a,double b,char c) if(c='+') result=a+b; if(c='-') result=a-b; if(c='*') result=a*b; if(c='/') result=a/b; if(c='%') result=a%b; return result; 3. 編程題:
21、請編寫一個簡單的用戶登錄程序。要求使用圖形用戶界面,* 用戶名和密碼假定均為java,且密碼輸入時全部顯示為星號(*)。 */代碼public class TheExam7_3 public static void main(String args) MyLand my=new MyLand(); class MyLand int a=0; public MyLand() JFrame frame = new JFrame(); frame.setBounds(450,300,370,220); Font font=new Font("宋體",Font.PLAIN,30);
22、 frame.setFont(font); frame.setLayout(new BorderLayout(); JPanel paneltop = new JPanel(); paneltop.add(new JLabel("用戶登陸",JLabel.CENTER); frame.add(paneltop,BorderLayout.NORTH); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel_1 = new JPanel(new Gri
23、dLayout(3,1); frame.add(panel_1,BorderLayout.CENTER); JPanel panel_11 = new JPanel(new FlowLayout(); JPanel panel_12 = new JPanel(new FlowLayout(); JPanel panel_13 = new JPanel(new GridLayout(1,5); panel_1.add(panel_11); panel_1.add(panel_12); panel_1.add(panel_13); panel_11.add(new JLabel("用戶名
24、:"); final JTextField text1 = new JTextField("",10); panel_11.add(text1); panel_12.add(new JLabel("用戶密碼:"); final JPasswordField text2 = new JPasswordField("",10); text2.setEchoChar('*'); panel_12.add(text2); JButton b=new JButton("確定"); class MyE
25、xam7_3 implements ActionListener public void actionPerformed(ActionEvent e) JDialog dialog=new JDialog(); dialog.setBounds(450, 200,560,400); dialog.setVisible(true); Font font1 = new Font("宋體",Font.BOLD,70); dialog.setFont(font1); if(text1.getText().equalsIgnoreCase("java")&
26、&String.valueOf(text2.getPassword().equals("java") dialog.add(new JLabel("登陸成功!",JLabel.CENTER); / new MyCalculator(); else dialog.add(new JLabel("密碼或用戶名錯誤!",JLabel.CENTER); b.addActionListener(new MyExam7_3(); panel_13.add(new Label(" "); panel_13.add(new
27、 Label(" "); panel_13.add(b); panel_13.add(new Label(" "); panel_13.add(new Label(" "); 結(jié)果截圖第八章 Java多線程技術(shù)1、程序如下:public class Borley extends Threadpublic static void main(String argv) Borley b = new Borley(); b.start(); public void run() System.out.println("Running&
28、quot;); 下面描述正確的是:(B)單選A) 通過編譯和運行但是沒有任何輸出。B) 通過編譯,運行后輸出"Running"C) 編譯出錯,沒有線程可供運行D) 編譯出錯,沒有權(quán)限使用Thread2、 編寫程序,啟動1000個線程,每個線程給初值為0的變量sum加1。(提示,需要通過引用傳遞,將sum傳遞給每個線程,為了能夠進行引用傳遞,需要定義一個Integer包裝對象來保存sum)。代碼public class Borley extends Threadpublic static void main(String argv)/通過Integer包裝對象sumInteg
29、er sum = new Integer(0);/循環(huán)1000次,開啟1000個線程for(int i = 0;i<1000;i+)Borley b = new Borley(sum);b.start();private Integer sum;public Borley(Integer sum)this.sum = sum;public void run() sum+;System.out.println(sum);截圖3、利用多線程技術(shù)模擬龜兔賽跑的場面。提示:先設(shè)計一個線程類模擬兩個參與賽跑的選手的行為;然后創(chuàng)建該類的兩個對象分別代表烏龜和兔子;讓兔子跑快些,但在路上睡眠休息時間長
30、些,而烏龜跑慢些卻不休息;當某個選手到達終點時其線程運行結(jié)束;二個選手均到達終點時,顯示并祝賀獲勝選手,然后主線程結(jié)束。代碼Import java.util.Date;public class xiancheng extends Thread private int tortoise_walk = 0; / 烏龜已跑長度存放變量 private int rabbit_walk = 0; / 兔子已跑長度存放變量 private int finish = 1000; / 終點 private volatile boolean hasWinner = false;/ 勝利者誕生 /* class
31、Tortoise_Run implements Runnable public void run() try while (!hasWinner) if (tortoise_walk % 100 = 0 && (tortoise_walk != 0|tortoise_walk>=finish) /烏龜每100米休息500毫秒 Thread.sleep(500); tortoise_walk+; System.out.println("烏龜已跑"+tortoise_walk+"米"); catch (InterruptedException e) e.printStackTrace(); class Rabbit_Run implements Runnable public void run() try while (!hasWinner) if (rabbit_walk % 20 = 0 && (rabbit_walk != 0|rabbit_walk>=finish) /兔子每20米休息500毫秒 System.out.pr
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 環(huán)保隱患排查制度
- 安全駕駛案例心得體會
- 煤礦個人安全責任承諾書
- 生產(chǎn)部的績效考核方案
- 2025年中國水罐消防車行業(yè)發(fā)展趨勢預測及投資戰(zhàn)略研究報告
- 燃氣事故專項應急預案
- 中國SIP封裝行業(yè)市場競爭格局及投資前景展望報告
- 2025年中國3D人臉識別器行業(yè)發(fā)展趨勢及投資前景預測報告
- 2021-2026年中國制糖行業(yè)發(fā)展監(jiān)測及投資戰(zhàn)略規(guī)劃研究報告
- 牛的飼養(yǎng)管理技術(shù)課件
- 2024年湖北省普通高中學業(yè)水平合格性考試數(shù)學試題(原卷版)
- 2025至2030年中國轎車輪轂造型線模具市場分析及競爭策略研究報告
- 2024年安徽中醫(yī)藥高等??茖W校招聘考試真題
- 2025屆吉林省長春市朝陽區(qū)英語八下期末學業(yè)水平測試模擬試題含答案
- 2022室外排水設(shè)施設(shè)計與施工-鋼筋混凝土化糞池22S702
- 中小學校長招聘考試試題
- 一級建造師繼續(xù)教育考試題(重點)
- 組合導航與融合導航解析課件
- 數(shù)與代數(shù)課件
- 工會審計實務(wù)課件
- 預防艾滋病、梅毒和乙肝母嬰傳播相關(guān)報表、上報流程和要求
評論
0/150
提交評論