189 8069 5689

java源代码100例的简单介绍

java新手,求完整的源代码

//都是从新手过来的,以下代码供参考

站在用户的角度思考问题,与客户深入沟通,找到濮阳县网站设计与濮阳县网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站制作、成都做网站、企业官网、英文网站、手机端网站、网站推广、主机域名、网页空间、企业邮箱。业务覆盖濮阳县地区。

//1.

public class BankAccount {

private static String acctnum;

private static double money;

private static void showAcct() {

System.out.println("账号为: " + acctnum);

}

private static void showMoney() {

System.out.println("余额为: " + money);

}

public BankAccount(String acc, double m) {

this.acctnum = acc;

this.money = m;

}

public static void main(String[] args) {

BankAccount ba = new BankAccount("626600018888", 5000.00);

ba.showAcct();

ba.showMoney();

}

}

//2.

public class Triangle {

private static float a;

private static float b;

private static float c;

public Triangle(float a, float b, float c) {

this.a = a;

this.b = b;

this.c = c;

}

public static boolean judgeTriangle(float a, float b, float c) {

if ((a  Math.abs(b - c)  a  b + c)

 (b  Math.abs(a - c)  b  a + c)

 (c  Math.abs(a - b)  c  a + b))

return true;

else

return false;

}

public float getCircumference() {

return this.a + this.b + this.c;

}

}

//3.

public class TestTriangle {

public static void main(String[] args) {

Triangle t = new Triangle(5.3f,7.8f,9.3f);

if(t.judgeTriangle(5.3f,7.8f,9.3f)){

System.out.print("能够成三角形,周长为: ");

System.out.printf("%9.2f",t.getCircumference());}

else

System.out.println("不能构成三角形");

}

}

求个简单点的Java程序 100行左右。 需要解释。

贪吃蛇游戏 望采纳

import java.awt.Button;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Point;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.util.*;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class Snake extends JFrame implements KeyListener{

int Count=0;

Button[][] grid = new Button[20][20];

ArrayListPoint snake_list=new ArrayListPoint();

Point bean=new Point(-1,-1);//保存随机豆子【坐标】

int Direction = 1; //方向标志 1:上 2:下 3:左 4:右

//构造方法

public Snake()

{

//窗体初始化

this.setBounds(400,300,390,395);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GridLayout f=new GridLayout(20,20);

this.getContentPane().setBackground(Color.gray);

this.setLayout(f);

//初始化20*20个按钮

for(int i=0;i20;i++)

for(int j=0;j20;j++)

{

grid[i][j]=new Button();

this.add(grid[i][j]);

grid[i][j].setVisible(false);

grid[i][j].addKeyListener(this);

grid[i][j].setBackground(Color.blue);

}

//蛇体初始化

grid[10][10].setVisible(true);

grid[11][10].setVisible(true);

grid[12][10].setVisible(true);

grid[13][10].setVisible(true);

grid[14][10].setVisible(true);

//在动态数组中保存蛇体按钮坐标【行列】信息

snake_list.add(new Point(10,10));

snake_list.add(new Point(11,10));

snake_list.add(new Point(12,10));

snake_list.add(new Point(13,10));

snake_list.add(new Point(14,10));

this.rand_bean();

this.setTitle("总分:0");

this.setVisible(true);

}

//该方法随机一个豆子,且不在蛇体上,并使豆子可见

public void rand_bean(){

Random rd=new Random();

do{

bean.x=rd.nextInt(20);//行

bean.y=rd.nextInt(20);//列

}while(snake_list.contains(bean));

grid[bean.x][bean.y].setVisible(true);

grid[bean.x][bean.y].setBackground(Color.red);

}

//判断拟增蛇头是否与自身有碰撞

public boolean is_cross(Point p){

boolean Flag=false;

for(int i=0;isnake_list.size();i++){

if(p.equals(snake_list.get(i) )){

Flag=true;break;

}

}

return Flag;

}

//判断蛇即将前进位置是否有豆子,有返回true,无返回false

public boolean isHaveBean(){

boolean Flag=false;

int x=snake_list.get(0).x;

int y=snake_list.get(0).y;

Point p=null;

if(Direction==1)p=new Point(x-1,y);

if(Direction==2)p=new Point(x+1,y);

if(Direction==3)p=new Point(x,y-1);

if(Direction==4)p=new Point(x,y+1);

if(bean.equals(p))Flag=true;

return Flag;

}

//前进一格

public void snake_move(){

if(isHaveBean()==true){//////////////有豆子吃

Point p=new Point(bean.x,bean.y);//【很重要,保证吃掉的是豆子的复制对象】

snake_list.add(0,p); //吃豆子

grid[p.x][p.y].setBackground(Color.blue);

this.Count++;

this.setTitle("总分:"+Count);

this.rand_bean(); //再产生一个豆子

}else{///////////////////无豆子吃

//取原蛇头坐标

int x=snake_list.get(0).x;

int y=snake_list.get(0).y;

//根据蛇头坐标推算出拟新增蛇头坐标

Point p=null;

if(Direction==1)p=new Point(x-1,y);//计算出向上的新坐标

if(Direction==2)p=new Point(x+1,y);//计算出向下的新坐标

if(Direction==3)p=new Point(x,y-1);//计算出向左的新坐标

if(Direction==4)p=new Point(x,y+1);//计算出向右的新坐标

//若拟新增蛇头碰壁,或缠绕则游戏结束

if(p.x0||p.x19|| p.y0||p.y19||is_cross(p)==true){

JOptionPane.showMessageDialog(null, "游戏结束!");

System.exit(0);

}

//向蛇体增加新的蛇头坐标,并使新蛇头可见

snake_list.add(0,p);

grid[p.x][p.y].setVisible(true);

//删除原蛇尾坐标,使蛇尾不可见

int x1=snake_list.get(snake_list.size()-1).x;

int y1=snake_list.get(snake_list.size()-1).y;

grid[x1][y1].setVisible(false);

snake_list.remove(snake_list.size()-1);

}

}

@Override

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_UP Direction!=2) Direction=1;

