网站首页 > 文章中心 > 其它

java源代码100例的简单介绍

作者:小编 更新时间:2023-09-21 08:14:35 浏览量:81人看过

java新手,求完整的源代码

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

java源代码100例的简单介绍-图1

//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);

java源代码100例的简单介绍-图2

public?BankAccount(String?acc,?double?m)?{

this.acctnum?=?acc;

this.money?=?m;

public?static?void?main(String[]?args)?{

ba.showAcct();

ba.showMoney();

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)?{

java源代码100例的简单介绍-图3

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;

public?class?TestTriangle?{

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

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;

ArrayListPoint snake_list=new ArrayListPoint();

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

//构造方法

public Snake()

{

//窗体初始化

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

this.setLayout(f);

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);

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

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

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

this.rand_bean();

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

this.setVisible(true);

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

public void rand_bean(){

Random rd=new Random();

do{

}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(){

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(bean.equals(p))Flag=true;

//前进一格

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{///////////////////无豆子吃

//取原蛇头坐标

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

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

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

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) {

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

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

Snake win=new Snake();

while(true){

win.snake_move();

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

import java.net.*;

import java.io.*;

public class ClientSocketDemo

Socket socket = null;

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

DataInputStream in;

DataOutputStream out;

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

String response[];

public ClientSocketDemo()

try

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);

//从服务器接收数据

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 ClientSocketDemo(String hostname)

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

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

public ClientSocketDemo(String hostname,String serverPort)

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

public static void main(String[] args)

String comd[] = args;

if(comd.length == 0)

ClientSocketDemo demo = new ClientSocketDemo();

else if(comd.length == 1)

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

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

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源代码】

public class ServerSocketDemo

//声明ServerSocket类对象

ServerSocket serverSocket;

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

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

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

InetAddress ip = null;

String request[];

public ServerSocketDemo()

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

ip = InetAddress.getLocalHost();

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

serverSocket = new ServerSocket(PORT);

Socket socket = serverSocket.accept();

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

request[0] = in.readUTF();

request[1] = in.readUTF();

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

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

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

out.writeUTF("Hello client!");

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

ServerSocketDemo demo = new ServerSocketDemo();

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

public static void main(String []args){

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

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="您的随机数是:";

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;

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;

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

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

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

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

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

System.out.println(" ");

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

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("第一行按从小到大排列:");

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

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

春春?还不快采纳嘛

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

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

//Server 代码,

package message;

public class Server {

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

System.out.print("Server");

Vector v=new Vector();

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 代码

public class ServerThread extends Thread{

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 代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ClientFrame extends JFrame implements ActionListener{

JButton b1=new JButton ("SendMessage");

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

JPanel p1=new JPanel();

Socket socket;

public ClientFrame()

this.getContentPane().add(p1);

p1.setLayout(new BorderLayout());

b1.addActionListener(this);

this.pack();

show();

public void actionPerformed(ActionEvent e)

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

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"))

out.writeUTF(t1.getText());

t1.setText("");

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

//ClientThread.java 代码

public class ClientThread extends Thread {

JTextArea area;

public ClientThread(DataInputStream in,JTextArea area){

this.area=area;

String s=in.readUTF();

area.append(s);

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

//Client.java代码

public class Client {

/**

* @param args

*/

new ClientFrame();

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

good luck to you!

以上就是土嘎嘎小编为大家整理的java源代码100例的简单介绍java源代码100例的简单介绍相关咨询咨询主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章