资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java缴电费算法代码 java话费充值代码

java课程的题目,问题在补充内,求高手。

Scanner input=new Scanner(System.in);

成都创新互联专注于网站建设,为客户提供网站设计、成都网站制作、网页设计开发服务,多年建网站服务经验,各类网站都可以开发,成都品牌网站建设,公司官网,公司展示网站,网站设计,建网站费用,建网站多少钱,价格优惠,收费合理。

   System.out.println("请输入本月用电量:");

   int num=0;

   try {

   num=input.nextInt();

   } catch (Exception e) {

   System.out.println("请输入数字");

   }

   double count=0;

   if(num200){

   count=num*0.10;

   }else if(num=200  num=500){

   count=num*0.30;

   }else{

   count=num*0.50;

   }

   System.out.println("你需交电费:"+count);

用Java代码生成打印收据

首先 来看看效果 如下图

代码如下

PrintTest java

import java awt BasicStroke;

import java awt Color;

import java awt Font;

import java awt Graphics;

import java awt Graphics D;

import java awt print Book;

import java awt print PageFormat;

import java awt print Paper;

import java awt print Printable;

import java awt print PrinterException;

import java awt print PrinterJob;

import java text SimpleDateFormat;

import java util Date;

import snjp model RentDetail;

import snjp parameter RentRecordParameter;

public class PrintTest implements Printable

