求助,用java编写一个小程序

求助,用java编写一个小程序,第1张

package com;

public interface PlaneGraphics {

public abstract double area();

public abstract double perimeter();

public abstract void print();

}

package com;

public class Rectangle implements PlaneGraphics{

private double x = 75;

private double y = 157;

private double s = 0;

private double l= 0;

public double area() {

s = x y;

return s;

}

public double perimeter() {

l = 2 (x + y);

return l;

}

public void print() {

Systemoutprintln("这是长方形");

Systemoutprintln("长方形的宽度:"+x+"长方形的长度:"+y);

Systemoutprintln("长方形的面积:"+s);

Systemoutprintln("长方形的周长:"+l);

}

package com;

public class Ellipse implements PlaneGraphics {

private double a = 57;

private double b = 25;

private double s = 0;

private double l = 0;

private double pie = 314;

public double area() {

s = pie a b;

return s;

}

public double perimeter() {

double m = 0;

m = javalangMathsqrt(a b);

l = pie (15 (a+b) - m);

return l;

}

public void print() {

Systemoutprintln("这是椭圆形");

Systemoutprintln("椭圆形的a轴长:"+a+"椭圆形的b轴长:"+b);

Systemoutprintln("椭圆形的面积:"+s);

Systemoutprintln("椭圆形的周长:"+l);

}

}

package com;

public class Test {

public static void main(String[] args) {

Rectangle rec = new Rectangle();

recarea();

recperimeter();

recprint();

Ellipse ell = new Ellipse();

ellarea();

ellperimeter();

ellprint();

}

}

public class CalculatorA {

private JFrame jf;

private JButton[] jbs;

private JTextField jtf;

private JButton clear;

private double num1,num2,jieguo;

private char c;

/

构造方法实例化属性

/

public CalculatorA(){

jf=new JFrame("我的计算器v10"); //定义窗口的title

jtf=new JTextField(20);//定义文本域 也就是计算器的输入和现实区域

clear=new JButton("clear");// 定义按钮

jbs=new JButton[16];//定义按钮数组

String str="123+456-7890/=";//此处用来批量创建数字和元素符号。

for(int i=0; i<strlength(); i++){//批量赋值。这样做使得定义按钮方便很多否则,你需要一个按钮一个按钮像上面的"clear"那样的定义

jbs[i]=new JButton(strcharAt(i)+"");

}

init();

addEventHandler();

// setFont();

// setColor();

showMe();

}

/

布局图形界面

/

public void init(){

// jfsetLayout(manager)

JPanel jp1=new JPanel();//定义面板,用来容纳上面的按钮文本域等组件

jp1add(jtf);//将 文本域 让如面板中

JPanel jp2=new JPanel();//定义面板,用来容纳上面的按钮文本域等组件

jp2setLayout(new GridLayout(4,4));//定义网格布局,类似html 的table一个4X4的格子,然后将计算的按钮放到这个面板中

for(int i=0; i<16; i++){

jp2add(jbs[i]);

}

JPanel jp3=new JPanel();//定义面板,用来容纳上面的按钮文本域等组件

jp3add(clear);//将 清除按钮 让如面板中

jfadd(jp1,BorderLayoutNORTH);//将显示数据面板,使用边框布局,放置最顶端。

jfadd(jp2,BorderLayoutCENTER);//将显示计算按钮面板,使用边框布局,放置中间。

jfadd(jp3,BorderLayoutSOUTH);//将显示清除按钮面板,使用边框布局,放置最底端。

}

public void setFont(){

}

public void setColor(){

}

public void addEventHandler(){//创建监听事件,用来监听每一个按钮,当点击后需要触发什么事件。

ActionListener lis=new ActionListener(){

public void actionPerformed(ActionEvent e){

// TODO Auto-generated method stub

JButton jb=(JButton)egetSource();//获取事件源

// String key=(String)egetActionCommand();

String str =jbgetText()trim();//把字符串的首尾空格去掉!

// String str2=egetActionCommand()trim();//返回事件源的文本内容

if("0123456789"indexOf(str)!=-1){//如果是数字或点号

jtfsetText(jtfgetText()+str);//设置显示文本域,也就是你点击数字按钮,在显示文本域显示出你点击的值。

return;

}

if("+-/"indexOf(str)!=-1){//当点击运算按钮触发的事件

num1=DoubleparseDouble(jtfgetText());//获取文本域的值并转换类型并暂时保存起来。

jtfsetText("");

c=strcharAt(0);

jtfsetText("");

return ;

}

if(strequals("=")){//根据点击的按钮式"="号执行结算结果

num2=DoubleparseDouble(jtfgetText());

// jtfsetText("");

switch(c){

case '+': jieguo=num1+num2;break;

case '-': jieguo=num1-num2;break;

case '': jieguo=num1num2; break;

case '/': jieguo=num1/num2;break;

}

jtfsetText(DoubletoString(jieguo)); //返回结算结果到显示文本域

return;

}

if(egetActionCommand()equals("clear")){

jtfsetText("");//清空显示文本域

return;

}

}

};

for(int i=0; i<jbslength; i++){

//为每个按钮加上监听类

jbs[i]addActionListener(lis);

}

clearaddActionListener(lis);

}

public void showMe(){

jfpack();//调整此窗口的大小,以适合其子组件的首选大小和布局。

jfsetVisible(true);//设置窗口可见

jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);//设置程序的退出按钮,退出即同时停止程序

}

public static void main(String[] args){//启动程序的入口

new CalculatorA();

}

}

