當前位置:首頁 » 編程語言 » java樣例

java樣例

發布時間: 2022-12-27 21:01:30

⑴ 求使用java在word中添加圖片的樣例代碼,最好能添加到指定位置,如果使用poi最好。急急急!!!

首先你得打開這個word文檔,然後你再把這個圖片做成文件流的格式,然後把它寫進去。
但是這個過程如果你用java自帶的文件流格式打開的話,寫進去會是亂碼。我做過的方法是用開源框架包,poi.jar可以用來做有關office的打開、寫入、讀出等操作,具體代碼沒有了,但是demo裡面的代碼都已經足夠了,你自己稍微看看就能知道怎麼做了。這個包還是比較穩定的。 求採納

⑵ java怎麼得到json中的數據

如果不是Android開發環境的話,首先需要引入處理JSON數據的包:json-lib-2.2.3-jdk15.jar


Java樣常式序如下:

importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;

publicclassDoJSON{
publicstaticvoidmain(String[]args){
JSONArrayemployees=newJSONArray(); //JSON數組
JSONObjectemployee=newJSONObject(); //JSON對象

employee.put("firstName","Bill"); //按「鍵-值」對形式存儲數據到JSON對象中
employee.put("lastName","Gates");
employees.add(employee); //將JSON對象加入到JSON數組中

employee.put("firstName","George");
employee.put("lastName","Bush");
employees.add(employee);

employee.put("firstName","Thomas");
employee.put("lastName","Carter");
employees.add(employee);

System.out.println(employees.toString());
for(inti=0;i<employees.size();i++){
JSONObjectemp=employees.getJSONObject(i);
System.out.println(emp.toString());
System.out.println("FirstName: "+emp.get("firstName"));
System.out.println("LastName: "+emp.get("lastName"));
}
}
}


運行效果:

[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"},{"firstName":"Thomas","lastName":"Carter"}]

{"firstName":"Bill","lastName":"Gates"}

FirstName : Bill

LastName : Gates

{"firstName":"George","lastName":"Bush"}

FirstName : George

LastName : Bush

{"firstName":"Thomas","lastName":"Carter"}

FirstName : Thomas

LastName : Carter

⑶ 求n以內的親密數對(Java編程),輸入輸出樣例已有

按照你的要求編寫的求n以內的親密數對的Java程序如下

importjava.util.Scanner;
publicclassCCC{
publicstaticvoidmain(String[]args){
inta,b,i,n,num;
System.out.println("請輸入一個正整數");
Scannersc=newScanner(System.in);
n=sc.nextInt();
for(a=1;a<n;a++)
{
for(b=0,i=1;i<=a/2;i++)
if(a%i==0)b+=i;
for(num=0,i=1;i<=b/2;i++)
if(b%i==0)num+=i;
if(num==a&&a<b)
System.out.println(a+","+b);
}
}
}

運行結果

請輸入一個正整數
3000
220,284
1184,1210
2620,2924

⑷ Java多線程Socket操作猜數游戲樣例

伺服器端程序 接受連接並處理客戶端的請求 ServerApp java package test; import java io *; import *; /** *//** * 伺服器端程序 * * @author luxuan 修正趙學慶 * */ public class ServerApp { static final int PORT = ; private ServerSocket serverSocket; private Socket socket; private BufferedReader netIn; private PrintWriter netOut; public ServerApp() throws IOException { serverSocket = new ServerSocket(PORT); System out println( server start ); while (true) { // 等待連接 socket = serverSocket accept(); ServerThread st = new ServerThread(socket); new Thread(st) start(); } } class ServerThread implements Runnable { private Socket socket; private int randomNumber; private int clientGuessNumber; public ServerThread(Socket s) throws IOException { socket = s; netIn = new BufferedReader(new InputStreamReader(socket getInputStream())); netOut = new PrintWriter(socket getOutputStream()); } public void run() { System out println( client + socket getInetAddress() + is connect ); randomNumber = (int) (Math random() * ); System out println( random number is: + randomNumber); try { clientGuessNumber = Integer parseInt(netIn readLine()); System out println( client guess number is: + clientGuessNumber); for (int i = ; i > ; i ) { if (clientGuessNumber == randomNumber) { netOut println( OK 恭喜 猜對了 ); ClientApp finished = true; } else if (clientGuessNumber < randomNumber) { netOut println( 您猜的數小了 您還有 + (i ) + 次機會 ); ClientApp finished = false; } else if (clientGuessNumber > randomNumber) { netOut println( 您猜的數大了 您還有 + (i ) + 次機會 ); ClientApp finished = false; } netOut flush(); if (!ClientApp finished) { clientGuessNumber = Integer parseInt(netIn readLine()); } else { break; } } if (!ClientApp finished) { netOut println( OK 您沒有機會了 游戲結束 ); } ClientApp finished = true; } catch (IOException e) { } finally { try { netOut close(); netIn close(); socket close(); } catch (IOException ee) { } } } } public static void main(String[] args) throws IOException { new ServerApp(); } } 客戶端程序 發送每次猜的數字 package test; import java io *; import *; public class ClientApp { private Socket socket; private BufferedReader netIn; private PrintWriter netOut; private BufferedReader keyboardIn; static Boolean finished = false; public ClientApp() throws IOException { System out println( 請輸入伺服器地址 連接本地伺服器請輸入localhost ); keyboardIn = new BufferedReader(new InputStreamReader(System in)); try { if (keyboardIn readLine() equalsIgnoreCase( localhost )) { socket = new Socket(InetAddress getLocalHost() ServerApp PORT); } else { socket = new Socket(InetAddress getByName(keyboardIn readLine()) ServerApp PORT); } netIn = new BufferedReader(new InputStreamReader(socket getInputStream())); netOut = new PrintWriter(socket getOutputStream()); } catch (UnknownHostException e) { System err println( 連接不到伺服器 ); System exit( ); } System out println( 連接成功 ); while (!finished) { System out println( 請輸入 - 之間的數字 ); netOut println(keyboardIn readLine()); netOut flush(); String str = netIn readLine(); if (str == null) { finished = true; break; } System out println(str); if (str startsWith( OK )) { finished = true; break; } } netIn close(); netOut close(); keyboardIn close(); socket close(); } public static void main(String[] args) throws IOException { new ClientApp(); } } 運行結果

lishixin/Article/program/Java/gj/201311/27387

⑸ 用Java 生成一個長度為40的、完全由16進制數組成的隨機字元串的方法:

用java.util.UUID 可以實現這個。
下面是樣例 :
ss[0]=====4cdbc040-657a-4847-b266-7e31d9e2c3d9,
ss[1]=====72297c88-4260-4c05-9b05-d28bfb11d10b,
ss[2]=====6d513b6a-69bd-4f79-b94c-d65fc841ea95,
ss[3]=====d897a7d3-87a3-4e38-9e0b-71013a6dbe4c,

⑹ 用JAVA編寫一個簡單的計算器,要求如下

太麻煩了 說個思路, 過去字元串後 先獲取+-*%的下標.然後然後分割,獲取到一個數組或list
然後循環獲取() 按照數學運算順序拼起來,
然後把公式拆分一步一步操作就得出結果啦

⑺ java document調用Webservice服務端樣例

你好
可以參考這篇文章
http://wenku..com/link?url=nfYM-UCY5ISznldgJHvw-lehm8kCalrgMs9P7

⑻ java數組元素求和,求最大值和最小值樣例輸入 10 1 2 3 4 5 6 7 8 9 10 樣例輸出 55 10 1

初始化max和min時有問題

max=a[0]
min=a[0]
你的源代碼中min=0,max=0
但是你輸入的是123456789 10,min就肯定會為0,因為你初始化時的min比你輸入的數據都要小

還有你的兩個if語句中賦值都反掉了

兩個if語句改成

if(max<=a[i])
{
max=a[i];

}
if(min>=a[i])
{
min=a[i]

}

⑼ 用java寫出來

importjava.util.Scanner;
classPrintNumber{
publicstaticvoidmain(String[]args){
Scannerin=newScanner(System.in);
System.out.print("請輸入一個整數(1-50):");
intnum=in.nextInt();
while(num<1||num>50){
System.out.print("輸入的數字超出范圍:請重新輸入:");
num=in.nextInt();
}
printNum(num);



}
//明確返回值類型:void
//明確參數列表:intn
publicstaticvoidprintNum(intn){
for(intx=1;x<=n;x++){
System.out.print(x+"");
}
for(intx=n-1;x>0;x--){
if(x==1){
System.out.print(x);
}else{
System.out.print(x+"");
}

}
}

}

⑽ (java程序題)輸入四個數字的加減乘除表達式, 輸出運算結果 樣例輸入 6.5-4*3+3 樣例輸出 -2.5

這道題我使用的是棧來做的,如果是JDK1.6以上可以使用Java調用JavaScript做,這樣代碼量可以減少不少
import java.util.Scanner;
import java.util.Stack;
public class test{
public static void TrnsInToSufix(String IFX,String []PFX)//PFX放後綴表達式,IFX為中綴表達式
{
StringBuffer numBuffer = new StringBuffer();// 用來保存一個數的
Stack<String> s=new Stack<String>();//放操作符
String a;
s.push("=");//第一個為等號
int i=0,j=0;
char ch;
for(i=0;i<IFX.length();)
{
ch=IFX.charAt(i);
switch(ch)
{
case '0':case '1':case '2':
case '3':case '4':case '5':
case '6':case '7':case '8':
case '9':
while(Character.isDigit(ch)||ch=='.')//拼數
{
numBuffer.append(ch); // 追加字元
ch = IFX.charAt(++i);
}
PFX[j++]=numBuffer.toString();//break;
numBuffer = new StringBuffer(); //清空已獲取的運算數字
continue; //這里要重新循環,因為i已經增加過了
case '(':
s.push("(");break;
case ')':
while(s.peek()!="(")
PFX[j++]=s.pop();
break;
case '+':
case '-':
while(s.size()>1&&s.peek()!="(")
PFX[j++]=s.pop();
a=String.valueOf(ch);
s.push(a);break;
case '*':
case '/':
while(s.size()>1&&(s.peek()=="*")||s.peek()=="/"
||s.peek()=="s"||s.peek()=="c"||s.peek()=="t"
||s.peek()=="^"||s.peek()=="√")//優先順序比較,與棧頂比較,
PFX[j++]=s.pop();//當前操作符優先順序大於等於棧頂的彈出棧頂
a=String.valueOf(ch);
s.push(a);break;
case 's':
case 'c':
case 't'://三角函數
while(s.size()>1&&(s.peek()=="s"||s.peek()=="c"||s.peek()=="t"
||s.peek()=="^"||s.peek()=="√"))//優先順序比較,與棧頂,大於等於的彈出
PFX[j++]=s.pop();
a=String.valueOf(ch);
s.push(a);break;
case '^':// 冪
case '√':// 開方
while(s.size()>1&&(s.peek()=="^"||s.peek()=="√"))
PFX[j++]=s.pop();
a=String.valueOf(ch);
s.push(a);break;
}
i++;
}
while(s.size()>1)
PFX[j++]=s.pop();
PFX[j]="=";
}
public static String Evaluate (String []PFX)//後綴表達式求值
{
int i=0;
double x1,x2,n;
String str;
Stack<String> s= new Stack<String>();
while(PFX[i]!="=")
{
str=PFX[i];
switch(str.charAt(0))
{
case '0':case '1':case '2':
case '3':case '4':case '5':
case '6':case '7':case '8':
case '9':
s.push(str);break;
case '+':
x1=Double.parseDouble(s.pop());
x2=Double.parseDouble(s.pop());
n=x1+x2;
s.push(String.valueOf(n));break;
case '-':
x1=Double.parseDouble(s.pop());
x2=Double.parseDouble(s.pop());
n=x2-x1;
s.push(String.valueOf(n));break;
case '*':
x1=Double.parseDouble(s.pop());
x2=Double.parseDouble(s.pop());
n=x1*x2;
s.push(String.valueOf(n));break;
case '/':
x1=Double.parseDouble(s.pop());
x2=Double.parseDouble(s.pop());
n=x2/x1;
s.push(String.valueOf(n));break;
case 's':
x1=Double.parseDouble(s.pop());
n=Math.sin(x1 * Math.PI / 180);
s.push(String.valueOf(n));break;
case 'c':
x1=Double.parseDouble(s.pop());
n=Math.cos(x1 * Math.PI / 180);
s.push(String.valueOf(n));break;
case 't':
x1=Double.parseDouble(s.pop());
n=Math.tan(x1 * Math.PI / 180);
s.push(String.valueOf(n));break;
case '√':
x1=Double.parseDouble(s.pop());
n=Math.sqrt(x1);
s.push(String.valueOf(n));break;// 開方
case '^':
x1=Double.parseDouble(s.pop());
x2=Double.parseDouble(s.pop());
n=Math.pow(x2, x1);
s.push(String.valueOf(n));break;
}
i++;
}
return s.pop();
}
public static void main(String[] args) {
System.out.println("請輸入一個表達式:");
String s= new Scanner(System.in).next();//sin45*2,sin 要用string.replace轉換成 s
s+="=";
String[] PFX = new String[100];
TrnsInToSufix(s, PFX);
System.out.println("該算式的結果是:"+Evaluate(PFX));

}
}

熱點內容
黎明我的世界伺服器 發布:2024-05-19 17:17:34 瀏覽:538
雷神g50如何設置安卓原生模式 發布:2024-05-19 16:50:04 瀏覽:120
c語言小數四捨五入 發布:2024-05-19 16:23:28 瀏覽:525
資料庫被注入攻擊 發布:2024-05-19 16:21:31 瀏覽:835
微信忘記密碼從哪裡看 發布:2024-05-19 16:06:37 瀏覽:33
寶馬x4貸款買哪個配置好 發布:2024-05-19 15:56:03 瀏覽:23
微控pid演算法 發布:2024-05-19 15:46:31 瀏覽:136
雲盤視頻解壓密碼 發布:2024-05-19 15:23:17 瀏覽:848
和平精英怎麼改地區位置安卓 發布:2024-05-19 15:19:05 瀏覽:286
酒店的路由器如何配置 發布:2024-05-19 15:10:44 瀏覽:502