有java程序的代码 java常用的代码
Java程序代码
import java.awt.*;//计算器实例
创新互联不只是一家网站建设的网络公司;我们对营销、技术、服务都有自己独特见解,公司采取“创意+综合+营销”一体化的方式为您提供更专业的服务!我们经历的每一步也许不一定是最完美的,但每一步都有值得深思的意义。我们珍视每一份信任,关注我们的成都网站设计、网站制作、外贸营销网站建设质量和服务品质,在得到用户满意的同时,也能得到同行业的专业认可,能够为行业创新发展助力。未来将继续专注于技术创新,服务升级,满足企业一站式成都全网营销推广需求,让再小的高端网站设计也能产生价值!
import java.awt.event.*;
public class calculator
{
public static void main(String args[])
{
MyWindow my=new MyWindow("计算器");
}
}
class MyWindow extends Frame implements ActionListener
{ StringBuffer m=new StringBuffer();
int p;
TextField tex;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,jia,jian,cheng,chu,deng,dian,qingling,kaifang;
MyWindow(String s)
{
super(s);
//StringBuffer s2=new StringBuffer();
//String s;
tex=new TextField(18);
b0=new Button(" 0 ");
b1=new Button(" 1 ");
b2=new Button(" 2 ");
b3=new Button(" 3 ");
b4=new Button(" 4 ");
b5=new Button(" 5 ");
b6=new Button(" 6 ");
b7=new Button(" 7 ");
b8=new Button(" 8 ");
b9=new Button(" 9 ");
dian=new Button(" . ");
jia=new Button(" + ");
jian=new Button(" - ");
cheng=new Button(" × ");
chu=new Button(" / ");
deng=new Button(" = ");
qingling=new Button(" 清零 ");
kaifang=new Button(" √ ");
setLayout(new FlowLayout());
add(tex);
add(b0);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(dian);
add(jia);
add(jian);
add(cheng);
add(chu);
add(kaifang);
add(qingling);
add(deng);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
dian.addActionListener(this);
deng.addActionListener(this);
qingling.addActionListener(this);
kaifang.addActionListener(this);
setBounds(200,200,160,280);
setResizable(false);//不可改变大小
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent ee)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
m=m.append("0");
tex.setText(String.valueOf(m));
}
if(e.getSource()==b1)
{
m=m.append("1"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b2)
{
m=m.append("2"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b3)
{
m=m.append("3"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b4)
{
m=m.append("4"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b5)
{
m=m.append("5"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b6)
{
m=m.append("6"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b7)
{
m=m.append("7"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b8)
{
m=m.append("8"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b9)
{
m=m.append("9"); tex.setText(String.valueOf(m));
}
if(e.getSource()==jia)
{
m=m.append("+"); tex.setText(String.valueOf(m));
}
if(e.getSource()==jian)
{
m=m.append("-"); tex.setText(String.valueOf(m));
}
if(e.getSource()==cheng)
{
m=m.append("*"); tex.setText(String.valueOf(m));
}
if(e.getSource()==chu)
{
m=m.append("/"); tex.setText(String.valueOf(m));
}
if(e.getSource()==dian)
{
m=m.append("."); tex.setText(String.valueOf(m));
}
String mm=String.valueOf(m);
int p1=mm.indexOf("+");
int p2=mm.indexOf("-");
int p3=mm.indexOf("*");
int p4=mm.indexOf("/");
if(p1!=-1)
{
p=p1;
}
else if(p3!=-1)
{
p=p3;
}
else if(p2!=-1)
{
p=p2;
}
else if(p4!=-1)
{
p=p4;
}
if(e.getSource()==deng)
{
String m1=mm.substring(0,p);
String m2=mm.substring(p+1);
String ch=mm.substring(p,p+1);
//System.out.println(m1);
//System.out.println(m2);
//System.out.println(ch);
if(ch.equals("+"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1+n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("-"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1-n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("*"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1*n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("/"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1/n2;
String su=String.valueOf(sum);
tex.setText(su);
}
}
if(e.getSource()==qingling)
{StringBuffer kk=new StringBuffer();
m=kk;
tex.setText("0");
// System.out.println(mm);
}
if(e.getSource()==kaifang)
{
String t=tex.getText();
float num=Float.parseFloat(t);
double nub=Math.sqrt(num);
tex.setText(String.valueOf(nub));
}
}
}
一个简单的Java程序代码?
package com.zpp;public class Charge {
public static void main(String [] args) {
if(args.length ==0) {
System.out.println("parameter error!");
System.out.println("java com.zpp.Charge [int]");
return;
}
int min = Integer.parseInt(args[0]);
double money = 0.0;
if (min = 0) {
money =0.0;
System.out.println("not money");
} else if (min = 60) {
money = 2.0;
} else {
money = 2.0 + (min - 60) * 0.01;
}
System.out.println("please pay: " + money);
}
} 编译:javac -d . Charge.java运行:java com.zpp.Charge 111
Java求程序代码
按照你的要求编写的Java程序如下
import java.util.Scanner;
public class S{
public static void main(String[] args){
int n=5;
int[] a=new int[n];
System.out.print("请输入"+n+"个正整数:");
Scanner sc=new Scanner(System.in);
for(int i=0;in;i++){
a[i]=sc.nextInt();
}
System.out.print("其中的偶数为:");
for(int i=0;in;i++){
if(a[i]%2==0){
System.out.print(a[i]+" ");
}
}
}
}
求一个java程序代码
package Test1;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.IntSummaryStatistics;
import java.util.Scanner;
import java.util.Vector;
public class D {
static String s1 = "";
public static void main(String[] args) {
int day = LocalDateTime.now().getDayOfMonth();
Month month = LocalDateTime.now().getMonth();
int i = 1, i1 = 0;
VectorInteger vector=new Vector();
Scanner scanner = new Scanner(System.in);
while (i day + 1) {
System.out.println("请输入" + month + "月" + i + "日走的步数");
try {
i1 = scanner.nextInt();
} catch (Exception e) {
// TODO: handle exception
System.out.println("输入错误,请重新输入");
continue;
}
vector.add(i1);
i++;
}
scanner.close();
IntSummaryStatistics intSummaryStatistics=vector.stream().mapToInt(x-x).summaryStatistics();
int i2=intSummaryStatistics.getMax();
int i3=vector.indexOf(i2);
System.out.println(month+"月走的步数最多的日期为: "+(i3+1)+"号");
System.out.println("最多的步数为: "+i2);
}
}
分享标题:有java程序的代码 java常用的代码
分享网址:http://ybzwz.com/article/hgiphj.html