这代码注释写的太累人了。小窗口费劲。给加点分吧。

很简单啊,你这是个while循环,catch到这个exception以后提示重新输入,然后因为上一次的输入结果还在,所以当然还是会显示是高还是低了啊

应该改成先输入数字,再判断是高还是低

while(correctCount==0){

try{

userString=JOptionPaneshowInputDialog("Start guessing, it's a number between 1 and 1000");

userNumber=IntegerparseInt(userString);}

catch(NumberFormatException ex){

JOptionPaneshowMessageDialog(null, "Please input a number",JOptionPaneWARNING_MESSAGE);

continue;

}

if((userNumber>rightNumber)&&(userNumber<1000)){

JOptionPaneshowMessageDialog(null,"too high!");

count++;

}

else if((userNumber<rightNumber)&&(userNumber>0)){

JOptionPaneshowMessageDialog(null,"too low!");

count++;

}

else if(userNumber<0||userNumber>1000)

JOptionPaneshowMessageDialog(null,"Stupid Guy",JOptionPaneWARNING_MESSAGE);

else

JOptionPaneshowMessageDialog(null, "Please input a number",JOptionPaneWARNING_MESSAGE);

if (userNumber==rightNumber){

count++;

correctCount++;

}

}

你等我,我来写!

要加注释吗?txt文件也贴给你吧。

import javaioFile;

import javaioFileInputStream;

public class Test {

private static int excellentNum;

private static int goodNum;

private static int passNum;

private static int failNum;

private static int totalNum;

public static void count() {

String str = readFile();

String[] records = strsplit("\n");

totalNum = recordslength;

for(int i = 0; i < totalNum; i++) {

String[] record = records[i]split(" ");

String[] scoreStr = record[recordlength -1]split("\r");

int score = IntegerparseInt(scoreStr[0]);

if(score >= 90) {

excellentNum++;

}else if(score >= 70) {

goodNum++;

}

if(score >= 60) {

passNum++;

}else {

failNum++;

}

}

}

public static void countExcellent() {

Systemoutprintln("优秀率是: " + (float)excellentNum/totalNum100 + "%");

}

public static void countGood() {

Systemoutprintln("良好率是: " + (float)goodNum/totalNum100 + "%");

}

public static void countPass() {

Systemoutprintln("及格率是: " + (float)passNum/totalNum100 + "%");

}

public static void countFail() {

Systemoutprintln("不及格率是: " + (float)failNum/totalNum100 + "%");

}

public static String readFile() {

File file = new File("D:\\scoretxt");

StringBuffer sb = new StringBuffer();

try {

FileInputStream fis = new FileInputStream(file);

byte[] b = new byte[1024];

int length = 0;

while(fisavailable() > 0) {

length = fisread(b);

String str = new String(b, 0, length);

sbappend(str);

}

} catch (Exception e) {

eprintStackTrace();

}

return sbtoString();

}

public static void main(String[] args) {

count();

countExcellent();

countGood();

countPass();

countFail();

}

}

