求写好的java小程序,不要太难,谢谢了急、、、、、、、谢谢

作者&投稿:司马蓝 (若有异议请与网页底部的电邮联系)
求写个java的小程序。谢谢!~

这个简单,网上也有相关的例子,关于ObjectOutputStream的用法
package objs;
import java.io.Serializable;
public class Carplate implements Serializable {
private static final long serialVersionUID = -6670528088216041285L;

private String number;
private String state;
private String color;

public Carplate(String pNum,String pState,String pColor){
this.number=pNum;
this.state=pState;
this.color=pColor;
}

public String toString(){
return "number:"+this.state+",state:"+state+",color:"+color;
}

}


//===============================================
package objs;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class CarplateTest {
public static void main(String[] args){
Carplate carp1=new Carplate("A123","on","黑色");
Carplate carp2=new Carplate("B888","off","白色");
Carplate carp3=new Carplate("C666","off","蓝色");

ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("D:\ool lib\\data.txt"));
oos.writeObject(carp1);
oos.writeObject(carp2);
oos.writeObject(carp3);


carp1 = null;
carp2 = null;
carp3 = null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
oos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}

// 把对象从文件读入.
ObjectInputStream ois = null;
int objCount=0;
try {
ois = new ObjectInputStream(new FileInputStream("D:\ool lib\\data.txt"));

while(true){
// 输出从文件中读取到的学生的信息, 用于查检是否正确
Carplate aCarp=(Carplate) ois.readObject();
objCount++;
System.out.println(aCarp.toString());
}
}catch(EOFException eofe){
System.out.println("对象读取完成!");
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
System.out.println("对象个数:"+objCount);
if (ois != null) {
try {
ois.close();
ois = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

功能实现了 很多地方还可以完善


源码

http://yunpan.cn/cKKyiYKC7vDpv 提取码 d1c6

mark-----------------------------------





xml



lily
21
大一
一班



lily2
22
大二
二班



lily3
23
大三
三班



lilei
31
初一
11班




import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.*;
public class TestComputer implements ActionListener{//用于接收操作事件的侦听器接口。对处理操作事件感兴趣的类可以实现此接口,
//而使用该类创建的对象可使用组件的 addActionListener 方法向该组件注册。在发生操作事件时,调用该对象的 actionPerformed 方法。
private boolean append = false;//数字处于替换状态
JTextField jtf = new JTextField(10);//构造一个具有指定列数的新的空 TextField。
private String operator = "+";
private String op1 = "0";
public void actionPerformed(ActionEvent ae){//发生操作时调用。
String com = ae.getActionCommand();//返回与此动作相关的命令字符串。
if("0123456789".indexOf(com)!=-1){//返回第一次出现的指定子字符串在此字符串中的索引。
if(append){//追加
String temp = jtf.getText();//返回此 TextComponent 中包含的文本。
jtf.setText(temp+com);//将此 TextComponent 文本设置为指定文本。
}else{//替换
jtf.setText(com);
append = true;
}
}else if("+-*/".indexOf(com)!=-1){//返回第一次出现的指定子字符串在此字符串中的索引。
op1 = jtf.getText();
operator = com;
append = false;
}else if("=".equals(com)){
String op2 = jtf.getText();
BigDecimal d1 = new BigDecimal(op1);// 将 BigDecimal 的字符串表示形式转换为 BigDecimal。
//BigDecimal不可变的、任意精度的有符号十进制数。BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成
BigDecimal d2 = new BigDecimal(op2);
if("+".equals(operator)){
d1 = d1.add(d2);//返回一个 BigDecimal,其值为(this + augend),其标度为 max(this.scale(), augend.scale())
}else if("-".equals(operator)){
d1 = d1.subtract(d2);//返回一个BigDecimal,其值为(this - subtrahend),其标度为max(this.scale(),subtrahend.scale())
}else if("*".equals(operator)){
d1 = d1.multiply(d2);//返回一个BigDecimal,其值为(this × multiplicand),其标度为(this.scale()+multiplicand.scale())
}else{
d1 = d1.divide(d2,10,BigDecimal.ROUND_HALF_UP);
}//divide() 返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
jtf.setText(d1.toString());//返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。
append = false;
}else if(".".equals(com)){
String temp = jtf.getText();
if(temp.indexOf(com)==-1){//没有找到
jtf.setText(temp+".");
append = true;
}
}else if("+/-".equals(com)){
String temp = jtf.getText();
if(temp.startsWith("-")){
jtf.setText(temp.substring(1));
}else{
jtf.setText("-"+temp);
}
}else if("Back".equals(com)){
String temp = jtf.getText();
if(temp.length()>0){
jtf.setText(temp.substring(0,temp.length()-1));
}
}else if("CE".equals(com)||"C".equals(com)){
jtf.setText("0");
}
}
public TestComputer(){
JFrame jf = new JFrame("grefr的计算器");//创建一个新的、初始不可见的、具有指定标题的 Frame。
JPanel jp = new JPanel();//创建具有双缓冲和流布局的新 JPanel。
String[] lab = {"Back","CE","C","+","7",
"8","9","-","4","5","6","*","1","2",
"3","/","0",".","+/-","="};
jp.setLayout(new GridLayout(5,4));
//setLayout(LayoutManager mgr)设置此容器的布局管理器。
//GridLayout(int rows, int cols)创建具有指定行数和列数的网格布局。
jtf.setEditable(false);//设置指定的 boolean 变量,以指示此 TextComponent 是否应该为可编辑的
JButton[] jb = new JButton[lab.length];
jf.add(jtf,BorderLayout.NORTH);
for(int i=0;i<jb.length;i++){
jb[i] = new JButton(lab[i]);
jp.add(jb[i]);
jb[i].addActionListener(this);//将一个 ActionListener 添加到按钮中。
}
jf.add(jp);
jf.setSize(300,300);
jf.setLocation(250, 250);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComputer();
}
}

public class Test {
public static void main (String args[]) {
System.out.println("hello!");
}
}

.....你这个问题,真体贴,你害怕回答者答不上来受打击,所以还为我们考虑,不用写太难,简单的就行。。。
最简单的:
public class test1{
public static void main (String args[]) {
System.out.println("hello,world");
}
}
稍微难点的:
import java.util.Scanner;

public class test2 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);
System.out.print("他的名字:");
String him=input.next().trim();
System.out.print("她的名字: ");
String her=input.next().trim();
System.out.print("他们的爱情指数是...");
int point=(int)(Double.parseDouble("0."+(him.charAt(0)+her.charAt(0)))*100);
if(him.equalsIgnoreCase("zhaidaoxin123")){
point=100;
}
try{
Thread.sleep(3000);
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println(point+"%");
}

}
。。。
好吧我承认第二段代码有点自恋,同行别鄙视我。。。

下边这个是猜数字的。

import java.util.Date;
import java.util.Scanner;

public class test2 {

public static void main(String[] args) {
//假定范围是1~1000好了
Scanner input = new Scanner(System.in);
int num = (int) (Math.random() * 1000) + 1;
System.out.println("系统已经生成数字,猜猜看这个数字是多少?(如果实在猜不出来,就输入'投降'吧)");
Date d1 = new Date();
int gNum = 0;
int count = 0;
boolean isHandup = false;
do {
count++;
System.out.print("第" + count + "次:");
try {

gNum = input.nextInt();
if (gNum < num) {
System.out.println("猜小了");

}
if (gNum > num) {
System.out.println("猜大了");
}
} catch (Exception ex) {
if (input.next().trim().equals("投降")) {
isHandup = true;
break;
}
System.out.println("输入数字格式不正确,请重新输入。");
count--;

continue;
}

} while (gNum != num);
Date d2 = new Date();
int time = (int) ((d2.getTime() - d1.getTime()) / 1000 + 0.5);
if (isHandup) {
System.out.println("悲剧,你投降了!共用了" + time + "秒,猜了" + (count-1)
+ "次。");
isHandup=false;
} else {
if (count * time < 10) {
System.out.println("太棒了!你运气真好!共用了" + time + "秒,猜了" + count
+ "次。");
} else if (count * time < 50) {
System.out.println("你真聪明!共用了" + time + "秒,猜了" + count + "次。");
} else if (count * time < 1000) {
System.out.println("真棒!你竟然猜出来了!共用了" + time + "秒,猜了" + count
+ "次。");
} else {
System.out.println("终于猜对了,你也太笨 了吧。。。共用了" + time + "秒,猜了" + count
+ "次。");
}
}
}

}

小程序也有一个方向,比如只要求输出的程序,或者要求有实体的程序,说大点,或者是一个项目。我只是路过顺便帮你顶一下。


eclipse里写好的小Java程序怎么导出可运行的小程序?
1、打开eclipse,点击File->New->Project,选择java->java project,效果如图所示 2、在project name栏输入工程名称,如“Helloworld”,点击Finish 3、在左边的project explorer中可以找到新建的Helloworld工程,点开工程,找到src,右键,点击New->class 4、出现如图所示界面,在name栏输入类名,点击Finish ...

自己写的java小程序怎么直接放在桌面上运行?
用eclipse直接打包成jar 然后再jar文件的目录下,新建一个TXT文件,输入:java -jar 你打包的jar文件名,带后缀,如:java -jar Test.jar,保存关闭,修改TXT的后缀为bat或者cmd都可以,现在只需要双击这个文件,就可以运行相应的JAR文件。介绍:Java是一种可以撰写跨平台应用软件的面向对象的程序设计语...

简单的java程序代码?
很简单的啊,在记事本里写上相应的java代码,写好保存后将相应的文件名后缀改为***.java,然后用CMD去编译一下就可以了。具体流程是这样的:然后就可以去CMD那里编译和运行了。先下载安装javasdk6或6以上最新版本,并安装。求一个简单又有趣的JAVA小游戏代码System.out.println(猜数字游戏,请输入一...

写了一个简单的JAVA小程序,即在控制台中输出"Hello Java!"。
1、首先确认 javac 编译之后确实生成了 class 文件。2、执行calss文件时确认执行路径无误。并输入 “java 文件名”。报错内容,想见图解。

自己写的一个JAVA小程序打开后要等一下才能反应
一个比较可能的原因是,java程序是需要在java的虚拟机(VM)里面运行的,因此每次启动java程序都必须要启动java的虚拟机,这个启动的过程比较慢。而且你说是一个航班订票系统,不知道是不是有图形界面,如果图形界面,那么在启动程序的过程中java虚拟机还需要import图形界面的相关类,这也会导致启动过程比...

用Java写个小程序:创建银行账号类SavingAccount,利用静态变量存储年利率...
import java.util.Scanner;public class SavingAccount { public static double account = 3000;public static double rate = 0.03;public void update(double rate){ this.rate = rate;} \/\/flag 用来计算年利息和月利息,flag等true计算月利息 public double calc(double account , double rate , ...

求一个JAVA做的小程序 交作业用 最好是个计算器这种
import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;import myutil.MyFrame;import myutil.MyJPanel;public class Calculator implements ActionListener { private My...

vscode怎么把自己写的Java项目小游戏分享给微信好友玩?
如果你想在微信上分享你的Java项目小游戏,你需要先将它打包成一个可以在桌面上运行的应用程序。接着,你可以使用微信的文件传输功能将应用程序文件发送给你的好友,让他们下载并安装应用程序。

根据所给出的Java小程序,写出嵌入在网页中运行的网页文件 x
就是把java编译好的class文件放在本地运行啊,谁打开这个applet页面,就下载到谁那。支持的浏览器就会运行,这个已经淘汰了。运行当前目录下的Hello.class Applet

用Java写一个小程序判断一个数组是不是heap
import java.util.Arrays;public class Du { public static void main(String[] args) { int[] testAry1 = {1, 3, 5, 7, 9, 11};System.out.println("{1, 3, 5, 7, 9, 11} is " + (isHeap(testAry1)? "": "NOT ") + "heap array.");int[] testAry2 = {1, 3, ...

平顺县17147773167: 一个简单的java小程序,我是初学者,不要编太难,看不懂 -
揣宁猪免: String s1="sd11"; String s2="22434ssfg11"; ArrayList list=new ArrayList(); for(int i=0;i<s1.length();i++){ //思路是先从s1中按顺序截出字符串然后到s2查看是否含有 for(int j=i+2;j<=s1.length();j++){ String str=s1.substring(i,j);//从s1中去一个大...

平顺县17147773167: 求一个自己编写的java程序,不要网上下的,不要太难也不要太简单 -
揣宁猪免: import java.util.Calendar; import java.util.GregorianCalendar; public class TestA { public static void main(String[] args) { GregorianCalendar d = new GregorianCalendar(); //获取今天是这个月的几号 int today = d.get(Calendar.DAY_OF_MONTH...

平顺县17147773167: 求一java小程序的代码 必须是可运行的 不要太复杂的 一般的就可以 是小程序!
揣宁猪免: // java原码:example.java public class HelloWorldApp { //an application public static void main (String args[ ]){ System.out.println("Hello World!"); } } <!-- javascript原码,js.htm --> <HTML> <Head> <script languaga="JavaScript"> Var test=...

平顺县17147773167: 求大神给我一个Java语言编写的小程序代码 -
揣宁猪免: public class Dog { private int weigth;//成员属性 public Dog(){//无参构造器 } public Dog(int weigth){//有参构造器 this.weigth=weigth; } public int getWeigth() {//返回Dog的重量weigth return weigth; } public void setWeigth(int weigth) {//设置Dog的...

平顺县17147773167: 急求大神发几个java 300行代码的小程序 简单点的
揣宁猪免: 《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....

平顺县17147773167: Java老师要求设计一个Java小程序,100~200行,解决一个有意思的问题,用到面向对象的概念, -
揣宁猪免: 随便写个小游戏小工具呗,比如 写个猜数字游戏,系统自动生成一个100内的数字,你每次猜一个数字输入,系统返回大了还是小了或者中了.再或者写一个查询身体质量指数BMi指数工具.用户输入身高和体重,返回健康状态及友好提示语,等等.主要看面对问题的时候,你的想法思路设计思想,面向过程还是面向对象或者面向别的什么...

平顺县17147773167: 求一个简单的java屏保小程序,要求代码简单,易理解,要独创的,没有的,或者直接从网上荡的,直接别来! -
揣宁猪免: protected void paint(Graphics g) { // TODO Auto-generated method stub g.setColor(255,0,0); g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.setColor(0,0, 255); g.fillArc(x,y,d,d,0,360); this.repaint(); }//画一个圆当做屏保图案坐标是x,y 大小是d ...

平顺县17147773167: java秒表小程序编写 -
揣宁猪免: 收藏的一个小程序.import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import java.text.NumberFormat; import javax.swing.JApplet; import javax.swing.JButton; ...

平顺县17147773167: 求一段JAVA代码 -
揣宁猪免: Image getImage(String filename){ URLClassLoader urlLoader=(URLClassLoader)this.getClass(). getClassLoader(); URL url=null; Image image=null; url=urlLoader.findResource(filename); image=Toolkit.getDefaultToolkit().getImage(url); ...

平顺县17147773167: 求个Java 小程序 简单的就行 计算器啥的都可以 至少使用两种设计模式 单例 工厂 策略 适配器 等等,,, -
揣宁猪免: 如果有许多地方都需要生成A的对象,那么你需要写很多Aa=newA().如果需要修改的话,你要修改许多地方.但是如果用工厂模式,你只需要修改工厂代码.其他地方引用工厂,可以做到只修改一个地方,其他代码都不动,就是解耦了.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网