//都是從新手過來的,以下代碼供參考
站在用戶的角度思考問題,與客戶深入溝通,找到濮陽縣網(wǎng)站設(shè)計(jì)與濮陽縣網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋濮陽縣地區(qū)。
//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("不能構(gòu)成三角形");
}
}
貪吃蛇游戲 望采納
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);//保存隨機(jī)豆子【坐標(biāo)】
int Direction = 1; //方向標(biāo)志 1:上 2:下 3:左 4:右
//構(gòu)造方法
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);
//在動態(tài)數(shù)組中保存蛇體按鈕坐標(biāo)【行列】信息
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);
}
//該方法隨機(jī)一個豆子,且不在蛇體上,并使豆子可見
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;
}
//判斷蛇即將前進(jìn)位置是否有豆子,有返回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;
}
//前進(jìn)一格
public void snake_move(){
if(isHaveBean()==true){//////////////有豆子吃
Point p=new Point(bean.x,bean.y);//【很重要,保證吃掉的是豆子的復(fù)制對象】
snake_list.add(0,p); //吃豆子
grid[p.x][p.y].setBackground(Color.blue);
this.Count++;
this.setTitle("總分:"+Count);
this.rand_bean(); //再產(chǎn)生一個豆子
}else{///////////////////無豆子吃
//取原蛇頭坐標(biāo)
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
//根據(jù)蛇頭坐標(biāo)推算出擬新增蛇頭坐標(biāo)
Point p=null;
if(Direction==1)p=new Point(x-1,y);//計(jì)算出向上的新坐標(biāo)
if(Direction==2)p=new Point(x+1,y);//計(jì)算出向下的新坐標(biāo)
if(Direction==3)p=new Point(x,y-1);//計(jì)算出向左的新坐標(biāo)
if(Direction==4)p=new Point(x,y+1);//計(jì)算出向右的新坐標(biāo)
//若擬新增蛇頭碰壁,或纏繞則游戲結(jié)束
if(p.x0||p.x19|| p.y0||p.y19||is_cross(p)==true){
JOptionPane.showMessageDialog(null, "游戲結(jié)束!");
System.exit(0);
}
//向蛇體增加新的蛇頭坐標(biāo),并使新蛇頭可見
snake_list.add(0,p);
grid[p.x][p.y].setVisible(true);
//刪除原蛇尾坐標(biāo),使蛇尾不可見
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);
}
}
}
【ClientSocketDemo.java 客戶端Java源代碼】
import java.net.*;
import java.io.*;
public class ClientSocketDemo
{
//聲明客戶端Socket對象socket
Socket socket = null;
//聲明客戶器端數(shù)據(jù)輸入輸出流
DataInputStream in;
DataOutputStream out;
//聲明字符串?dāng)?shù)組對象response,用于存儲從服務(wù)器接收到的信息
String response[];
//執(zhí)行過程中,沒有參數(shù)時(shí)的構(gòu)造方法,本地服務(wù)器在本地,取默認(rèn)端口10745
public ClientSocketDemo()
{
try
{
//創(chuàng)建客戶端socket,服務(wù)器地址取本地,端口號為10745
socket = new Socket("localhost",10745);
//創(chuàng)建客戶端數(shù)據(jù)輸入輸出流,用于對服務(wù)器端發(fā)送或接收數(shù)據(jù)
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//獲取客戶端地址及端口號
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
//向服務(wù)器發(fā)送數(shù)據(jù)
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
//從服務(wù)器接收數(shù)據(jù)
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();}
}
//執(zhí)行過程中,有一個參數(shù)時(shí)的構(gòu)造方法,參數(shù)指定服務(wù)器地址,取默認(rèn)端口10745
public ClientSocketDemo(String hostname)
{
try
{
//創(chuàng)建客戶端socket,hostname參數(shù)指定服務(wù)器地址,端口號為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();}
}
//執(zhí)行過程中,有兩個個參數(shù)時(shí)的構(gòu)造方法,第一個參數(shù)hostname指定服務(wù)器地址
//第一個參數(shù)serverPort指定服務(wù)器端口號
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 服務(wù)器端Java源代碼】
import java.net.*;
import java.io.*;
public class ServerSocketDemo
{
//聲明ServerSocket類對象
ServerSocket serverSocket;
//聲明并初始化服務(wù)器端監(jiān)聽端口號常量
public static final int PORT = 10745;
//聲明服務(wù)器端數(shù)據(jù)輸入輸出流
DataInputStream in;
DataOutputStream out;
//聲明InetAddress類對象ip,用于獲取服務(wù)器地址及端口號等信息
InetAddress ip = null;
//聲明字符串?dāng)?shù)組對象request,用于存儲從客戶端發(fā)送來的信息
String request[];
public ServerSocketDemo()
{
request = new String[3]; //初始化字符串?dāng)?shù)組
try
{
//獲取本地服務(wù)器地址信息
ip = InetAddress.getLocalHost();
//以PORT為服務(wù)端口號,創(chuàng)建serverSocket對象以監(jiān)聽該端口上的連接
serverSocket = new ServerSocket(PORT);
//創(chuàng)建Socket類的對象socket,用于保存連接到服務(wù)器的客戶端socket對象
Socket socket = serverSocket.accept();
System.out.println("This is server:"+String.valueOf(ip)+PORT);
//創(chuàng)建服務(wù)器端數(shù)據(jù)輸入輸出流,用于對客戶端接收或發(fā)送數(shù)據(jù)
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//接收客戶端發(fā)送來的數(shù)據(jù)信息,并顯示
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]);
//向客戶端發(fā)送數(shù)據(jù)
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();
}
}
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="您的隨機(jī)數(shù)是:";
for(int i=0;iarr.length;i++){
str=str+arr[i]+" ";
}
System.out.println(str);
}
}
第一題
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("清猜數(shù)字(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]);
}
}
春春??還不快采納嘛
//這是個聊天程序, 在ECLIPSE 運(yùn)行 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("輸入服務(wù)器名字:");
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();
}
}
// 每段代碼都是個類,不要弄在一個文件里。 運(yùn)行 Client.java
good luck to you!
分享名稱:java源代碼100例的簡單介紹
路徑分享:http://jinyejixie.com/article24/hsohje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、手機(jī)網(wǎng)站建設(shè)、云服務(wù)器、品牌網(wǎng)站制作、網(wǎng)站策劃、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)