import javautil;
import javaio;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=anextInt(100);
Systemoutprintln("请输入一个100以内的整数:");
for (int i=0;i<=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(Systemin));
String str=bfreadLine();
int shu=IntegerparseInt(str);
if (shu>num)
Systemoutprintln("输入的数大了,输小点的!");
else if (shu<num)
Systemoutprintln("输入的数小了,输大点的!");
else {
Systemoutprintln("恭喜你,猜对了!");
if (i<=2)
Systemoutprintln("你真是个天才!");
else if (i<=6)
Systemoutprintln("还将就,你过关了!");
else if (i<=8)
Systemoutprintln("但是你还……真笨!");
else
Systemoutprintln("你和猪没有两样了!"); break;}
}
} }
环境变量设置有问题。
JAVA_HOME:
D:\Program Files\Java(不需要分号)
Path:
%JAVA_HOME%\bin;(中间加入bin)
CLASSPATH:
;%JAVA_HOME%\lib;(注意前面的点了,不能省略了)
我的是JDK160_01就这这些了,楼主看看试下。
聊天系统
客户端类的
so=new Socket("服务端机子的IP",9000);
服务端机子的IP 手动加上去
使用方法:
服务器类在A运行
客户端类在B运行 IP填写A的 就可以聊天了
服务器类
import javaawteventActionEvent;
import javaawteventActionListener;
import javaioIOException;
import javaioPrintWriter;
import javanetServerSocket;
import javanetSocket;
import javautilScanner;
import javaxswing;
public class fuwuqi extends JFrame implements ActionListener {
JTextArea ja;
JScrollPane jp;
static JTextField jt;
JButton jb;
ServerSocket ss;
Socket so;
PrintWriter pw;
Scanner sc=new Scanner(Systemin);
fuwuqi()
{
ja=new JTextArea();
jp=new JScrollPane(ja);
jt=new JTextField(10);
jb=new JButton("发送");
thisadd(jp);
thisadd(jt);
thisadd(jb);
thissetLayout(null);
jpsetBounds(10, 10, 300, 180);
jtsetBounds(10, 200, 200, 20);
jbsetBounds(240, 200, 70, 20);
jbaddActionListener(this);
jtaddActionListener(this);
thissetResizable(false);
thissetLocation(300, 210);
thissetTitle("服务端 - 聊天");
thissetSize(325,270);
thissetVisible(true);
thissetDefaultCloseOperation(thisEXIT_ON_CLOSE);
try {
ss=new ServerSocket(9000);
so=ssaccept();
pw=new PrintWriter(sogetOutputStream(),true);
Scanner sc=new Scanner(sogetInputStream());
while(true)
{
jaappend("客户端说:"+scnextLine()+"\r\n");
}
} catch (IOException e) {
Systemoutprintln("端口被占用");
}
}
public static void main(String[] args) {
new fuwuqi();
}
public void actionPerformed(ActionEvent e) {
String a=jtgetText();
pwprintln(a);
jaappend("我说:"+a+"\r\n");
jtsetText("");
}
}
客户端类
import javaawteventActionEvent;
import javaawteventActionListener;
import javaioIOException;
import javaioPrintWriter;
import javanetServerSocket;
import javanetSocket;
import javautilScanner;
import javaxswing;
public class kehu extends JFrame implements ActionListener {
JTextArea ja;
JScrollPane jp;
static JTextField jt;
JButton jb;
ServerSocket ss;
Socket so;
PrintWriter pw;
Scanner sc=new Scanner(Systemin);
kehu()
{
ja=new JTextArea();
jp=new JScrollPane(ja);
jt=new JTextField(10);
jb=new JButton("发送");
thisadd(jp);
thisadd(jt);
thisadd(jb);
thissetLayout(null);
jpsetBounds(10, 10, 300, 180);
jtsetBounds(10, 200, 200, 20);
jbsetBounds(240, 200, 70, 20);
jbaddActionListener(this);
jtaddActionListener(this);
thissetResizable(false);
thissetLocation(300, 210);
thissetTitle("客户端 - 聊天");
thissetSize(325,270);
thissetVisible(true);
thissetDefaultCloseOperation(thisEXIT_ON_CLOSE);
try {
so=new Socket("服务端机子的IP",9000);
//so=new Socket("127001",9000);
//so=new Socket("服务器IP",9000);
pw=new PrintWriter(sogetOutputStream(),true);
Scanner sc1=new Scanner(sogetInputStream());
while(true)
{
jaappend("服务器说:"+sc1nextLine()+"\r\n");
}
} catch (IOException e) {
Systemoutprintln("端口被占用");
}
}
public static void main(String[] args) {
new kehu();
}
public void actionPerformed(ActionEvent e) {
String a=jtgetText();
pwprintln(a);
jaappend("我说:"+a+"\r\n");
jtsetText("");
}
}
我有个300多行的单机版五子棋。。不知道你说的小程序是指在网页上运行的,还是代码量少的程序。
------------------------------
import javaawt;
import javaawtevent;
import javaxswing;
class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(ColorBLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = egetX();
int y = egetY();
if(x < 25 || x > 330 + 25 ||y < 25 || y > 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i <= 330;i += 30)
{
for(int j = 30;j <= 330; j+= 30)
{
gsetColor(ColorWHITE);
gdrawLine(i, j, i, 330);
}
}
for(int j = 30;j <= 330;j += 30)
{
gsetColor(ColorWHITE);
gdrawLine(30, j, 330, j);
}
}
void Drawchess(Graphics g)
{
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
gsetColor(ColorBLACK);
gfillOval((i + 1) 30 - 8, (j + 1) 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
gsetColor(ColorWHITE);
gfillOval((i + 1) 30 - 8, (j + 1) 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;
for(i = 0;i < 11;i++)//横向判断
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPaneshowMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPaneshowMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i < 11;i++)//竖向判断
{
for(int j = 0;j < 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPaneshowMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPaneshowMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i < 7;i++)//左向右斜判断
{
for(int j = 0;j < 7;j++)
{
for(int k = 0;k < 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPaneshowMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPaneshowMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
for(i = 4;i < 11;i++)//右向左斜判断
{
for(int j = 6;j >= 0;j--)
{
for(int k = 0;k < 5;k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPaneshowMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPaneshowMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
}
void Clear_Chess()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panelsetBounds(0,23, 360, 360);
setTitle("单人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
Systemexit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}
gdrawString()需要指定字符串的起始位置x,y改正后的代码如下:
import javaapplet;
import javaawt;
public class 习题2 extends Applet {
public void paint(Graphics g)
{
int i, j, b;
int x_step=10;
gsetColor(Colorblack);
gdrawString("1到100中的素数为:", 5, 10);
for (i = 2; i <= 100; i++) {
for (j = 2; j <= i / 2; j++) {
if (i % j == 0)
break;
}
b = i;
{
gsetColor(Colorred);
gdrawString(" " + i, 120+x_step, 10);
x_step+=20;
}
}
}
}
先回答:4:
public class Plus {
/
@param args
/
public static void main(String[] args) {
for(int i=1;i<=9;i++){
//
for(int j=1;j<=i;j++){
Systemoutprint(j+""+j+"="+ij);
Systemoutprint(" ");
if(j==i){
Systemoutprint("\n");
}
}
}
}
}
欢迎分享,转载请注明来源:表白网
评论列表(0条)