包含javajishuan的词条

博主:adminadmin 2022-11-23 13:53:09 47

今天给各位分享javajishuan的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎么计算一个整数长度(不含负数符号)?

实现方法如下:

1 把这个数取绝对值

2 然后转成字符串,然后用length函数获取长度就行了。

java运算符的计算方法?

位运算符用于对整型数据进行按位操作,“与”例如10=0,|“或”如:1|0=1

a=4|3=100(2)|11(2)=111(2)=7(10);

b=43=100(2)011(2)=000(2)=0(10);

java中计算两个时间相差多少分钟

java中计算两个时间相差多少分钟

具体代码如下:

public static String getDatePoor(Date endDate, Date nowDate) {

long nd = 1000 * 24 * 60 * 60;

long nh = 1000 * 60 * 60;

long nm = 1000 * 60;

// long ns = 1000;

// 获得两个时间的毫秒时间差异

long diff = endDate.getTime() - nowDate.getTime();

// 计算差多少天

long day = diff / nd;

// 计算差多少小时

long hour = diff % nd / nh;

// 计算差多少分钟

long min = diff % nd % nh / nm;

// 计算差多少秒//输出结果

// long sec = diff % nd % nh % nm / ns;

return day + "天" + hour + "小时" + min + "分钟";

}

data默认有toString()

输出格林威治时间,比如说Date date = new Date();

String toStr = date.toString();

扩展资料:

Java是由Sun Microsystems公司于1995年5月推出的Java程序设计语言(以下简称Java语言)和Java平台的总称。

用Java实现的HotJava浏览器(支持Java applet)显示了Java的魅力:跨平台、动感的Web、Internet计算。

从此,Java被广泛接受并推动了Web的迅速发展,常用的浏览器现在均支持Java applet。另一方面,Java技术也不断更新。

Java语言是一个面向对象的。Java语言提供类、接口和继承等原语,为了简单起见,只支持类之间的单继承,但支持接口之间的多继承,并支持类与接口之间的实现机制(关键字为implements)。

参考资料:JAVA计算-百度百科

java如何进行精确运算