{

private String roomID;

private Date printDate;

private Integer rentType;

private Float waterFee;

private Float electricityFee;

private Float rent;

private String remark;

private static int RECEIPT_NO = ;

public PrintTest()

{

}

public PrintTest printTest;

public PrintTest(RentRecordParameter prentRecord RentDetail prentDetail)

{

this roomID = prentRecord getRoomId()+ 房间 ;

this printDate = prentRecord getRegisterDate();

this rentType = prentRecord getRentType();

this waterFee = prentDetail getWaterBill();

this electricityFee = prentDetail getElectricityBill();

this rent = prentDetail getRent();

this remark = prentDetail getRemark();

}

/** */

/**

* @param Graphic指明打印的图形环境

* @param PageFormat指明打印页格式(页面大小以点为计量单位 点为 英才的 / 英寸为 毫米 A 纸大致为 × 点)

* @param pageIndex指明页号

**/

public int print(Graphics graphics PageFormat pf int pageIndex)

throws PrinterException

{

//print string

String receiptTitle = 收款收据 ;

String content = 今收到 ;

String feeItem = null;

Float rentTotal = f;

String content = 总计人民币(大写) ;

//收款的内容(根据租赁类型 只有 房租 房租 水费 电费 两种值)

switch(this rentType)

{

case :

feeItem = 房租 押金 ;

rentTotal=this rent;

break;

case :

feeItem = 房租 押金 水费 电费 ;

rentTotal=this rent+this electricityFee+this waterFee;

break;

default:

feeItem = 未知收费项目 ;

}

//转换成Graphics D

Graphics D graphics D = (Graphics D) graphics;

//设置打印颜色为黑色

graphics D setColor(Color black);

//打印起点坐标x=

double x = pf getImageableX();

double y = pf getImageableY();

System out println( 打印起点坐标值是{ +x+ +y+ } );

switch (pageIndex)

{

case :

//设置打印字体(字体名称 样式和点大小)(字体名称可以是物理或者逻辑名称)

//Java平台所定义的五种字体系列 Serif SansSerif Monospaced Dialog 和 DialogInput

Font font = new Font( 新宋体 Font TRUETYPE_FONT );

graphics D setFont(font);//设置字体

BasicStroke   basicStroke=new   BasicStroke( f);

float[] dash = { f };

//设置打印线的属性

// 线宽; ; ; 空白的宽度; 虚线的宽度; 偏移量

//                graphics D setStroke(new BasicStroke( f

//                                BasicStroke CAP_BUTT BasicStroke JOIN_MITER

//                                f dash f));

//graphics D setStroke(new BasicStroke( f));

graphics D setStroke(basicStroke);//设置线宽

float height = font getSize D();//字体高度

System out println( 字体高度是 +height);

//绘制收据标题

graphics D drawString(receiptTitle (float) y + * height);

//绘制收据编号

String receiptNo = String valueOf(RECEIPT_NO);

graphics D drawString( No : +GlobalMethod get String(receiptNo ) (float)x* (float) y + * height);

//绘制打印时间

SimpleDateFormat myDateFormat = new SimpleDateFormat( yyyy年MM月dd日 );

graphics D drawString(myDateFormat format(printDate) (float)x* (float) y + * height);

//绘制收据标题下面的第一条横线

graphics D drawLine(

(int) (y + * height+ )

(int) (y + * height+ )

);

//绘制收据标题下面的第二条横线

graphics D drawLine(

(int) (y + * height+ )

(int) (y + * height+ )

);

//正文 ——今收到

graphics D drawString(content (float) x* (float) y + * height);

//收费项目名称

graphics D drawString(this roomID+feeItem (float) x* (float) y + * height);

//绘制正文 下划线

graphics D drawLine(

(int)x*

(int) (y + * height+ )

(int)x+

(int) (y + * height+ )

);

//人民币(大写)

graphics D drawString(content +Money NumToRMBStr(rentTotal) (float) x* (float) y + * height);

//绘制人民币(大写)下划线

graphics D drawLine(

(int)x*

(int) (y + * height+ )

(int)x+

(int) (y + * height+ )

);

//(小写)

graphics D drawString( (小写) +rentTotal+ 元 (float) x* (float) y + * height);

//绘制(小写)下划线

graphics D drawLine(

(int)x*

(int) (y + * height+ )

(int)x+

(int) (y + * height+ )

);

//附注

graphics D drawString( 附注: +this remark (float) x* (float) y + * height);

//绘制附注下划线

graphics D drawLine(

(int)x* +

(int) (y + * height+ )

(int)x+

(int) (y + * height+ )

);

// 签字(收款人)

graphics D drawString( 签字(收款人) (float) x* + (float) y + * height);

//签字(收款人)下划线

graphics D drawLine(

(int)x*

(int) (y + * height+ )

(int)x+

(int) (y + * height+ )

);

return PAGE_EXISTS;

default:

RECEIPT_NO=RECEIPT_NO ;

return NO_SUCH_PAGE;

}

}

//方法调用

public  void printReceipt(PrintTest printTest){

//通俗理解就是书 文档

Book book = new Book();

//设置成竖打

PageFormat pf = new PageFormat();

pf setOrientation(PageFormat PORTRAIT);

//通过Paper设置页面的空白边距和可打印区域 必须与实际打印纸张大小相符

Paper p = new Paper();

p setSize( );//纸张大小

p setImageableArea( );//A ( X )设置打印区域 其实 应该是 因为A 纸的默认X Y边距是

pf setPaper(p);

//把 PageFormat 和 Printable 添加到书中 组成一个页面

book append(printTest pf);

//获取打印服务对象

PrinterJob job = PrinterJob getPrinterJob();

//设置打印类

job setPageable(book);

try

{

//可以用printDialog显示打印对话框 在用户确认后打印 也可以直接打印

//            boolean isShowing=job printDialog();

//            if(isShowing)

//            {

//              System out println(this printDate+ ************* );

RECEIPT_NO=RECEIPT_NO+ ;

job print();

//            }

}

catch (PrinterException e)

{

e printStackTrace();

}

}

public static void main(String[] args)

{

//    通俗理解就是书 文档

Book book = new Book();

//    设置成竖打

PageFormat pf = new PageFormat();

pf setOrientation(PageFormat PORTRAIT);

//    通过Paper设置页面的空白边距和可打印区域 必须与实际打印纸张大小相符

Paper p = new Paper();

p setSize( );//纸张大小

p setImageableArea( );//A ( X )设置打印区域 其实 应该是 因为A 纸的默认X Y边距是

pf setPaper(p);

//    把 PageFormat 和 Printable 添加到书中 组成一个页面

book append(new PrintTest() pf);

//获取打印服务对象

PrinterJob job = PrinterJob getPrinterJob();

// 设置打印类

job setPageable(book);

try

{

//可以用printDialog显示打印对话框 在用户确认后打印 也可以直接打印

//boolean a=job printDialog();

//if(a)

//{

job print();

//}

}

catch (PrinterException e)

{

e printStackTrace();

}

}

lishixinzhi/Article/program/Java/hx/201311/25778

一个java实验.请高手帮忙,

import java.util.Date;

public class Test {

public static void main(String[] args){

System.out.println("夏季用125度电的总电费是:"+new DianFei(125,true).getFee());

System.out.println("冬季用125度电的总电费是:"+new DianFei(125,false).getFee());

}

}

//电费类

class DianFei {

private double data;

private boolean b;

private double dj=0.49;//每度电费的价格

//data 为使用的度数,b为true表示夏天,b为false表示冬天

public DianFei(double data,boolean b){

this.data=data;

this.b=b;

}

//如果只传入一个用电度数,那么4-9月为夏,其他为冬

public DianFei(double data){

this.data=data;

int month=new Date().getMonth();

if(month=3 month=8){//月数十从0开始的

this.b=true;

}else{

this.b=false;

}

}

//得到电费的钱数

public double getFee(){

if(!b) data+=20;

return data*dj;

}

}

这个java程序是抄的书,没有报错,怎么无法运行,求大神指点

当然有错了,你没给main函数的参数args赋值,就直接访问了args[0],肯定会报数组越界异常。

你是不是通过命令行执行该程序的?

执行时要加上参数,告诉程序本月用电度数到底是多少


分享标题:java缴电费算法代码 java话费充值代码
本文路径:http://cdkjz.cn/article/ddjcoed.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220