问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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

发布网友 发布时间:2022-04-23 21:26

我来回答

4个回答

热心网友 时间:2023-10-10 03:23

第一题:
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";
}
}

热心网友 时间:2023-10-10 03:23

Use.java
----------

class Use {
public static void main(String[] args){
Class c = new Class("摄像班");
System.out.println(c);
}
}
class People {
String name;
String sex;
String age;

public People(String name,String sex,String age){
this.name = name;
this.sex = sex;
this.age = age;
}

public String toString(){
String str = "name is "+name+",sex is "+sex+",age is "+age;
return str;
}
}
class Student extends People{
String sn;
public Student(String name, String sex, String age,String sn) {
super(name, sex, age);
this.sn = sn;
}
public String toString(){
String str = "name is "+name+",sex="+sex+",age="+age+" ,sn="+sn;
return str;
}

}

class Class {
People teacher ;
Student student;
String className;

public Class(String className){
teacher = new People("陈老师","男","38");
student = new Student("苍jk","女","18","419");
this.className = className;
}

public String toString(){
String str = "this className is"+className+" ,the teacher "+teacher+",the student "+student;
return str;
}
}

---------------------------------------------------------
运行结果:
this className is摄像班 ,the teacher name is 陈老师,sex is 男,age is 38,the student name is 苍jk,sex=女,age=18 ,sn=419
------------------------------------------------------

先写一个

热心网友 时间:2023-10-10 03:24

计算器传送门
http://wenku.baidu.com/view/02e0d91a964bcf84b9d57baa.html

热心网友 时间:2023-10-10 03:24

是想用awt包还是swing包写。追问都可以。。。谢谢了。。哪个简单用哪个把。。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
临沂比较有名的男装品牌 呼伦贝尔市悦动网络科技有限公司怎么样? 呼伦贝尔中汇实业有限公司怎么样? 呼伦贝尔油玉不绝电子商务有限公司怎么样? 如何避免wps卡顿? 属鼠的男人找对象是属什么,属鼠的人和什么属相合 96年鼠的姻缘在哪年 属相相合年份运势提升 2024属鼠找对象属什么最佳 黑客攻击网站能报案吗 黑客攻击报案有用吗 有两万块余款想把他放余额宝里面安全吗?利息一天有多少钱呢? 想给运送货物的大货车购买保险,需要买哪些保险 java入门的书大家推荐几本!·~ 货物运输买的保险主要有哪些种类? 余额宝安全吗,我准备放两万进去,你们都放了 Java入门,编程 运满满司机怎么买货物保险 墨盒清零有什么用? 两万元想存在余额宝上请问,余额宝安全吗 货物运输保险如何投保,我们公司要在网上买货运险,请问投保流程是怎样的 艺术品买家要求购买运输保险有什么猫腻吗 存两万在余额宝可安全 我存两万块钱在余额宝会不会不安全啊?? 刚刚往余额宝里放了两万块钱,没事儿不?有风险么?别再给我卷跑喽! 新车运输会买保险吗 钱放在余额宝安全吗,2w左右 国任保险的货物运输险怎么样?如何投保? 怎么购买货物运输保险? 两万全部放进余额宝安全吗? 货车运输如何买保险 喷墨打印机改装成连供日久以后还要清零吗? 广州住房公积金可以注销吗 广州银行住房公积金办理注意事项有哪些?还有办理流程怎样? 阳光货物运输保险在哪里买 两万块钱,是存在银行好还是放在余额宝里好,在余额宝里有没有风险阿 2万存余额宝还是银行 打印机连供清零的问题 余额宝放20000块钱,安全吗? java编程入门书籍推荐 广州公积金提取需要什么材料,怎么提取广州市公积金 余额宝有安全风险吗?我转入2万一天大概收入多少钱? 广州公积金怎么网上注册 广州市住房公积金销户的问题 惠普2131打印机加墨水后怎么清零? 广州取住房公积金都需要准备什么材料 惠普2131打印机,使用的803墨盒,加墨水后怎么清零,机器显示缺墨 广州公积金提取有哪些注意事项 惠普2131打印机墨盒怎么清零 广州首次提取公积金需要什么材料 广州公积金自己怎么提取