importjava.math.BigDecimal;publicclassArith{//默认除法运算精度privatestaticfinalintDEF_DIV_SCALE=10;//这个类不能实例化privateArith(){;}/***提供精确的加法运算。*@paramv1被加数*@paramv2加数*@return 两个参数的和*/publicstaticdoubleadd(doublev1, doublev2){ BigDecimalb1=newBigDecimal(Double.toString(v1)); BigDecimalb2=newBigDecimal(Double.toString(v2));returnb1.add(b2).doubleValue();}/***提供精确的减法运算。*@paramv1被减数*@paramv2减数*@return 两个参数的差*/publicstaticdoublesub(doublev1, doublev2){ BigDecimalb1=newBigDecimal(Double.toString(v1)); BigDecimalb2=newBigDecimal(Double.toString(v2));returnb1.subtract(b2).doubleValue();}/***提供精确的乘法运算。*@paramv1被乘数*@paramv2乘数*@return 两个参数的积*/publicstaticdoublemul(doublev1, doublev2){ BigDecimalb1=newBigDecimal(Double.toString(v1)); BigDecimalb2=newBigDecimal(Double.toString(v2));returnb1.multiply(b2).doubleValue();}/***提供(相对)精确的除法运算,当发生除不尽的情况时,精确到*小数点以后10位,以后的数字四舍五入。*@paramv1被除数*@paramv2除数*@return 两个参数的商*/publicstaticdoublediv(doublev1, doublev2){returndiv(v1,v2,DEF_DIV_SCALE);}/***提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指*定精度,以后的数字四舍五入。

如何用java编程计算

你是问算法 还是带界面?

package Counter;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

class pbl extends Frame

{

double x,y,a,b;

int z;

GridLayout gl1,gl2,gl3,gl4;

Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn17;

JTextField tf1;

StringBuffer str;

Panel p1,p2,p3,p4;

public pbl()

{

JFrame jf=new JFrame("计算器");

jf.setSize(180,280);

jf.setLocation(150,150);

gl1=new GridLayout(1,1,5,5);

gl2=new GridLayout(5,3,5,5);

gl3=new GridLayout(3,1,5,5);

gl4=new GridLayout(1,1,5,5);

p1=new Panel();

p1.setLayout(gl1);

tf1=new JTextField("0");

tf1.setHorizontalAlignment(JTextField.RIGHT);

p1.add(tf1);

p1.setBounds(10, 20,153, 30);

tf1.setEditable(false);

str=new StringBuffer();

btn1=new Button("1");

btn1.setForeground(Color.BLUE);

btn1.addActionListener(new ac());

btn2=new Button("2");

btn2.setForeground(Color.BLUE);

btn2.addActionListener(new ac());

btn3=new Button("3");

btn3.setForeground(Color.BLUE);

btn3.addActionListener(new ac());

btn4=new Button("4");

btn4.setForeground(Color.BLUE);

btn4.addActionListener(new ac());

btn5=new Button("5");

btn5.setForeground(Color.BLUE);

btn5.addActionListener(new ac());

btn6=new Button("6");

btn6.setForeground(Color.BLUE);

btn6.addActionListener(new ac());

btn7=new Button("7");

btn7.setForeground(Color.BLUE);

btn7.addActionListener(new ac());

btn8=new Button("8");

btn8.setForeground(Color.BLUE);

btn8.addActionListener(new ac());

btn9=new Button("9");

btn9.setForeground(Color.BLUE);

btn9.addActionListener(new ac());

btn0=new Button("0");

btn0.setForeground(Color.BLUE);

btn0.addActionListener(new ac());

btn10=new Button("+");

btn10.setForeground(Color.RED);

btn10.addActionListener(new ac());

btn11=new Button("-");

btn11.setForeground(Color.RED);

btn11.addActionListener(new ac());

btn12=new Button("*");

btn12.setForeground(Color.red);

btn12.addActionListener(new ac());

btn13=new Button("/");

btn13.setForeground(Color.RED);

btn13.addActionListener(new ac());

btn14=new Button(".");

btn14.setForeground(Color.RED);

btn14.addActionListener(new ac());

btn15=new Button("+/-");

btn15.setForeground(Color.RED);

btn15.addActionListener(new ac());

btn16=new Button("CE");

btn16.setForeground(Color.RED);

btn16.addActionListener(new ac());

btn17=new Button("=");

btn17.setForeground(Color.RED);

btn17.addActionListener(new ac());

p2=new Panel();

p2.setLayout(gl2);

p2.add(btn16);

p2.add(btn13);

p2.add(btn12);

p2.add(btn1);

p2.add(btn2);

p2.add(btn3);

p2.add(btn4);

p2.add(btn5);

p2.add(btn6);

p2.add(btn7);

p2.add(btn8);

p2.add(btn9);

p2.add(btn0);

p2.add(btn14);

p2.add(btn15);

p2.setBounds(10, 75, 120, 150);

p3=new Panel();

p3.setLayout(gl3);

p3.add(btn10);

p3.add(btn17);

p3.setBounds(133,105,30,182);

p4=new Panel();

p4.setLayout(gl4);

p4.add(btn11);

p4.setBounds(133, 75, 30, 26);

jf.setLayout(null);

jf.setResizable(false);

jf.add(p1);

jf.add(p2);

jf.add(p3);

jf.add(p4);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

class ac implements ActionListener

{

public void actionPerformed(ActionEvent ce)

{

if(ce.getSource()==btn16)

{

tf1.setText("0");//ce

str.setLength(0);

}

else if(ce.getSource()==btn15)//(+/-)

{

a=Double.parseDouble(tf1.getText());

//a*=-1;

tf1.setText(""+(-a));

}

else if(ce.getSource()==btn14)//(.)

{

if(tf1.getText().trim().indexOf(".")!=-1)

{

}

else if(tf1.getText().trim().equals("0"))

{

tf1.setText("0"+str.append(ce.getActionCommand()).toString());

}

else

{

tf1.setText(str.append(ce.getActionCommand()).toString());

}

}

else if(ce.getSource()==btn10)//(+)

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0;

z=1;

}

else if(ce.getSource()==btn11)//(-)

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0;

z=2;

}

else if(ce.getSource()==btn12)//(*)

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0;

z=3;

}

else if(ce.getSource()==btn13)//(/)

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0;

z=4;

}

else if(ce.getSource()==btn17)//(=)

{

str.setLength(0);

y=Double.parseDouble(tf1.getText().trim());

switch (z)

{

case 1: tf1.setText(""+(x+y)) ;break;

case 2: tf1.setText(""+(x-y)) ;break;

case 3: tf1.setText(""+(x*y)) ;break;

case 4: tf1.setText(""+(x/y)) ;break;

}

z=0;

}

else

{

tf1.setText(str.append(ce.getActionCommand()).toString());

}

}

}

public static void main(String[] args)

{

new pbl();

}

}

JAVA怎么计算百分比?

int num1 = 7;  

int num2 = 9;  

// 创建一个数值格式化对象

NumberFormat numberFormat = NumberFormat.getInstance();  

// 设置精确到小数点后2位  

public String getPercent(int x,int total){

String result="";//接受百分比的值

double x_double=x*1.0;

double tempresult=x/total;

numberFormat.setMaximumFractionDigits(2);

String result = numberFormat.format((float) num1 / (float) num2 * 100);  

System.out.println("num1和num2的百分比为:" + result + "%");

比较运算符

比较运算符属于二元运算符,用于程序中的变量之间,变量和自变量之间以及其他类型的信息之间的比较。比较运算符的运算结果是boolean型。当运算符对应的关系成立时,运算的结果为true,否则为false。比较运算符共有6个,通常作为判断的依据用于条件语句中。

Java解释器在没有生成任何对象的情况下,以main作为入口来执行程序。每个类中可以定义多个方法,但main方法只能有一个。关键字public表示访问权限,指明所有的类都可以使用这一方法。

以上内容参考:百度百科-Java

javajishuan的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javajishuan的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。