java999,java官方网站
这篇文章给大家聊聊关于java999,以及java官方网站对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。
急求java编“加减乘除”功能的程序
import java.util.*;
class Calculator{
private double result;
private double precision= 0.0001;//精准度
public static void main(String args []){
Calculator clerk=new Calculator();
try{
System.out.println("请按格式输入符号空格数字");
System.out.println("例如:+ 3");
System.out.println("如果想结束输入e");
System.out.println("清零请按c");
clerk.doCalculation();
}
catch(ArithmeticException e){
clerk.handleArithmeticException(e);//抓住后调用方法
}
catch(UnknownOpException e){//catch自定义异常
clerk.handleUnknownOpException(e);
}
System.out.println("最终的结果是"+clerk.getValue());
System.out.println("计算器计算结束");
}
public Calculator(){
result= 0;
}
public void reset(){
result=0;
}
public void setResult(double newResult){//setResult没用到
result=newResult;
}
public double getValue(){
return result;
}
public void doCalculation() throws ArithmeticException,UnknownOpException
{
char nextOp;
double nextNumber;
Scanner keyboard= new Scanner(System.in);
boolean done=false;
System.out.println("结果="+result);
while(!done){
nextOp=(keyboard.next()).charAt(0);//按位置返回字符
if((nextOp=='e')||(nextOp=='E'))
done=true;
else if((nextOp=='c')||(nextOp=='c')){//按位置返回字符
reset();
System.out.println("结果已清0");
}
else{
nextNumber=keyboard.nextDouble();
result=evaluate(nextOp,result,nextNumber);
System.out.println("结果"+ nextOp+""+nextNumber+"="+result);
System.out.println("最终的结果="+ result);
}
}
}
public double evaluate(char op,double n1,double n2) throws ArithmeticException,UnknownOpException
{
double answer=0;
switch(op){
case'+':
answer=n1+n2;
break;
case'-':
answer=n1-n2;
break;
case'*':
answer=n1*n2;
break;
case'/':
if((-precision<n2)&&(n2<precision))
throw new ArithmeticException(); answer=n1/n2;
break;
default:
throw new UnknownOpException("符号错误!!!");
}
return answer;
}
public void handleArithmeticException(ArithmeticException e){//catch的调用方法
System.out.println("出错了!!!被除数是0");
System.out.println("程序结束");
System.exit(0);
}
public void handleUnknownOpException(UnknownOpException e){
//System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("清从新输入:");
try{//第二次尝试输入
System.out.println("请按格式输入符号空格数字");
System.out.println("例如:+ 3");
System.out.println("如果想结束输入e");
doCalculation();
}
catch(UnknownOpException e2){
// System.out.println(e2.getMessage());
e.printStackTrace();
System.out.println("符号未知");
System.out.println("程序结束");
System.exit(0);
}
catch(ArithmeticException e3){
handleArithmeticException(e3);
}
}
}
class UnknownOpException extends Exception{//自定义无符号异常
public UnknownOpException(){
super("无符号");
}
public UnknownOpException(String o){
super(o);
System.out.println(o+"没有这个符号");
}
}
你看看行不?程序设计的不是很好,但能实现+-*/
另一个小+-*/的程序,你自己再改改
public class TestArgs{
public static void main(String[] args){//在编译时自带参数。类型强制转换
if(args.length<3){//命令行参数个数小于3
System.out.println(
"Usage: java Test\"n1\"\"op\"\"n2\"");
System.exit(-1);//系统退出,-1表示非正常退出。0表示正常退出。
}
double d1= Double.parseDouble(args[0]);
double d2= Double.parseDouble(args[2]);
double d= 0;
if(args[1].equals("+")) d= d1+d2;
else if(args[1].equals("-")) d= d1-d2;
else if(args[1].equals("x")) d= d1*d2;//*在这个程序里的cmd运行下有错误,所以用x
else if(args[1].equals("/")) d= d1/d2;
else{
System.out.println("Error operator!");
System.exit(-1);
}
System.out.println(d);
}
}
用java怎么制作验证码
原理:
1.随机生成4个数字用到了Random类
2.对这4个数字设置字体格式用 setFont方法
3.改变字体颜色用setColor然后随机生成颜色
代码如下
package s1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class GetImage extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
//发送图片不能够添加这2行代码
// response.setContentType("text/html;charset=UTF-8");
// request.setCharacterEncoding("UTF-8");
int width=100;
int height=50;
//获得一张图片
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(1, 1, width-2, height-2);
g.setFont(new Font("宋体",Font.BOLD,30));
Random random=new Random();
//填充的字符串
String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//缓存生成的验证码
StringBuffer stringbuffer=new StringBuffer();
//随机生成验证码的颜色和字符
for(int i=0;i<4;i++)
{//设置随机颜色
g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
int index=random.nextInt(62);//这里的62就是从填充字符段中随意选取一个位置
String str1=str.substring(index,index+1);
g.drawString(str1, 20*i, 30);//x,y数值设置太小会显示不出来
stringbuffer.append(str1);
}
//将生成的验证码存到服务器
request.getSession().setAttribute("checkcode", stringbuffer.toString());//key和value
//将图片发送给浏览器
ImageIO.write(image,"jpg", response.getOutputStream());
}
}
用户登录界面代码
package s1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Login extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");//设置服务器发送给浏览器的编码方式
request.setCharacterEncoding("UTF-8");//客户端向服务器提交的数据的解码方式
//获得用户提交的数据
String checkcode= request.getParameter("checkcode");
System.out.println(checkcode);
//判断输入的验证码是不是符合
HttpSession session= request.getSession();// session是存放数据的地方
String str=(String) session.getAttribute("checkcode");
if(str!= null){
if(checkcode.compareToIgnoreCase(str)== 0)//验证码忽略大小写
response.getWriter().println("验证码输入正确");
else
response.getWriter().println("验证码输入错误");
}
else response.getWriter().println("验证码失效");
//使用完的验证码信息要删除,返回原页面再输一次,验证码就失效了
session.removeAttribute("checkcode");
}
}
java如何播放wav文件
建议使用jmf(java media framwork),这样就能播放mp3等众多格式的音乐了;去sun官网下一个jmf,安装好后,把
jmf.jar包引入便可使用,给出例zi代码:使用方法:构造函数中传入文件路径名即可,播放、暂停、继续、停止等功能均已实现。
/*************************************************
* Subclass: MusicPlay
*************************************************/
public class MusicPlay implements Runnable{
private Time zeroTime= new Time(0);
private Player player;
private boolean isloop= false;
/*************************************************
* Function: MusicPlay Description: constructor, load the music file and
* get ready for play Called By: MultiMedia()
*************************************************/
//实例化各个参数 filename为文件名,可为绝对路径
public MusicPlay(String filename){
File file= new File(filename);
try{
player= Manager.createRealizedPlayer(file.toURI().toURL());
player.addControllerListener(new ControllListener());
} catch(NoPlayerException e){
e.printStackTrace();
} catch(CannotRealizeException e){
e.printStackTrace();
} catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
/*************************************************
* Function: isRunning Description: test if this music is playing Called
* By:
*************************************************/
public boolean isRunning(){
return player.getState()== Player.Started;
}
/*************************************************
* Function: play Description: play the music for once Called By:
* resumeAll()
*************************************************/
//只播放一次
public void play(){
if(!turnOff)
player.start();
}
/*************************************************
* Function: replay Description: replay the music Called By: musics that
* will be played many times will invoke this methed
*************************************************/
//再播放一次
public void replay(){
if(turnOff)
return;
if(player.getState()== Controller.Prefetched)
player.setMediaTime(zeroTime);
player.start();
}
/*************************************************
* Function: stop Description: stop this music Called By: stopAll() of
* upper class,suspendAll() of upper
* class,BackroundForMenuPanel,GameOverPanel
*************************************************/
public void stop(){
player.stop();
}
/*************************************************
* Function: close Description: dispose the music Called By: closeAll()
* of super class
*************************************************/
public void close(){
player.stop();
player.close();
}
/*************************************************
* Function: loop Description: make the music played repetitiously
* Called By: music that will repetitious play
*************************************************/
//循环播放
public void loop(){
if(turnOff)
return;
isloop= true;
player.prefetch();
replay();
}
/*************************************************
* Function: run Description: trig this music Called By: Override method
*************************************************/
@Override
public void run(){
loop();
}
/*************************************************
* Subclass: ControllListener Description: listener for playing and
* implement playing repetitiously
*************************************************/
//通过对播放进度的监听,实现循环播放
private class ControllListener implements ControllerListener{
public void controllerUpdate(ControllerEvent e){
if(e instanceof EndOfMediaEvent){
if(isloop){
player.setMediaTime(new Time(0));
player.start();
}
}
}
}
}
java999的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java官方网站、java999的信息别忘了在本站进行查找哦。