189 8069 5689

java初级编程代码 java基础编程

一个简单的Java程序代码?

package com.zpp;public class Charge {

靖江ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

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程序 初级的

package test;

import java.util.Scanner;

public class Test2 {

private String myName;

private int myID;

public static void main(String[] args) {

Test2 test=new Test2();

Scanner in=new Scanner(System.in);

System.out.println("输入姓名");

String myName=in.nextLine();

System.out.println("输入学号");

int myID=in.nextInt();

test.setMyID(myID);//分别赋值给myName和MyID

test.setMyName(myName);

//通过printf方法向控制台输出:我叫xxx,我的学号是xxxxxxxx;

System.out.println("我叫"+test.getMyName()+",我的学号是"+test.getMyID()+";");

//从myName中提取姓和名,并输出:我姓x,我的名字是xx;

System.out.println("我姓"+test.getFirstName()+",名字是"+test.getName()+";");

test.getUnicode();

test.getFan();

}

public void getUnicode(){

char[] a=this.myName.toCharArray();

for(int i=0;ia.length;i++){

System.out.print(a[i]);

System.out.print(Integer.toHexString((int)a[i]));

}

System.out.println("");

}

public void getFan(){

System.out.println(Integer.toBinaryString(-myID+1));

}

public String getFirstName(){

//return name.charAt(0);

return this.myName.substring(0, 1);

}

public String getName(){

return this.myName.substring(1, this.myName.length());

}

public String getMyName() {

return myName;

}

public void setMyName(String myName) {

this.myName = myName;

}

public int getMyID() {

return myID;

}

public void setMyID(int myID) {

this.myID = myID;

}

}

JAVA初级编程。。很简单。希望是原创的越短越简单越好,写的好有追加分!!

第一题:

public class Test {//测试类

public static void main(String[] args) {

Person bzr=new Person("班主任1","男",50);//班主任

Student[] students=new Student[9];//9个学生

for (int i = 0; i 9; i++) {//初始化9个学生

if (i%2==0) {

students[i]=new Student("学生"+(i+1),"男",12,"00"+(i+1));

}else {

students[i]=new Student("学生"+(i+1),"女",12,"00"+(i+1));

}

}

Cls cls=new Cls("一班",bzr,students);//班级

System.out.println(cls);//打印班级

}

}

class Person{//一个person类

String name;//姓名

String sex;//性别

int age;//年龄

public Person(String name, String sex, int age) {//定义构造函数用来初始化类的这些属性

this.name = name;

this.sex = sex;

this.age = age;

}

public Person() {

}

public String toString() {//toString()方法输出Person的信息

return "姓名:"+name+"\t性别:"+sex+"\t年龄:"+age;

}

}

class Student extends Person{//一个Student类,该类继承Person

String sn;//添加一个用于表示学生学号的(sn)属性

public Student(String name, String sex, int age, String sn) {//定义构造函数用来初始化类的这些属性

super(name, sex, age);

this.sn = sn;

}

public Student(){}

public String toString() {//定义toString()方法输出Student的信息

return super.toString()+"\t学号:"+sn;

}

}

class Cls{//一个班级类

String name;//班级名称

Person bzr;//班主任(Person的对象)

Student[] students;//学生(Student类的对象)

public Cls(String name, Person bzr, Student[] students) {

this.name = name;

this.bzr = bzr;

this.students = students;

}

public Cls(){};

public String toString() {//定义toString方法输出班级的信息

String s= "班级名称:\n"+name+"\n班主任:\n"+bzr+"\n";

for (Student student : students) {

s+="学生:\n"+student+"\n";

}

return s;

}

}

第二题:

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.Panel;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class CalculatorGUI {

private Frame f;

private Panel p1, p2;

private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;

private Button bAdd,bCal;

private TextField tf;

private String s, op;

private Calculator cal = new Calculator();

private boolean ifOp;

public CalculatorGUI() {

f = new Frame("Calculator");

p1 = new Panel();

p2 = new Panel();

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");

bAdd = new Button("+");

bCal = new Button("=");

tf = new TextField(25);

tf.setEditable(false);

}

public void launchFrame() {

f.setSize(220, 160);

f.setResizable(false);

f.addWindowListener(new myWindowListener());

p1.setLayout(new FlowLayout(FlowLayout.CENTER));

p1.add(tf);

f.add(p1, BorderLayout.NORTH);

p2.setLayout(new GridLayout(4, 4));

b0.addActionListener(new setLabelText_ActionListener());

b1.addActionListener(new setLabelText_ActionListener());

b2.addActionListener(new setLabelText_ActionListener());

b3.addActionListener(new setLabelText_ActionListener());

b4.addActionListener(new setLabelText_ActionListener());

b5.addActionListener(new setLabelText_ActionListener());

b6.addActionListener(new setLabelText_ActionListener());

b7.addActionListener(new setLabelText_ActionListener());

b8.addActionListener(new setLabelText_ActionListener());

b9.addActionListener(new setLabelText_ActionListener());

bAdd.addActionListener(new setOperator_ActionListener());

bCal.addActionListener(new setOperator_ActionListener());

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.add(b7);

p2.add(b8);

p2.add(b9);

p2.add(b0);

p2.add(bAdd);

p2.add(bCal);

f.add(p2, BorderLayout.SOUTH);

f.setVisible(true);

}

public void setTextFieldText_Temp() {

if (tf.getText().length() 15

(tf.getText().indexOf(".") == -1 || !s.equals("."))) {

tf.setText(tf.getText() + s);

} else {

tf.setText((tf.getText() + s).substring(0, 15));

}

}

public void setTextFieldText() {

if (ifOp) {

ifOp = false;

tf.setText("");

setTextFieldText_Temp();

} else {

setTextFieldText_Temp();

}

}

public static void main(String[] args) {

CalculatorGUI calculator = new CalculatorGUI();

calculator.launchFrame();

}

class myWindowListener extends WindowAdapter {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

}

class setLabelText_ActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

Button tempB = (Button) e.getSource();

s = tempB.getLabel();

setTextFieldText();

}

}

class setOperator_ActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

Button tempB = (Button) e.getSource();

op = tempB.getLabel();

if (op.equals("+")) {

tf.setText(cal.opAdd(tf.getText()));

ifOp = true;

} else if (op.equals("=")) {

tf.setText(cal.opEquals(tf.getText()));

ifOp = true;

}

}

}

}

class Calculator {

private String result = "0";

private int op = 0, add = 1;

private double stringToDouble(String x) {

double y = Double.parseDouble(x);

return y;

}

private void operate(String x) {

double x1 = stringToDouble(x);

double y = stringToDouble(result);

switch (op) {

case 0:

result = x;

break;

case 1:

result = String.valueOf(y + x1);

break;

}

}

public String opAdd(String x) {

operate(x);

op = add;

return result;

}

public String opEquals(String x) {

operate(x);

op = 0;

return result;

}

public void opClean() {

op = 0;

result = "0";

}

}


名称栏目:java初级编程代码 java基础编程
文章位置:http://cdxtjz.cn/article/hjoshd.html

其他资讯