㈠ java源代码
packagecom.;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
importstaticjava.lang.System.out;
importstaticjava.lang.System.in;
/**
*Createdbytracyon2017/10/11.
*/
publicclassLR{
staticint[]numbers;
staticList<Domain>domains=newArrayList<>();
publicstaticvoidmain(String...asd){
intm,n;
out.print("请输入数字个数N:");
n=input(1,Integer.MAX_VALUE);
out.print("请输入计算行数M:");
m=input(1,10000);
out.println("请输入"+n+"个数字,每个数字介于1跟1000000之间,用空格隔开:");
numbers=inputNumbers(n);
for(inti=0;i<m;i++){
domains.add(inputDomain(n,i+1));
}
for(Domaindomain:domains){
domain.output();
}
}
/**
*输入一个介于min跟max之间的整数
*@parammin
*@parammax
*@return
*/
privatestaticintinput(intmin,intmax){
Scannerscanner=newScanner(in);
try{
inti=scanner.nextInt();
if(i<min||i>max){
out.print("输入数字大小不符合要求,请重新输入”");
returninput(min,max);
}
returni;
}catch(Exceptione){
out.print("您输入的不是数字,请重新输入“");
returninput(min,max);
}
}
/**
*输入N个介于1到1000000之间的整数
*@paramn
*@return
*/
privatestaticint[]inputNumbers(intn){
Scannerscanner=newScanner(in);
StringnumStr=scanner.nextLine();
String[]nums=numStr.trim().split("\s+");
if(nums.length!=n){
out.println("输入数字个数不对,请重新输入:");
returninputNumbers(n);
}
int[]numbers=newint[n];
for(inti=0;i<n;i++){
try{
intnumber=Integer.valueOf(nums[i]);
if(number<1||number>10000){
out.print("输入数字大小不符合要求,请重新输入”");
numbers[i]=input(1,1000000);
}else{
numbers[i]=number;
}
}catch(NumberFormatExceptione){
out.print(String.format("您输入的%s不是数字,请重新输入:",nums[i]));
numbers[i]=input(1,1000000);
}
}
returnnumbers;
}
(intn,intindex){
out.println("请输入第"+index+"行的2个数字L,R(1<=L<=R<=%d)用空格隔开:");
Scannerscanner=newScanner(in);
StringnumStr=scanner.nextLine();
String[]nums=numStr.trim().split("\s+");
if(nums.length!=2){
out.print(String.format("输入数字个数不对,请重新输入!",n));
returninputDomain(n,index);
}
Stringls=nums[0];
Stringrs=nums[1];
try{
intl=Integer.valueOf(ls);
intr=Integer.valueOf(rs);
if(l<1||r<l||r>n){
out.print(String.format("输入数字大小不符合,请重新输入!",n));
returninputDomain(n,index);
}
returnnewDomain(l,r,index);
}catch(NumberFormatExceptione){
out.println(String.format("输入数字格式错误,请重新输入2个数字L,R(1<=L<=R<=%d)用空格隔开:",n));
returninputDomain(n,index);
}
}
privatestaticclassDomain{
intl;
intr;
intindex;
Domain(intl,intr,intindex){
this.l=l;
this.r=r;
this.index=index;
}
voidoutput(){
intresult=0;
for(inti=l-1;i<r;i++){
result+=numbers[i];
}
out.println(String.format("第%d行:第%d到第%d个数之和为:%d",index,l,r,result));
}
}
}
㈡ java中如何根据一个网址获得该网页的源代码
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
private String u;
private String encoding;
public static void main(String[] args) throws Exception {
HttpTest client = new HttpTest("http://www..com/", "UTF-8");
client.run();
}
public HttpTest(String u, String encoding) {
this.u = u;
this.encoding = encoding;
}
public void run() throws Exception {
URL url = new URL(u);// 根据链接(字符串格式),生成一个URL对象
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();// 打开URL
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), encoding));// 得到输入流,即获得了网页的内容
String line; // 读取输入流的数据,并缺兄显示
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
㈢ 急求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;i<all.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实例源代码
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public class Counter extends Frame
{
//声明三个面板的布局
GridLayout gl1,gl2,gl3;
Panel p0,p1,p2,p3;
JTextField tf1;
TextField tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;
StringBuffer str;//显示屏所显示的字符串
double x,y;//x和y都是运算数
int z;//Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"
static double m;//记忆的数字
public Counter()
{
gl1=new GridLayout(1,4,10,0);//实例化三个面板的布局
gl2=new GridLayout(4,1,0,15);
gl3=new GridLayout(4,5,10,15);
tf1=new JTextField(27);//显示屏
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setText("0");
tf2=new TextField(10);//显示记忆的索引值
tf2.setEditable(false);
//实例化所有按钮、设置其前景色并注册监听器
b0=new Button("Backspace");
b0.setForeground(Color.red);
b0.addActionListener(new Bt());
b1=new Button("CE");
b1.setForeground(Color.red);
b1.addActionListener(new Bt());
b2=new Button("C");
b2.setForeground(Color.red);
b2.addActionListener(new Bt());
b3=new Button("MC");
b3.setForeground(Color.red);
b3.addActionListener(new Bt());
b4=new Button("MR");
b4.setForeground(Color.red);
b4.addActionListener(new Bt());
b5=new Button("MS");
b5.setForeground(Color.red);
b5.addActionListener(new Bt());
b6=new Button("M+");
b6.setForeground(Color.red);
b6.addActionListener(new Bt());
b7=new Button("7");
b7.setForeground(Color.blue);
b7.addActionListener(new Bt());
b8=new Button("8");
b8.setForeground(Color.blue);
b8.addActionListener(new Bt());
b9=new Button("9");
b9.setForeground(Color.blue);
b9.addActionListener(new Bt());
b10=new Button("/");
b10.setForeground(Color.red);
b10.addActionListener(new Bt());
b11=new Button("sqrt");
b11.setForeground(Color.blue);
b11.addActionListener(new Bt());
b12=new Button("4");
b12.setForeground(Color.blue);
b12.addActionListener(new Bt());
b13=new Button("5");
b13.setForeground(Color.blue);
b13.addActionListener(new Bt());
b14=new Button("6");
b14.setForeground(Color.blue);
b14.addActionListener(new Bt());
b15=new Button("*");
b15.setForeground(Color.red);
b15.addActionListener(new Bt());
b16=new Button("%");
b16.setForeground(Color.blue);
b16.addActionListener(new Bt());
b17=new Button("1");
b17.setForeground(Color.blue);
b17.addActionListener(new Bt());
b18=new Button("2");
b18.setForeground(Color.blue);
b18.addActionListener(new Bt());
b19=new Button("3");
b19.setForeground(Color.blue);
b19.addActionListener(new Bt());
b20=new Button("-");
b20.setForeground(Color.red);
b20.addActionListener(new Bt());
b21=new Button("1/X");
b21.setForeground(Color.blue);
b21.addActionListener(new Bt());
b22=new Button("0");
b22.setForeground(Color.blue);
b22.addActionListener(new Bt());
b23=new Button("+/-");
b23.setForeground(Color.blue);
b23.addActionListener(new Bt());
b24=new Button(".");
b24.setForeground(Color.blue);
b24.addActionListener(new Bt());
b25=new Button("+");
b25.setForeground(Color.red);
b25.addActionListener(new Bt());
b26=new Button("=");
b26.setForeground(Color.red);
b26.addActionListener(new Bt());
//实例化四个面板
p0=new Panel();
p1=new Panel();
p2=new Panel();
p3=new Panel();
//创建一个空字符串缓冲区
str=new StringBuffer();
//添加面板p0中的组件和设置其在框架中的位置和大小
p0.add(tf1);
p0.setBounds(10,25,300,40);
//添加面板p1中的组件和设置其在框架中的位置和大小
p1.setLayout(gl1);
p1.add(tf2);
p1.add(b0);
p1.add(b1);
p1.add(b2);
p1.setBounds(10,65,300,25);
//添加面板p2中的组件并设置其的框架中的位置和大小
p2.setLayout(gl2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBounds(10,110,40,150);
//添加面板p3中的组件并设置其在框架中的位置和大小
p3.setLayout(gl3);//设置p3的布局
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b10);
p3.add(b11);
p3.add(b12);
p3.add(b13);
p3.add(b14);
p3.add(b15);
p3.add(b16);
p3.add(b17);
p3.add(b18);
p3.add(b19);
p3.add(b20);
p3.add(b21);
p3.add(b22);
p3.add(b23);
p3.add(b24);
p3.add(b25);
p3.add(b26);
p3.setBounds(60,110,250,150);
//设置框架中的布局为空布局并添加4个面板
setLayout(null);
add(p0);
add(p1);
add(p2);
add(p3);
setResizable(false);//禁止调整框架的大小
//匿名类关闭窗口
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1)
{
System.exit(0);
}
});
setBackground(Color.lightGray);
setBounds(100,100,320,280);
setVisible(true);
}
//构造监听器
class Bt implements ActionListener
{
public void actionPerformed(ActionEvent e2)
{
try{
if(e2.getSource()==b1)//选择"CE"清零
{
tf1.setText("0");//把显示屏清零
str.setLength(0);//清空字符串缓冲区以准备接收新的输入运算数
}
else if(e2.getSource()==b2)//选择"C"清零
{
tf1.setText("0");//把显示屏清零
str.setLength(0);
}
else if(e2.getSource()==b23)//单击"+/-"选择输入的运算数是正数还是负数
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(-x));
}
else if(e2.getSource()==b25)//单击加号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
y=0d;
z=0;
}
else if(e2.getSource()==b20)//单击减号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=1;
}
else if(e2.getSource()==b15)//单击乘号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=2;
}
else if(e2.getSource()==b10)//单击除号按钮获得x的值和z的值并空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=3;
}
else if(e2.getSource()==b26)//单击等号按钮输出计算结果
{
str.setLength(0);
switch(z)
{
case 0 : tf1.setText(""+(x+y));break;
case 1 : tf1.setText(""+(x-y));break;
case 2 : tf1.setText(""+(x*y));break;
case 3 : tf1.setText(""+(x/y));break;
}
}
else if(e2.getSource()==b24)//单击"."按钮输入小数
{
if(tf1.getText().trim().indexOf(′.′)!=-1)//判断字符串中是否已经包含了小数点
{
}
else//如果没数点有小
{
if(tf1.getText().trim().equals("0"))//如果初时显示为0
{
str.setLength(0);
tf1.setText((str.append("0"+e2.getActionCommand())).toString());
}
else if(tf1.getText().trim().equals(""))//如果初时显示为空则不做任何操作
{
}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
}
}
y=0d;
}
else if(e2.getSource()==b11)//求平方根
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText("数字格式异常");
if(x<0)
tf1.setText("负数没有平方根");
else
tf1.setText(""+Math.sqrt(x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b16)//单击了"%"按钮
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(0.01*x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b21)//单击了"1/X"按钮
{
x=Double.parseDouble(tf1.getText().trim());
if(x==0)
{
tf1.setText("除数不能为零");
}
else
{
tf1.setText(""+(1/x));
}
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b3)//MC为清除内存
{
m=0d;
tf2.setText("");
str.setLength(0);
}
else if(e2.getSource()==b4)//MR为重新调用存储的数据
{
if(tf2.getText().trim()!="")//有记忆数字
{
tf1.setText(""+m);
}
}
else if(e2.getSource()==b5)//MS为存储显示的数据
{
m=Double.parseDouble(tf1.getText().trim());
tf2.setText("M");
tf1.setText("0");
str.setLength(0);
}
else if(e2.getSource()==b6)//M+为将显示的数字与已经存储的数据相加要查看新的数字单击MR
{
m=m+Double.parseDouble(tf1.getText().trim());
}
else//选择的是其他的按钮
{
if(e2.getSource()==b22)//如果选择的是"0"这个数字键
{
if(tf1.getText().trim().equals("0"))//如果显示屏显示的为零不做操作
{
}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
else if(e2.getSource()==b0)//选择的是“BackSpace”按钮
{
if(!tf1.getText().trim().equals("0"))//如果显示屏显示的不是零
{
if(str.length()!=1)
{
tf1.setText(str.delete(str.length()-1,str.length()).toString());//可能抛出字符串越界异常
}
else
{
tf1.setText("0");
str.setLength(0);
}
}
y=Double.parseDouble(tf1.getText().trim());
}
else//其他的数字键
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
}
catch(NumberFormatException e){
tf1.setText("数字格式异常");
}
catch( e){
tf1.setText("字符串索引越界");
}
}
}
public static void main(String args[])
{
new Counter();
}
}
㈤ 求JAVA源代码 ~~~~!!!!!!
我给你一个计算器的源代码,这个好讲。自己前几天写的,已经被网络收录了。
import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Calculator extends WindowAdapter implements MouseListener { private String first; private String second; private String operator; private Button zero = new Button("0"); private Button one = new Button("1"); private Button two = new Button("2"); private Button three = new Button("3"); private Button four = new Button("4"谨升); private Button five = new Button("5"); private Button six = new Button("6"); private Button seven = new Button("7"); private Button eight = new Button("8"); private Button nine = new Button("9"); private Button decimal = new Button("."); private Button equal = new Button("="); private Button add = new Button("+"); private Button sub = new Button("-"); private Button mul = new Button("*"); private Button div = new Button("/"); private TextField input = new TextField(); private Button CE = new Button("CE"); private Button DEL = new Button("Del"); 旦晌旦public static void main(String[] args) { new Calculator(); } public Calculator(){ Frame f = new Frame("Calculator"); f.add("North", input); Panel keys = new Panel(); f.add(keys, "Center"); keys.setLayout(new GridLayout(4, 4)); keys.add(seven); keys.add(eight); keys.add(nine); keys.add(add); keys.add(four); keys.add(five); keys.add(six); keys.add(sub); keys.add(one); keys.add(two); keys.add(three); keys.add(mul); keys.add(zero); 模扰keys.add(decimal); keys.add(equal); keys.add(div); zero.addMouseListener(this); one.addMouseListener(this); two.addMouseListener(this); three.addMouseListener(this); four.addMouseListener(this); five.addMouseListener(this); six.addMouseListener(this); seven.addMouseListener(this); eight.addMouseListener(this); nine.addMouseListener(this); add.addMouseListener(this); sub.addMouseListener(this); div.addMouseListener(this); mul.addMouseListener(this); equal.addMouseListener(this); decimal.addMouseListener(this); Panel addtionalPanel = new Panel(); addtionalPanel.setLayout(new GridLayout(1, 2)); addtionalPanel.add(CE); addtionalPanel.add(DEL); CE.setBackground(Color.green); DEL.setBackground(Color.cyan); CE.addMouseListener(this); DEL.addMouseListener(this); f.add("South", addtionalPanel); f.addWindowListener(this); f.setVisible(true); f.setLocation(200, 300); f.setSize(200, 200); } public void windowClosing(WindowEvent e) { System.exit(0); } public void mouseClicked(MouseEvent e) { Button btn = (Button) e.getSource(); String key = btn.getActionCommand().trim(); if(btn == one || btn == two || btn == three || btn == zero || btn == four || btn == five || btn == six || btn == seven || btn == eight || btn == nine){ if(isBlank(operator)){ if(isBlank(first)){ first = key; }else{ first += key; } input.setText(first); }else{ if(isBlank(second)){ second = key; }else{ second += key; } input.setText(second); } }else if(btn == decimal){ if(isBlank(operator)){ if(isBlank(first)){ first = "0."; input.setText(first); }else{ if(first.indexOf(".") == -1){ first += "."; input.setText(first); } } }else{ if(isBlank(second)){ second = "0."; input.setText(second); }else{ if(second.indexOf(".") == -1){ second += "."; input.setText(second); } } } }else if(btn == equal){ if(!isBlank(second) && !isBlank(first) && !isBlank(operator)){ double result = 0.0D; if(operator.equals("+")){ result = Double.parseDouble(first) + Double.parseDouble(second); }else if(operator.equals("-")){ result = Double.parseDouble(first) - Double.parseDouble(second); }else if(operator.equals("*")){ result = Double.parseDouble(first) * Double.parseDouble(second); }else if(operator.equals("/")){ result = Double.parseDouble(first) / Double.parseDouble(second); } int value = (int)Math.round(result); if(value == result){ input.setText(String.valueOf(value)); }else{ input.setText(String.valueOf(result)); } first = String.valueOf(result); second = null; operator = null; } }else if(btn == add || btn == sub || btn == div || btn == mul){ if(!isBlank(first)){ if(!isBlank(operator) && !isBlank(second)){ if(operator.equals("+")){ first = String.valueOf(Double.parseDouble(first) + Double.parseDouble(second)); }else if(operator.equals("-")){ first = String.valueOf(Double.parseDouble(first) - Double.parseDouble(second)); }else if(operator.equals("*")){ first = String.valueOf(Double.parseDouble(first) * Double.parseDouble(second)); }else if(operator.equals("/")){ first = String.valueOf(Double.parseDouble(first) / Double.parseDouble(second)); } second = null; } operator = key; } }else if(btn == CE){ first = null; second = null; operator = null; input.setText(null); }else if(btn == DEL){ if(isBlank(operator)){ if(!isBlank(first)){ first = first.substring(0, first.length() -1); input.setText(first); } }else{ if(!isBlank(second)){ second = second.substring(0, second.length() -1); input.setText(second); } } } } private boolean isBlank(String str){ return str == null || str.trim().equals(""); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }