当前位置:首页 » 编程语言 » 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));

}
}

热点内容
内置存储卡可以拆吗 发布:2025-05-18 04:16:35 浏览:336
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:378
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:612
java用什么软件写 发布:2025-05-18 03:56:19 浏览:32
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:107
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:944
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:742
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:803
网卡访问 发布:2025-05-18 03:35:04 浏览:511
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:372