--------------------------------------

scoretxt

001 小明 90

002 小伟 38

004 好地方 80

005 小明 90

006 小伟 38

007 好地方 80

008 小明 90

009 小伟 38

010 好地方 80

001 小明 90

011 小伟 38

012 好地方 80

这个程序实现还要一个关联文件夹“image”象棋棋子 部分代码如下:(代码太长 给个邮箱 我发给你)

import javaawt;

import javaawtevent;

import javaxswing;

import javautil;

import javaio;

//主类

public class Chess{

public static void main(String args[]){

new ChessMainFrame("中国象棋:观棋不语真君子,棋死无悔大丈夫");

}

}

//主框架类

class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{

//玩家

JLabel play[] = new JLabel[32];

//棋盘

JLabel image;

//窗格

Container con;

//工具栏

JToolBar jmain;

//重新开始

JButton anew;

//悔棋

JButton repent;

//退出

JButton exit;

//当前信息

JLabel text;

//保存当前 *** 作

Vector Var;

//规则类对象(使于调用方法)

ChessRule rule;

/

单击棋子

chessManClick = true 闪烁棋子 并给线程响应

chessManClick = false 吃棋子 停止闪烁 并给线程响应

/

boolean chessManClick;

/

控制玩家走棋

chessPlayClick=1 黑棋走棋

chessPlayClick=2 红棋走棋 默认红棋

chessPlayClick=3 双方都不能走棋

/

int chessPlayClick=2;

//控制棋子闪烁的线程

Thread tmain;

//把第一次的单击棋子给线程响应

static int Man,i;

ChessMainFrame(){

new ChessMainFrame("中国象棋");

}

/

构造函数

初始化图形用户界面

/

ChessMainFrame(String Title){

//获行客格引用

con = thisgetContentPane();

consetLayout(null);

//实例化规则类

rule = new ChessRule();

Var = new Vector();

//创建工具栏

jmain = new JToolBar();

text = new JLabel("欢迎使用象棋对弈系统");

//当鼠标放上显示信息

textsetToolTipText("信息提示");

anew = new JButton(" 新 游 戏 ");

anewsetToolTipText("重新开始新的一局");

exit = new JButton(" 退 出 ");

exitsetToolTipText("退出象棋程序程序");

repent = new JButton(" 悔 棋 ");

repentsetToolTipText("返回到上次走棋的位置");

//把组件添加到工具栏

jmainsetLayout(new GridLayout(0,4));

jmainadd(anew);

jmainadd(repent);

jmainadd(exit);

jmainadd(text);

jmainsetBounds(0,0,558,30);

conadd(jmain);

//添加棋子标签

drawChessMan();

//注册按扭监听

anewaddActionListener(this);

repentaddActionListener(this);

exitaddActionListener(this);

//注册棋子移动监听

for (int i=0;i<32;i++){

conadd(play[i]);

play[i]addMouseListener(this);

}

//添加棋盘标签

conadd(image = new JLabel(new ImageIcon("image\\MainGIF")));

imagesetBounds(0,30,558,620);

imageaddMouseListener(this);

//注册窗体关闭监听

thisaddWindowListener(

new WindowAdapter() {

public void windowClosing(WindowEvent we){

Systemexit(0);

}

}

);

//窗体居中

Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();

Dimension frameSize = thisgetSize();

if (frameSizeheight > screenSizeheight){

frameSizeheight = screenSizeheight;

}

if (frameSizewidth > screenSizewidth){

frameSizewidth = screenSizewidth;

}

thissetLocation((screenSizewidth - frameSizewidth) / 2 - 280 ,(screenSizeheight - frameSizeheight ) / 2 - 350);

//设置

thissetIconImage(new ImageIcon("image\\红将GIF")getImage());

thissetResizable(false);

thissetTitle(Title);

thissetSize(558,670);

thisshow();

}

/

添加棋子方法

/

public void drawChessMan(){

//流程控制

int i,k;

//图标

Icon in;

//黑色棋子

//车

in = new ImageIcon("image\\黑车GIF");

for (i=0,k=24;i<2;i++,k+=456){

play[i] = new JLabel(in);

play[i]setBounds(k,56,55,55);

play[i]setName("车1");

}

//马

in = new ImageIcon("image\\黑马GIF");

for (i=4,k=81;i<6;i++,k+=342){

play[i] = new JLabel(in);

play[i]setBounds(k,56,55,55);

play[i]setName("马1");

}

//相

in = new ImageIcon("image\\黑象GIF");

for (i=8,k=138;i<10;i++,k+=228){

play[i] = new JLabel(in);

play[i]setBounds(k,56,55,55);

play[i]setName("象1");

}

//士

in = new ImageIcon("image\\黑士GIF");

for (i=12,k=195;i<14;i++,k+=114){

play[i] = new JLabel(in);

play[i]setBounds(k,56,55,55);

play[i]setName("士1");

}

//卒

in = new ImageIcon("image\\黑卒GIF");

for (i=16,k=24;i<21;i++,k+=114){

play[i] = new JLabel(in);

play[i]setBounds(k,227,55,55);

play[i]setName("卒1" + i);

}

//炮

in = new ImageIcon("image\\黑炮GIF");

for (i=26,k=81;i<28;i++,k+=342){

play[i] = new JLabel(in);

play[i]setBounds(k,170,55,55);

play[i]setName("炮1" + i);

}

//将

in = new ImageIcon("image\\黑将GIF");

play[30] = new JLabel(in);

play[30]setBounds(252,56,55,55);

play[30]setName("将1");

//红色棋子

//车

in = new ImageIcon("image\\红车GIF");

for (i=2,k=24;i<4;i++,k+=456){

play[i] = new JLabel(in);

play[i]setBounds(k,569,55,55);

play[i]setName("车2");

}

//马

in = new ImageIcon("image\\红马GIF");

for (i=6,k=81;i<8;i++,k+=342){

play[i] = new JLabel(in);

play[i]setBounds(k,569,55,55);

play[i]setName("马2");

}

//相

in = new ImageIcon("image\\红象GIF");

for (i=10,k=138;i<12;i++,k+=228){

play[i] = new JLabel(in);

play[i]setBounds(k,569,55,55);

play[i]setName("象2");

}

//士

in = new ImageIcon("image\\红士GIF");

for (i=14,k=195;i<16;i++,k+=114){

play[i] = new JLabel(in);

play[i]setBounds(k,569,55,55);

play[i]setName("士2");

}

//兵

in = new ImageIcon("image\\红卒GIF");

for (i=21,k=24;i<26;i++,k+=114){

play[i] = new JLabel(in);

play[i]setBounds(k,398,55,55);

play[i]setName("卒2" + i);

}

//炮

in = new ImageIcon("image\\红炮GIF");

for (i=28,k=81;i<30;i++,k+=342){

play[i] = new JLabel(in);

play[i]setBounds(k,455,55,55);

play[i]setName("炮2" + i);

}

//帅

in = new ImageIcon("image\\红将GIF");

play[31] = new JLabel(in);

play[31]setBounds(252,569,55,55);

play[31]setName("帅2");

}

/

线程方法控制棋子闪烁

/

}

以上就是关于求助,用java编写一个小程序全部的内容,包括:求助,用java编写一个小程序、有一段JAVA小程序 在线求详细解释、JAVA小程序代码问题,求高手等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10137652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存