如果小明的java成绩_求助Java

展开全部

两个Java文件CalcCanvas.java//CalcMIDlet.java

import javax.microedition.lcdui.*;

import java.io.*;

public class CalcCanvas extends Canvas{

/**退出按钮*/

private Command exitCommand;

/**标题*/

private String title = "计算器";

/**字体*/

private Font font;

/**计算器屏幕显示的内容*/

private long screenValue= 0;

/**按键编号*/

private int key = 0;

/**当前按键的坐标*/

private int focusX = 5;

private int focusY = 52;

/**按键宽度间隔32313133353236313431303231363533e78988e69d8331333337613833*/

private int blockWidth = 2;

/**按键高度间隔*/

private int blockHeight = 2;

/**键盘位置X轴坐标*/

private int PositionX = 2;

/**键盘位置Y轴坐标*/

private int PositionY = 50;

/**按键尺寸*/

private int keyWidth = 39;

private int keyHeight = 33;

/**键盘图片文件*/

private Image keyBoard = null;

/**存储按键的标志*/

private char[] keyFlag = {'f','k','m','c','a',

'7','8','9','/','%',

'4','5','6','*','r',

'1','2','3','-','j',

'0','.','=','+','p'

};

/**运算中的第一个操作数*/

private long firstNum = 0;

/**运算中的第二个操作数*/

private long secondNum = 0;

/**计算状态,true表示第一个操作数已经准备好*/

private boolean flag = false;

/**存储按下的运算符按键的索引*/

private int operatorIndex = 0;

/**构造方法*/

public CalcCanvas() {

try{

keyBoard = Image.createImage("/calc.png");

}catch(IOException e){

System.out.println(e);

}

}

public void paint(Graphics g) {

/**清除屏幕*/

g.setColor(255,255,255);

g.fillRect(0,0,getWidth(),getHeight());

g.setColor(0,0,0);

/**获得字体*/

font = g.getFont();

//绘制标题

g.drawString(title,getWidth() / 2,font.getHeight() / 2,Graphics.TOP|Graphics.HCENTER);

//绘制计算器屏幕

g.drawRect(3,24,200,20);

//绘制数字

String temp = Long.toString(screenValue);

g.drawString(temp,getWidth()-40,30,Graphics.TOP|Graphics.RIGHT);

//绘制键盘

g.drawImage(keyBoard,PositionX,PositionY,Graphics.TOP|Graphics.LEFT);

//绘制焦点按键框

drawFocus(g,operatorIndex);

}

public void keyPressed(int keyCode){

if(keyCode == KEY_NUM4){

if(operatorIndex == 0){

operatorIndex = 24;

}else{

operatorIndex--;

}

}

if(keyCode == KEY_NUM6){

if(operatorIndex == 24){

operatorIndex = 0;

}else{

operatorIndex++;

}

}

if(keyCode == KEY_NUM2){

if(operatorIndex - 5 < 0){

operatorIndex = 24 + operatorIndex - 4;

}else{

operatorIndex = operatorIndex - 5;

}

}

if(keyCode == KEY_NUM8){

if(operatorIndex + 5 > 24){

operatorIndex = operatorIndex + 4 - 24;

}else{

operatorIndex = operatorIndex + 5;

}

}

//确定按钮

if(keyCode == KEY_NUM5){

caculate();

}

//绘图

repaint();

}

private void drawFocus(Graphics g,int num){

g.setColor(255,0,0);

focusX = PositionX + (operatorIndex % 5) * (keyWidth + blockWidth);

focusY = PositionY + (operatorIndex / 5) * (keyHeight + blockHeight);

g.drawRect(focusX,focusY,keyWidth,keyHeight);

}

private void caculate(){

//判断焦点按键是否为数字

if((operatorIndex >=5 && operatorIndex 8) || (operatorIndex >= 10 && operatorIndex 13) || (operatorIndex >= 15 && operatorIndex < 18) || (operatorIndex == 20)){

//如果是数字,则将原数字乘以10,再加上现在的数字

screenValue = screenValue * 10 + (keyFlag[operatorIndex] - '0');

}

//如果按下的是负号,则求当前数字的负数

if(operatorIndex == 2){

screenValue = -screenValue;

}

if(operatorIndex == 3){

screenValue = 0;

secondNum = 0;

}

if(operatorIndex == 4){

screenValue = 0;

firstNum = 0;

secondNum = 0;

flag = false;

}

//判断第一个操作数是否已经准备好

if(flag == true){

secondNum = screenValue;

//按下运算按钮时,进行运算

if(operatorIndex == 22 || (operatorIndex == 1)|| (operatorIndex == 8)|| (operatorIndex == 13)|| (operatorIndex == 18)|| (operatorIndex == 23)){

if(key == 8){

//除法运算

screenValue = firstNum / secondNum;

}else if(key == 13){

//乘法运算

screenValue = firstNum * secondNum;

}else if(key == 18){

//减法运算

screenValue = firstNum - secondNum;

}else if(key == 23){

//加法运算

screenValue = firstNum + secondNum;

}

//按下=按钮

if(operatorIndex == 22){

//将运算结果赋给第一个操作数

firstNum = screenValue;

//将标志修改为false,表示一次计算完毕

flag = false;

}else{

//记录运算符的索引

key = operatorIndex;

//将运算结果赋给第一个操作数

firstNum = screenValue;;

screenValue = 0;

}

}

}else{

firstNum = screenValue;

if((operatorIndex == 1)|| (operatorIndex == 8)|| (operatorIndex == 13)|| (operatorIndex == 18)|| (operatorIndex == 23)){

key = operatorIndex;

flag = true;

screenValue = 0;

}

}

}

}

###############

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class CalcMIDlet extends MIDlet {

private static CalcMIDlet instance;

private CalcCanvas calc = new CalcCanvas();

/** Constructor */

public CalcMIDlet() {

instance = this;

}

/** Main method */

public void startApp() {

Display.getDisplay(this).setCurrent(calc);

}

/** Handle pausing the MIDlet */

public void pauseApp() {

}

/** Handle destroying the MIDlet */

public void destroyApp(boolean unconditional) {

}

/** Quit the MIDlet */

public static void quitApp() {

instance.destroyApp(true);

instance.notifyDestroyed();

instance = null;

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值