
正文
java代码编写小程序 如何用java编写小程序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java语言编写小程序
public class Test {
public static void main(String[] args) {
Trapezoid t = new Trapezoid(3, 4, 5);
System.out.println(t.area());
}
}
class Trapezoid {
private double topline;
private double baseline;
private double height;
public Trapezoid() {
}
public Trapezoid(double topline, double baseline, double height) {
this.topline = topline;
this.baseline = baseline;
this.height = height;
}
public double area() {
return ((topline + baseline) * height) / 2;
}
}
相关问答
Q1: 如何用Java编写一个绘制图形的小程序?
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不规则图形的绘制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例
Point aPoint;
//构造函数
public IrregularShapeDemo() {
super("不规则图形的绘制"); //调用父类构造函数
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件
setSize(300, 200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void paint(Graphics g) { //重载窗口组件的paint()方法
Graphics2D g2D = (Graphics2D)g;//获取图形环境
g2D.draw(gPath); //绘制路径
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠标事件处理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath = new GeneralPath(); //重新实例化GeneralPath对象
gPath.moveTo(aPoint.x,aPoint.y); //设置路径点
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath.lineTo(aPoint.x, aPoint.y); //设置路径
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重绘组件
}
}
}
Q2: 用java写一个小程序
java实现先输入10个int类型的参数,之后循环判断找到最大值和最小值,保存起来和其他的值进行比较。代码如下:
public class Test{
public static void main(String args[]){
int i,min,max;
int A[] = new int[10];
Scanner input=new Scanner(System.in);
for(int j=0,j10;j++){//输入10个数
System.out.println("请输入一个数字:");
int length=input.nextInt();//输入一个数字
A[i] =length;
}
min=max=A[0];
System.out.print("数组A的元素包括:");
int j =0;
int n =0 ;
for(i=0;iA.length;i++)
{
System.out.print(A[i]+" ");
if(A[i]max) // 判断最大值
j =i;
max=A[i];
if(A[i]min) // 判断最小值
min=A[i];
n =i
}
System.out.println("\n数组的最大值是:"+max+".数组的位置是:"+(j+1)); // 输出最大值和最大值的位置
System.out.println("数组的最小值是:"+min+".数组的位置是:"+(n+1)); // 输出最小值
}
}
Q3: 如何用JAVA语言编写计算器小程序?
具体代码如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
Q4: java小程序编写
楼主,以下是我的实现方式,看看是不是你想要的,哪里不清楚的可以单独M我。
import java.util.Arrays;
import java.util.Comparator;
public class Student {
private int id;
private String name;
private String birthday;
public Student(int id, String name, String birthday) {
this.id = id;
this.name = name;
this.birthday = birthday;
}
public static void main(String[] args) {
Student s1 = new Student(1, "张三", "1983-01-22");
Student s2 = new Student(2, "张一", "1983-01-21");
Student s3 = new Student(3, "张笑一", "1983-01-27");
Student s4 = new Student(4, "张含一", "1983-01-01");
Student[] students = { s1, s2, s3, s4 }; //将4个学生对象装入数组中
//现根据出生日期对数组元素进行排序,由于String已经实现了Comparable接口,可以直接利用compareTo方法进行比较
Arrays.sort(students, new ComparatorStudent() {
public int compare(Student o1, Student o2) {
return o1.getBirthday().compareTo(o2.getBirthday());
}
});
//打印排序后的结构
System.out.println(Arrays.toString(students));
Student[] newStudents = new Student[students.length];
int i = 0;
for (Student s : students) {
if (s.getName().indexOf("一") != -1) {
newStudents[i] = s;
i++;
}
}
//打印搜索出的姓名含“一”的学生
System.out.println(Arrays.toString(newStudents));
}
@Override
public String toString() {
// TODO 自动生成方法存根
return "Student姓名:" + this.name + ",出生日期:" + this.birthday;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
关于java代码编写小程序和如何用java编写小程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








