新聞中心
有兩個(gè)正整數(shù)a和b,已知a*b=2048,求a丶b各為何值時(shí),a+b的值最小值。 用java語(yǔ)言
一個(gè)循環(huán),x*temp=2048(即是y)
公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出雙橋免費(fèi)做網(wǎng)站回饋大家。
然后x值不斷+1,相除后得出的結(jié)果就是temp(一個(gè)中間變量),
if(y % x == 0)這段是確保了偶數(shù),不會(huì)除基數(shù)
然后中間變量+x小于 a(本次x+1的值)+ b(本次乘積的令一個(gè)值)
然后就賦值a = x;
b = temp;
最后不符合x(chóng) y; 了,就退出循環(huán),然后就得出最小值了,
因?yàn)樵谘h(huán)過(guò)程中,a,b值是通過(guò)中間值變量temp不斷在變化的
求java小游戲源代碼
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盤上每個(gè)小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盤的尺寸 – 包括黑色的輪廓線 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的顏色為深綠色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖動(dòng)標(biāo)記 --當(dāng)用戶在棋子上按下鼠標(biāo)按鍵時(shí)設(shè)為true, // 釋放鼠標(biāo)按鍵時(shí)設(shè)為false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盤左上角的左方向坐標(biāo) int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盤左上角的上方向坐標(biāo) int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原點(diǎn)(左上角)的左方向坐標(biāo) int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原點(diǎn)(左上角)的上方向坐標(biāo) int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按鍵時(shí)的鼠標(biāo)坐標(biāo)與棋子矩形原點(diǎn)之間的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按鍵時(shí)的鼠標(biāo)坐標(biāo)與棋子矩形原點(diǎn)之間的上方向位移 int rely; // Width of applet drawing area. // applet繪圖區(qū)域的寬度 int width; // Height of applet drawing area. // applet繪圖區(qū)域的高度 int height; // Image buffer. // 圖像緩沖 Image imBuffer; // Graphics context associated with image buffer. // 圖像緩沖相關(guān)聯(lián)的圖形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 獲取applet繪圖區(qū)域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 創(chuàng)建圖像緩沖 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出圖像緩沖相關(guān)聯(lián)的圖形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盤的原點(diǎn),使棋盤在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原點(diǎn),使得棋子在第一行左數(shù)第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一個(gè)用來(lái)監(jiān)聽(tīng)鼠標(biāo)按鍵的按下和釋放事件的鼠標(biāo)監(jiān)聽(tīng)器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 獲取按鍵時(shí)的鼠標(biāo)坐標(biāo) int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按鍵時(shí)如果鼠標(biāo)位于可拖動(dòng)的棋子上方 // (也就是contains (x, y)返回true),則保存當(dāng)前 // 鼠標(biāo)坐標(biāo)與棋子的原點(diǎn)之間的距離(始終為正值)并且 // 將拖動(dòng)標(biāo)志設(shè)為true(用來(lái)表明正處在拖動(dòng)過(guò)程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 計(jì)算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍處于棋子范圍內(nèi)則返回true // CHECKERDIM / 2為半徑 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 當(dāng)鼠標(biāo)按鍵被釋放時(shí),如果inDrag已經(jīng)為true, // 則將其置為false(用來(lái)表明不在拖動(dòng)過(guò)程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一個(gè)用來(lái)監(jiān)聽(tīng)鼠標(biāo)拖動(dòng)事件的鼠標(biāo)運(yùn)動(dòng)監(jiān)聽(tīng)器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 計(jì)算棋子新的原點(diǎn)(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)沒(méi)有被 // 移出棋盤,則將之前計(jì)算的原點(diǎn) // (tmpox, tmpoy)賦值給永久性的原點(diǎn)(ox, oy), // 并且刷新顯示區(qū)域(此時(shí)的棋子已經(jīng)位于新坐標(biāo)上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子將要被拖動(dòng)的位置上繪制棋盤 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 繪制即將被拖動(dòng)的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 繪制圖像緩沖的內(nèi)容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 設(shè)置棋子陰影的顏色 g.setColor (Color.black); // Paint checker shadow. // 繪制棋子的陰影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 設(shè)置棋子顏色 g.setColor (Color.red); // Paint checker. // 繪制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 繪制棋盤輪廓線 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 繪制棋盤 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT調(diào)用了update()方法來(lái)響應(yīng)拖動(dòng)棋子時(shí)所調(diào)用的repaint()方法。該方法從 // Container類繼承的默認(rèn)實(shí)現(xiàn)會(huì)在調(diào)用paint()之前,將applet的繪圖區(qū)域清除 // 為背景色,這種繪制之后的清除就導(dǎo)致了閃爍。CheckerDrag重寫了update()來(lái) // 防止背景被清除,從而消除了閃爍。 public void update (Graphics g) { paint (g); }}
java 源代碼 基礎(chǔ)點(diǎn)的 謝謝
package com.regex;
import java.io.*;
import java.net.URLDecoder;
import java.util.regex.*;
public class Regex {
private int REMARK=0;
private int LOGIC=0;
private int PHYSIC=0;
boolean start=false;
/**
* @param args
*/
public static void main(String[] args) { //測(cè)試方法
// TODO Auto-generated method stub
Regex re=new Regex();
re.regCount("Regex.java");
System.out.println("remark Line: "+re.REMARK);
System.out.println("logic Line: "+re.LOGIC);
System.out.println("physic Line: "+re.PHYSIC);
}/**
* @author BlueDance
* @param s
* @deprecated count
*/
public void regCount(String s){
String url=null;
try {
url=URLDecoder.decode(this.getClass().getResource(s).getPath(),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
try {
BufferedReader br=new BufferedReader(new FileReader(new File(url)));
String s1=null;
while((s1=br.readLine())!=null){
PHYSIC++;
if(CheckChar(s1)==1){
REMARK++;
System.out.println("純注釋行:"+s1);
}
if(CheckChar(s1)==2){
LOGIC++;
REMARK++;
System.out.println("非純注釋行:"+s1);
}
if(CheckChar(s1)==3)
LOGIC++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
/**
*
* @param s
* @return int
* @version check s
*/
public int CheckChar(String s){
String s1=null;
if(s!=null)
s1=s.trim();
//System.out.println(regCheck(s1,re));
if(regCheck(s1,"(//.*)")) //判斷//開(kāi)頭的為純注釋行
return 1;
if(regCheck(s1,"(.*[;{})] *//.*)")) //判斷不是//開(kāi)頭的非純注釋行
return 2;
if(regCheck(s1,"(//*.*)")){ //判斷/*開(kāi)頭的純注釋行
start=true;
return 1;
}
if(regCheck(s1,"(.*[;{})]//*.*)")){ //判斷不是/*開(kāi)頭的非純注釋行
start=true;
return 2;
}
if(regCheck(s1,"(.* */*/)")){ //判斷*/結(jié)尾的純注釋行
start=false;
return 1;
}
if(regCheck(s1,"(.* */*/.*)")!strCheck(s1)){ //判斷不是*/結(jié)尾的非純注釋行
if(strCheck(s1)){
start=false;
return 2;
}
}
if(start==true) //狀態(tài)代碼,start即/*開(kāi)始時(shí)start=true*/結(jié)束時(shí)為false
return 1;
return 3;//ssssllll
}//aeee
/**
*
* @param s
* @param re
* @return boolean
*/
public boolean regCheck(String s,String re){ //正則表達(dá)試判斷方法
return Pattern.matches(re,s);
}
public boolean strCheck(String s){ //中間有*/的字符判斷 此方法最關(guān)鍵
if(s.indexOf("*/")0){
int count=0;
String y[]=s.split("/*/");
boolean boo[]=new boolean[y.length];
for (int i = 0; i y.length-1; i++) {
char c[]=y[i].toCharArray();
for (int j = 0; j c.length; j++) {
if(c[j]=='\\'c[j+1]=='"'){
count++;
}
}
if(count%2==0){
if(countNumber("\"",y[i])%2!=0){
boo[i]=true;
}else{
boo[i]=false;
}
}else{
if(countNumber("\"",y[i])%2==0){
boo[i]=true;
}else{
boo[i]=false;
}
}
}
for(int i=0;iboo.length;i++){
if(!boo[i])
return false;
}
return true;
}
return false;
}
public int countNumber(String s,String y){ //此方法為我前面寫的字符串出現(xiàn)次數(shù)統(tǒng)計(jì)方法,不懂的可以看我前面的文章
int count=0;
String [] k=y.split(s);
if(y.lastIndexOf(s)==(y.length()-s.length()))
count=k.length;
else
count=k.length-1;
if(count==0)
System.out.println ("字符串\""+s+"\"在字符串\""+y+"\"沒(méi)有出現(xiàn)過(guò)");
else
return count;
return -1;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GoodLucky extends JFrame implements ActionListener{
JTextField tf = new JTextField(); //實(shí)例化一個(gè)文本域
//設(shè)置兩個(gè)按鈕
JButton b1 = new JButton("開(kāi)始");
JButton b2 = new JButton("停止");
boolean isGo = false;
//構(gòu)造函數(shù)
public GoodLucky(){
b1.setActionCommand("start");//在開(kāi)始按鈕上設(shè)置一個(gè)動(dòng)作監(jiān)聽(tīng) start
JPanel p = new JPanel(); //實(shí)例化一個(gè)可視化容器
//將兩個(gè)按鈕添加到可視化容器上面,用add方法
p.add(b1);
p.add(b2);
//在兩個(gè)按鈕上增加監(jiān)聽(tīng)的屬性,自動(dòng)調(diào)用下面的監(jiān)聽(tīng)處理方法actionPerformed(ActionEvent e),如果要代碼有更好的可讀性,可用內(nèi)部類實(shí)現(xiàn)動(dòng)作
//監(jiān)聽(tīng)處理。
b1.addActionListener(this);
b2.addActionListener(this);
//將停止按鈕設(shè)置為不可編輯(即不可按的狀態(tài))
b2.setEnabled(false);
this.getContentPane().add(tf,"North"); //將上面的文本域放在面板的北方,也就是上面(上北下南左西右東)
this.getContentPane().add(p,"South"); //將可視化容器pannel放在南邊,也就是下面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置用戶在此窗體上發(fā)起 "close" 時(shí)默認(rèn)執(zhí)行的操作,參數(shù)EXIT_ON_CLOSE是使用 System exit 方法退出應(yīng)用程序。僅在應(yīng)用程序中使用
this.setSize(300,200); //設(shè)置面板大小,寬和高
this.setLocation(300,300); //設(shè)置面板剛開(kāi)始的出現(xiàn)的位置
Cursor cu = new Cursor(Cursor.HAND_CURSOR); //用指定名稱創(chuàng)建一個(gè)新的定制光標(biāo)對(duì)象,參數(shù)表示手狀光標(biāo)類型
this.setCursor(cu); //為指定的光標(biāo)設(shè)置光標(biāo)圖像,即設(shè)置光標(biāo)圖像為上面所創(chuàng)建的手狀光標(biāo)類型
this.setVisible(true); //將面板可視化設(shè)置為true,即可視,如果為false,即程序運(yùn)行時(shí)面板會(huì)隱藏
tf.setText("welcome you! "); //設(shè)置面板的標(biāo)題為歡迎
this.go(); //調(diào)用go方法
}
public void go(){
while(true){ //這里是死循環(huán),也就是說(shuō)用戶不點(diǎn)擊停止按鈕的話他一直循環(huán)出現(xiàn)隨機(jī)數(shù),直到用戶點(diǎn)擊停止按鈕循環(huán)才能推出,具體流程在actionPerformed方法中控制。
if(isGo == true){ //上面所定義的isGo的初始值為false,所以程序第一次到此會(huì)跳過(guò)
String s = ""; //設(shè)置空字符串
for(int j = 1; j = 7;j++){ //產(chǎn)生7個(gè)隨機(jī)數(shù)
int i = (int)(Math.random() * 36) + 1;//每個(gè)隨機(jī)數(shù)產(chǎn)生方式,這里定義靈活,可以自由定義隨機(jī)數(shù)產(chǎn)生的方式
if(i 10){
s = s + " 0" + i; //如果產(chǎn)生的隨機(jī)數(shù)小于10的話做處理:這里就牽扯到一個(gè)重要的概念,簡(jiǎn)單敘述一下:
/*
當(dāng)一個(gè)字符串與一個(gè)整型數(shù)項(xiàng)相加的意思是連接,上面的s = s + " 0" + i的意思是字符串s鏈接0再連接整型i值,而不會(huì)導(dǎo)致0和整型的i相加,
產(chǎn)生的效果為s0i,由于s為空字符串(上面定義過(guò)的),所以當(dāng)i小于零時(shí),在個(gè)位數(shù)前面加上0,比如產(chǎn)生的隨機(jī)數(shù)i為7的話,顯示效果為 07.
*/
}else{
s = s + " " + i; //如果產(chǎn)生的隨機(jī)數(shù)比10打的話,那么加上空格顯示,即數(shù)字和數(shù)字之間有個(gè)空格
}
//以上循環(huán)循環(huán)七次,以保證能出現(xiàn)7個(gè)隨機(jī)數(shù)
}
tf.setText(s); //將產(chǎn)生的隨機(jī)數(shù)全部顯示在文本域上,用文本域?qū)ο髏f調(diào)用它的設(shè)置文本的方法setText(String)實(shí)現(xiàn)。
}
//以下為線程延遲
try{
Thread.sleep(10); //線程類同步方法sleep,睡眠方法,括號(hào)里的單位為ms。
}catch(java.lang.InterruptedException e){
e.printStackTrace(); //異常捕獲,不用多說(shuō)。
}
}
}
//以下是上面設(shè)置的事件監(jiān)聽(tīng)的具體處理辦法,即監(jiān)聽(tīng)時(shí)間處理方法,自動(dòng)調(diào)用
public void actionPerformed(ActionEvent e){ //傳入一個(gè)動(dòng)作事件的參數(shù)e
String s = e.getActionCommand(); //設(shè)置字符串s來(lái)存儲(chǔ)獲得動(dòng)作監(jiān)聽(tīng),上面的start
/*
以下這個(gè)條件語(yǔ)句塊的作用為:用戶點(diǎn)擊開(kāi)始后(捕獲start,用方法getActionCommand()),將命令觸發(fā)設(shè)置為true,從而執(zhí)行上面的go方法中的循環(huán)體(因?yàn)檠h(huán)體中要求isGo參數(shù)為true,而初始為false)。
執(zhí)行循環(huán)快產(chǎn)生隨機(jī)數(shù),并將開(kāi)始按鈕不可編輯化,而用戶只可以使用停止按鈕去停止。如果用戶按下停止時(shí),也就是沒(méi)有傳入?yún)?shù)“start”的時(shí)候,
執(zhí)行else語(yǔ)句塊中的語(yǔ)句,isGo設(shè)置為false,將不執(zhí)行上面go中的循環(huán)語(yǔ)句塊,從而停止產(chǎn)生隨機(jī)數(shù),并顯示,并且把開(kāi)始按鈕設(shè)置為可用,而把
停止按鈕設(shè)置為不可用,等待用戶按下開(kāi)始再去開(kāi)始新一輪循環(huán)產(chǎn)生隨機(jī)數(shù)。
*/
if(s.equals("start")){ //如果捕獲到start,也就是用戶觸發(fā)了動(dòng)作監(jiān)聽(tīng)器,那么下面處理
isGo = true; //設(shè)置isGo為true
b1.setEnabled(false); //將開(kāi)始按鈕設(shè)置為不可用
b2.setEnabled(true); //將停止按鈕設(shè)置為可用
}else{
isGo = false; //將isGo設(shè)置為false,isGo為循環(huán)標(biāo)志位
b2.setEnabled(false); //設(shè)置停止按鈕為不可用(注意看是b2,b2是停止按鈕)
b1.setEnabled(true); //設(shè)置開(kāi)始按鈕為可用
}
}
public static void main(String[] args){
new GoodLucky(); //產(chǎn)生類的實(shí)例,執(zhí)行方法
}
}
java里怎樣把文件轉(zhuǎn)換成二進(jìn)制
轉(zhuǎn)換文件成為二進(jìn)制數(shù)據(jù)并保存的Java代碼:
取出數(shù)據(jù)并還原文件到本地的java代碼:
[java]?view plain?copy//讀取數(shù)據(jù)庫(kù)二進(jìn)制文件
public?void?readerJpg()?throws?SQLException
{
connection=connectionManager.getconn();//自己連接自己的數(shù)據(jù)庫(kù)
String?sqlString="select?images?from?save_image?where?id=4";//從數(shù)據(jù)庫(kù)中讀出要還原文件的二進(jìn)制碼,這里我讀的是自己的數(shù)據(jù)庫(kù)id為4的文件
File?file=new?File("E:\\1.jpg");//本地生成的文件
if(!file.exists())
{
try?{
file.createNewFile();
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
try?{
byte[]?Buffer?=?new?byte[4096*5];
statement=connection.prepareStatement(sqlString);
resultSet?=?statement.executeQuery();
if(resultSet.next())
{
FileOutputStream?outputStream?=?new?FileOutputStream(file);
InputStream?iStream?=?resultSet.getBinaryStream("images");//去字段用getBinaryStream()
int?size=0;
while((size=iStream.read(Buffer))!=-1)
{
System.out.println(size);
outputStream.write(Buffer,0,size);
}
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
新聞名稱:包含2048java源代碼的詞條
轉(zhuǎn)載來(lái)源:http://fisionsoft.com.cn/article/phpdhi.html