1. 求一个简单的JAVA小程序,急急,今天就要,在线等
给你个简单的,你看看吧:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test1 {
private static int count = 0;
public static void allStuInfo(Student [] stu) {
System.out.println();
System.out.println("学生信息:");
System.out.println("姓名 课程 成绩");
for (int i = 0; i < stu.length; i++) {
if (stu[i] == null) {
break;
}
System.out.print(stu[i].getName() + " ");
System.out.print(stu[i].getScore().getCourse() + " ");
System.out.println(stu[i].getScore().getScore());
}
}
public static void addStuInfo(Student [] stu, BufferedReader in) throws IOException {
while (true) {
System.out.println("输入学生姓名:(输入0退出添加)");
String name = in.readLine();
if (name.equals("0")) {
break;
}
stu[count] = new Student();
stu[count].setName(name);
System.out.println("输入课程名:");
String course = in.readLine();
System.out.println("输入课程成绩:");
String str = in.readLine();
float score = str.equals("") ? 0 : Float.parseFloat(str);
Score s = new Score();
s.setCourse(course);
s.setScore(score);
stu[count].setScore(s);
count++;
}
}
public static void inqStuInfo(Student [] stu, BufferedReader in) throws IOException {
while(true) {
System.out.println("输入学生姓名:(输入0退出查询)");
String name = in.readLine();
if (name.equals("0")) {
break;
}
for (int i = 0; i < stu.length; i++) {
if (stu[i] == null) {
break;
}
if (stu[i].getName().equals(name)) {
System.out.print(stu[i].getName() + " ");
System.out.print(stu[i].getScore().getCourse() + " ");
System.out.println(stu[i].getScore().getScore());
}
}
}
}
public static void main(String[] args) {
Student [] stu = new Student[20];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
int count = 0;
while (count <= 20) {
System.out.println("输入1添加学生信息");
System.out.println("输入2查询学生信息");
System.out.println("输入3列出学生信息");
System.out.println("输入4退出");
String cmd = in.readLine();
if (cmd.equals("1")) {
addStuInfo(stu, in);
} else if (cmd.equals("2")) {
inqStuInfo(stu, in);
} else if (cmd.equals("3")) {
allStuInfo(stu);
} else if (cmd.equals("4")) {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Score {
private String course; // 课程
private float score; // 成绩
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
class Student {
private String name; // 姓名
private Score score; // 成绩
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
}
2. 求一个比较有趣的java小程序的源代码
public class Demo{ public static void main(String args[]){ System.out.println("有趣的java小程序"); }}
3. 高分悬赏求一JAVA小程序!!在线等!!!
/*
* 这个程序实现输入身高算出标准体重,输入体重,算出身高的功能
*/
import java.awt.*; //导入相关类包,这才样使用相应awt图形界面的类
import java.awt.event.*;//同上
public class Change extends Frame { //定义一个类Change, 父类是Frame(图形界面的)
Button b = new Button("互查"); //创建一个按钮的对象b,显示为"互查"
Label l1 = new Label("身高(cm)");//创建一个lable.显示身高
Label l2 = new Label("体重(kg)");//创建一个lable 显示体重
double heigth, weigth; //定义变量
double x, y; //定义变量
TextField tf1 = new TextField(null, 10);//添加Text框
TextField tf2 = new TextField(null, 10);//添加Text框
public Change() {//类的构造函数,完成初始化
super("互查表");//创建窗口,标题为互查表
setLayout(new FlowLayout(FlowLayout.LEFT));//设置布局
add(l1);//把lable 身高放到window里
add(tf1);//把Text 框 放到窗口上
add(l2); //把lable 体重放到window里
add(tf2);//Test放到窗口里
add(b);//把button放到窗口上
pack();//自动放到窗口里排列上边的组件
setVisible(true);//可以让用户看到窗口
addWindowListener(new WindowAdapter() {//如果按 X, 关闭窗口
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());//添加button监听函数
}
class ButtonListener implements ActionListener {//实现click button时功能操作
public void actionPerformed(ActionEvent e) {//当click调用
if (tf1.getText()!=null) {//检查tf1 test 是否为空
try {//取异常
x = Double.parseDouble(tf1.getText());//字符转为double型
weigth = (x - 100) * 0.9;//算重量
tf2.setText("" + weigth);//显示重量
} catch (NumberFormatException ex) {
tf1.setText("");//如果输入不是数字,设为空
}
}
if (tf1.getText().equals("")==true){//tf1是否为空
y = Double.parseDouble(tf2.getText());//把tf2里的文本转为double 型 的
heigth = y / 0.9 + 100; //算身高根据重量
tf1.setText("" + heigth);}//显示身高
}
}
public static void main(String[] args) {//主函数,程序入口
new Change(); //建立类Change的对象,并调用他的构造函数Change().显示窗口
}
}
4. 求JAVA入门小程序源代码
mport java.util.*;
public class HuiWen
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("please input a String:");
String st=in.nextLine();
String s=st.toLowerCase();
int i=0;
int j=s.length()-1;
boolean t=true;
char first=s.charAt(i);
char last=s.charAt(j);
for(;i<j;)
{
while(!(first>='a'&&first<='z'))
{
i++;
first=s.charAt(i);
}
while(!(last>='a'&&last<='z'))
{
j--;
last=s.charAt(i);
}
if(i>=j)
break;
if(first==last)
{
i++;
j--;
first=s.charAt(i);
last=s.charAt(j);
}
else
{
t=false;
break;
}
}
if(t)
System.out.println(st+"是回文串");
else
System.out.println(st+"不是回文串");
}
}
//判断是否为回文字串
//什么实用的程序,说具体点!你要计算器的程序吗?
//下面是计算器的程序,把分拿来吧!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame=new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel=new CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends Panel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());
result=0;
lastCommand="=";
start=true;
flag=true;
display=new JButton("0");
display.setEnabled(false);
add(display,BorderLayout.NORTH);
ActionListener insert=new InsertAction();
ActionListener command=new CommandAction();
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
addButton("7",insert);
addButton("8",insert);
addButton("9",insert);
addButton("/",command);
addButton("CE",command);
addButton("4",insert);
addButton("5",insert);
addButton("6",insert);
addButton("*",command);
addButton("Backspace",command);
addButton("1",insert);
addButton("2",insert);
addButton("3",insert);
addButton("-",command);
addButton("sqrt",command);
addButton("0",insert);
addButton(".",insert);
addButton("=",command);
addButton("+",command);
addButton("1/x",command);
add(panel,BorderLayout.CENTER);
}
private void addButton(String label,ActionListener listener)
{
JButton button=new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input=event.getActionCommand();
if(start&&flag)
{
display.setText("");
start=false;
}
if(flag)
display.setText(display.getText()+input);
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("CE"))
{
display.setText("0");
start=true;
flag=true;
command="=";
}
else
if(start&&flag)
{
if(command.equals("-"))
{
display.setText(command);
start=false;
}
else
if((command.equals("1/x")||command.equals("sqrt"))&&flag)
calculate(Double.parseDouble(display.getText()),command);
else
if(flag)
lastCommand=command;
}
else
{
if(command.equals("Backspace")&&flag)
{
String s=display.getText();
char[] s1=s.toCharArray();
if(s.length()>=2)
{
String s2=new String(s1,0,s.length()-1);
display.setText(s2);
}
else
{
display.setText("0");
start=true;
}
}
else if(flag)
{
calculate(Double.parseDouble(display.getText()),command);
lastCommand=command;
start=true;
}
}
}
}
public void calculate(double x,String command)
{
if(lastCommand.equals("+")) result+=x;
else if(lastCommand.equals("-")) result-=x;
else if(lastCommand.equals("/"))
{
if(x!=0)
result/=x;
else
{
display.setText("除数不能为0");
start=false;
flag=false;
return;
}
}
else if(lastCommand.equals("*")) result*=x;
else if(command.equals("1/x"))
{
if(x!=0)
result=1/x;
else
{
display.setText("除数不能为0");
start=false;
flag=false;
return;
}
}
else if(command.equals("sqrt"))
{
if(x>=0)
result=Math.sqrt(x);
else
{
display.setText("函数输入无效");
start=false;
flag=false;
return;
}
}
else if(lastCommand.equals("=")) result=x;
display.setText(""+result);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
private boolean flag;
}
5. 求一个java 小程序的源代码
楼上连什么是小程序(applet)都不懂就把代码贴上来了。。。。。。
6. 求完整简单的java计时器小程序代码,急!!
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.PopupMenu;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class jishiqi extends JFrame {
final TextField txt = new TextField(10);
JLabel lbl = new JLabel();
// JLabel lb2 = new JLabel();
Date now = new Date();
public jishiqi() {
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
final Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date now2 = new Date(now.getTime() + 1000);
now = now2;
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
lbl.setText(formatter.format(now));
}
});
Button b1 = new Button("开始计时");
Button b2 = new Button("停止计时");
Button b3 = new Button("记录时间");
b3.setBounds(50, 50, 50, 50);
b2.setBounds(40, 40, 40, 40);
b1.setBounds(30, 30, 30, 30);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Button b = (Button) e.getSource();
b.setLabel("开始计时");
timer.start();
b.setBackground(Color.yellow);
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Button b = (Button) e.getSource();
b.setLabel("停止计时");
timer.stop();
b.setBackground(Color.yellow);
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Button b = (Button) e.getSource();
Date now3 = new Date(now.getTime() + 1000);
now = now3;
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
// String s=(new
// SimpleDateFormat("yyyy-MM-dd")).format(Calendar.getInstance().getTime());
String s = formatter.format(now).toString();
txt.setText(s);
Button b = (Button) e.getSource();
b.setLabel("记录时间");
b.setBackground(Color.yellow);
}
});
this.setLayout(new FlowLayout());
this.add(txt);
this.add(b3);
this.add(b2);
this.add(b1);
this.add(lbl);
this.setSize(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
jishiqi t = new jishiqi();
}
}
差不多了,界面你自己优化一下吧。
7. 求一个JAVA小程序的源代码,要求如下
大概是这个样子。
------------------------------------------------------------------------------------------
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DateCheck extends JFrame implements ActionListener {
private boolean isOval = true;
public DateCheck() {
setSize(567, 419);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Oval");
btnNewButton.addActionListener(this);
btnNewButton.setBounds(80, 10, 93, 23);
getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Rect");
btnNewButton_1.setBounds(203, 10, 93, 23);
btnNewButton_1.addActionListener(this);
getContentPane().add(btnNewButton_1);
JPanel panel = new MyPanel();
panel.setBounds(12, 47, 537, 337);
getContentPane().add(panel);
new Timer().schedule(new MyTimesk(), new Date(), 100);
setVisible(true);
}
public static void main(String[] args) {
new DateCheck();
}
public void actionPerformed(ActionEvent event) {
isOval = "Oval".equals(event.getActionCommand());
System.out.println(isOval);
}
class MyPanel extends JPanel implements MouseListener {
Point point1 = null;
Point point2 = null;
// protected void paintComponent(Graphics g) {
// super.paintComponent(g);
//
// }
int click = 0;
public MyPanel() {
addMouseListener(this);
}
public void paint(Graphics g) {
super.paint(g);
Point p = this.getMousePosition();
if (p == null && click % 2 == 1) {
return;
}
if (click % 2 == 0 && (point1 == null || point2 == null)) {
return;
}
if (click % 2 == 0) {
if (isOval) {
int w = point2.x - point1.x;
int h = point2.y - point1.y;
int r = (int) Math.sqrt(w * w + h * h);
g.drawOval(point1.x - r / 2, point1.y - r / 2, r + r / 2, r
+ r / 2);
} else {
g.drawRect(point1.x, point1.y, point2.x - point1.x,
point2.y - point1.y);
}
return;
}
if (isOval) {
if (click % 2 == 1) {
int w = p.x - point1.x;
int h = p.y - point1.y;
int r = (int) Math.sqrt(w * w + h * h);
g.drawOval(point1.x - r / 2, point1.y - r / 2, r + r / 2, r
+ r / 2);
}
} else {
g.drawRect(point1.x, point1.y, p.x - point1.x, p.y - point1.y);
}
}
public void mouseClicked(MouseEvent mouseevent) {
click++;
if (click % 2 == 1) {
point1 = mouseevent.getPoint();
} else {
point2 = mouseevent.getPoint();
}
}
public void mouseEntered(MouseEvent mouseevent) {
}
public void mouseExited(MouseEvent mouseevent) {
}
public void mousePressed(MouseEvent mouseevent) {
}
public void mouseReleased(MouseEvent mouseevent) {
}
}
class MyTimesk extends TimerTask {
public void run() {
repaint();
}
}
}
8. 求简单的JAVA小程序的源代码
《Java就业培训教程》P34源码
程序清单:Promote.java
class Promote
{
public static void main(String args[])
{
byte b = 50;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}
《Java就业培训教程》P35源码
程序清单:TestScope.java
public class TestScope
{
public static void main(String[] args)
{
int x = 12;
{
int q = 96; // x和q都可用
System.out.println("x is "+x);
System.out.println("q is "+q);
}
q = x; /* 错误的行,只有x可用, q 超出了作用域范围 */
System.out.println("x is "+x);
}
}
《Java就业培训教程》P37源码
程序清单:TestVar.java
public class TestVar
{
public static void main(String [] args)
{
int x;//应改为int x=0;
x=x+1; //这个x由于没有初始化,编译会报错。
System.out.println("x is "+x);
}
}
程序清单:Func1.java
public class Func1
{
public static void main(String [] args)
{
/* 下面是打印出第一个矩形的程序代码*/
for(int i=0;i<3;i++)
{
for(int j=0;j<5;j++)
{
System.out.print("*");
}
System.out.println(); //换行
}
System.out.println();
//下面是打印出第二个矩形的程序代码
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
//下面是打印出第三个矩形的程序代码
for(int i=0;i<6;i++)
{
for(int j=0;j<10;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
}
}
《Java就业培训教程》P39源码
程序清单:Func2.java
public class Func2
{
public static void drawRectangle(int x,int y)
{
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
System.out.print(" * ");
}
System.out.println(); //换行
}
System.out.println();
}
public static void main(String [] args)
{
drawRectangle(3,5);
drawRectangle(2,4);
drawRectangle(6,10);
}
}
《Java就业培训教程》P40源码
程序清单:Func3.java
public class Func3
{
public static int getArea(int x,int y)
{
return x*y;
}
public static void main(String [] args)
{
int area = getArea(3,5);
System.out.println("first Acreage is " + area);
System.out.println("second Acreage is "+ getArea(2,4));
getArea(6,10);
}
}
《Java就业培训教程》P41源码
程序清单:Func4.java
public class Func4
{
public static int getArea(int x,int y)
{
if(x<=0 || y<=0)
{
return -1;
}
return x*y;
}
public static void main(String [] args)
{
int area = getArea(3,5);
System.out.println("first Acreage is " + area);
System.out.println("second Acreage is "+ getArea(2,4));
getArea(6,10);
}
}
《Java就业培训教程》P43源码
程序清单:Test.java
public class Test
{
public static void main(String [] args)
{
int isum;
double fsum;
isum=add(3,5);
isum=add(3,5,6);
fsum=add(3.2,6.5);
}
public static int add(int x,int y)
{
reutrn x+y;
}
public static int add(int x,int y,int z)
{
return x+y+z;
}
public static double add(double x,double y)
{
return x+y;
}
}
《Java就业培训教程》P47源码
public class TestAnd
{
public static void main(String[] args)
{
int x=0;
int y=0;
if(x!=0 && y==y/x)
System.out.println("y = "+y);
}
}
《Java就业培训教程》P48源码
程序清单:ShiftTest.java
public class ShiftTest
{
public static void main(String [] args)
{
int x=0x80000000;
int y=0x80000000;
x=x>>1;
y=y>>>1;
System.out.println("0x80000000>>1 = " + Integer.toHexString(x));
System.out.println("0x80000000>>>1 = " + Integer.toHexString(y));
}
}
《Java就业培训教程》P61源码
程序清单:TestDo.java
public class TestDo
{
public static void main(String[] args)
{
int x=3;
while(x==0)
{
System.out.println("ok1");
x++;
}
int y=3;
do
{
System.out.println("ok2");
y++;
}
while(y==0);
}
}
《Java就业培训教程》P64源码
程序清单:PrintOddNum.java
public class PrintOddNum
{
public static void main(String [] args)
{
for(int i=0;i<10;i++)
{
if(i%2==0)
continue;
System.out.println(i);
}
}
}
《Java就业培训教程》P67源码
程序清单:TestArray.java
public class TestArray
{
public static void main(String [] args)
{
int x[];
x=new int[100];
for(int i=0;i<100;i++)
{
System.out.println("x"+i+" is "+x[i]);
}
}
}
《Java就业培训教程》P73源码
程序清单:TestArrayCopy.java
public class TestArrayCopy
{
public static void main(String [] args)
{
int ia[]=new int[]{1,2,3,4,5};
int ib[]=new int[]{9,8,7,6,5,4,3};
System.arraycopy(ia,0,ib,0,3);
// 复制源数组中从下标0开始的3个元素到目的数组,从下标0的位置开始存储。
for(int i=0;i<ia.length;i++)
System.out.print(ia[i]);
System.out.println();
for(int j=0;j<ib.length;j++)
System.out.print(ib[j]);
System.out.println();
}
}