if(e.getKeyCode()==KeyEvent.VK_DOWN Direction!=1) Direction=2;

if(e.getKeyCode()==KeyEvent.VK_LEFT Direction!=4) Direction=3;

if(e.getKeyCode()==KeyEvent.VK_RIGHT Direction!=3) Direction=4;

}

@Override

public void keyReleased(KeyEvent e) { }

@Override

public void keyTyped(KeyEvent e) { }

public static void main(String[] args) throws InterruptedException {

Snake win=new Snake();

while(true){

win.snake_move();

Thread.sleep(300);

}

}

}

速求用JAVA语言写聊天室的源代码

【ClientSocketDemo.java 客户端Java源代码】

import java.net.*;

import java.io.*;

public class ClientSocketDemo

{

//声明客户端Socket对象socket

Socket socket = null;

//声明客户器端数据输入输出流

DataInputStream in;

DataOutputStream out;

//声明字符串数组对象response,用于存储从服务器接收到的信息

String response[];

//执行过程中,没有参数时的构造方法,本地服务器在本地,取默认端口10745

public ClientSocketDemo()

{

try

{

//创建客户端socket,服务器地址取本地,端口号为10745

socket = new Socket("localhost",10745);

//创建客户端数据输入输出流,用于对服务器端发送或接收数据

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

//获取客户端地址及端口号

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

//向服务器发送数据

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

//从服务器接收数据

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//执行过程中,有一个参数时的构造方法,参数指定服务器地址,取默认端口10745

public ClientSocketDemo(String hostname)

{

try

{

//创建客户端socket,hostname参数指定服务器地址,端口号为10745

socket = new Socket(hostname,10745);

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//执行过程中,有两个个参数时的构造方法,第一个参数hostname指定服务器地址

//第一个参数serverPort指定服务器端口号

public ClientSocketDemo(String hostname,String serverPort)

{

try

{

socket = new Socket(hostname,Integer.parseInt(serverPort));

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

String comd[] = args;

if(comd.length == 0)

{

System.out.println("Use localhost(127.0.0.1) and default port");

ClientSocketDemo demo = new ClientSocketDemo();

}

else if(comd.length == 1)

{

System.out.println("Use default port");

ClientSocketDemo demo = new ClientSocketDemo(args[0]);

}

else if(comd.length == 2)

{

System.out.println("Hostname and port are named by user");

ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);

}

else System.out.println("ERROR");

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

【ServerSocketDemo.java 服务器端Java源代码】

import java.net.*;

import java.io.*;

public class ServerSocketDemo

{

//声明ServerSocket类对象

ServerSocket serverSocket;

//声明并初始化服务器端监听端口号常量

public static final int PORT = 10745;

//声明服务器端数据输入输出流

DataInputStream in;

DataOutputStream out;

//声明InetAddress类对象ip,用于获取服务器地址及端口号等信息

InetAddress ip = null;

//声明字符串数组对象request,用于存储从客户端发送来的信息

String request[];

public ServerSocketDemo()

{

request = new String[3]; //初始化字符串数组

try

{

//获取本地服务器地址信息

ip = InetAddress.getLocalHost();

//以PORT为服务端口号,创建serverSocket对象以监听该端口上的连接

serverSocket = new ServerSocket(PORT);

//创建Socket类的对象socket,用于保存连接到服务器的客户端socket对象

Socket socket = serverSocket.accept();

System.out.println("This is server:"+String.valueOf(ip)+PORT);

//创建服务器端数据输入输出流,用于对客户端接收或发送数据

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

//接收客户端发送来的数据信息,并显示

request[0] = in.readUTF();

request[1] = in.readUTF();

request[2] = in.readUTF();

System.out.println("Received messages form client is:");

System.out.println(request[0]);

System.out.println(request[1]);

System.out.println(request[2]);

//向客户端发送数据

out.writeUTF("Hello client!");

out.writeUTF("Your ip is:"+request[1]);

out.writeUTF("Your port is:"+request[2]);

}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

ServerSocketDemo demo = new ServerSocketDemo();

}

}

求java随即产生20个1到100间数的源代码?

public class Test5{

public static void main(String []args){

java.util.Random ran=new java.util.Random();

int []arr=new int[20];

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

int temp=ran.nextInt(100)+1;

arr[i]=temp;

for(int j=0;ji;j++){

if(arr[j]==temp){

i--;

break;

}

}

}

String str="您的随机数是:";

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

str=str+arr[i]+" ";

}

System.out.println(str);

}

}

在线等一个java程序源代码 急用!!!

第一题

import java.util.Random;

import java.util.Scanner;

public class Guess{

public static void main(String[] args) {

int rightNum = new Random().nextInt(100) + 1;

Scanner scanner = new Scanner(System.in);

int input = 0;

do{

System.out.print("清猜数字(1-100)!");

input = scanner.nextInt();

if(input rightNum){

System.out.println("猜大了!");

}

else if(input rightNum){

System.out.println("猜小了!");

}

}while(input != rightNum);

System.out.println("猜对了" + rightNum);

}

}

第二题

import java.util.* ;

public class A{

public static void main(String args[]){

int i,j,k,temp;

int a[][]=new int[2][3];

a[0][0]=(int)(100*Math.random());

a[0][1]=(int)(100*Math.random());

a[0][2]=(int)(100*Math.random());

a[1][0]=(int)(100*Math.random());

a[1][1]=(int)(100*Math.random());

a[1][2]=(int)(100*Math.random());

for(j=0;j3;j++)

System.out.println("a[0]["+j+"]="+a[0][j]);

System.out.println(" ");

for(j=0;j3;j++)

System.out.println("a[1]["+j+"]="+a[1][j]);

System.out.println(" ");

for(i=0;i2;i++){

for(j=0;j2;j++){

for(k=j;k2;k++){

if(a[i][j]a[i][k+1]){

temp=a[i][j];

a[i][j]=a[i][k+1];

a[i][k+1]=temp;

}

}

}

}

System.out.println("第一行按从小到大排列:");

for(j=0;j3;j++){

System.out.println("a[0]["+j+"]="+a[0][j]);

}

System.out.println("第二行按从小到大排列:");

for(j=0;j3;j++)

System.out.println("a[1]["+j+"]=" +a[1][j]);

}

}

春春??还不快采纳嘛

急求JAVA源代码,小游戏或者别的

//这是个聊天程序, 在ECLIPSE 运行 Client.java 就可以了。 连接是:localhost

//Server 代码,

package message;

import java.io.*;

import java.net.*;

import java.util.*;

public class Server {

public static void main(String[] args) throws Exception{

System.out.print("Server");

ServerSocket socket=new ServerSocket(8888);

Vector v=new Vector();

while(true){

Socket sk=socket.accept();

DataInputStream in=new DataInputStream(sk.getInputStream());

DataOutputStream out=new DataOutputStream(sk.getOutputStream());

v.add(sk);

new ServerThread(in,v).start();

}

}

}

//ServerThread.java 代码

package message;

import java.net.*;

import java.io.*;

import java.util.*;

public class ServerThread extends Thread{

DataInputStream in;

Vector all;

public ServerThread(DataInputStream in,Vector v){

this.in=in;

this.all=v;

}

public void run()

{

while(true)

{

try{

String s1=in.readUTF();

for(int i=0;iall.size();i++)

{

Object obj=all.get(i);

Socket socket=(Socket)obj;

DataOutputStream out=new DataOutputStream(socket.getOutputStream());

out.writeUTF(s1);

System.out.print(i);

out.flush();

}

System.out.print("Message send over!");

}catch(Exception e){e.printStackTrace();};

}

}

}

//ClientFrame.java 代码

package message;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ClientFrame extends JFrame implements ActionListener{

JButton b1=new JButton ("SendMessage");

JButton b2=new JButton("Link Server");

JTextField t1=new JTextField(20);

JTextField t2=new JTextField(20);

JLabel l=new JLabel("输入服务器名字:");

JTextArea area=new JTextArea(10,20);

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JPanel p3=new JPanel();

JPanel p4=new JPanel();

Socket socket;

public ClientFrame()

{

this.getContentPane().add(p1);

p2.add(new JScrollPane(area));

p3.add(t1);

p3.add(b1);

p4.add(l);

p4.add(t2);

p4.add(b2);

p2.setLayout(new FlowLayout());

p3.setLayout(new FlowLayout());

p4.setLayout(new FlowLayout());

p1.setLayout(new BorderLayout());

p1.add("North",p2);

p1.add("Center",p3);

p1.add("South",p4);

b1.addActionListener(this);

b2.addActionListener(this);

this.pack();

show();

}

public void actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("Link Server"))

{

try{

socket=new Socket(t2.getText(),8888);

b2.setEnabled(false);

JOptionPane.showMessageDialog(this, "Connection Success");

DataInputStream in=new DataInputStream(socket.getInputStream());

new ClientThread(in,area).start();

}

catch(Exception e1){

JOptionPane.showMessageDialog(this, "Connection Error");

e1.printStackTrace();};

}

else if(e.getActionCommand().equals("SendMessage"))

{

try{

DataOutputStream out=new DataOutputStream(socket.getOutputStream());

out.writeUTF(t1.getText());

t1.setText("");

}catch(Exception e1){e1.printStackTrace();};

}

}

}

//ClientThread.java 代码

package message;

import java.net.*;

import java.io.*;

import javax.swing.*;

public class ClientThread extends Thread {

DataInputStream in;

JTextArea area;

public ClientThread(DataInputStream in,JTextArea area){

this.in=in;

this.area=area;

}

public void run()

{

while(true){

try{

String s=in.readUTF();

area.append(s);

}

catch(Exception e){e.printStackTrace();};

}

}

}

//Client.java代码

package message;

public class Client {

/**

* @param args

*/

public static void main(String[] args) {

new ClientFrame();

}

}

// 每段代码都是个类,不要弄在一个文件里。 运行 Client.java

good luck to you!


分享文章:java源代码100例的简单介绍
文章起源:http://cdxtjz.cn/article/hsohje.html

其他资讯