谁可以给我提供一份Java小程序 例如 用Java编写的日历 留言薄 聊天小程序 等 一定要有源代码哦 15
2个回答
展开全部
猜数字行不?代码如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GuessNumber {
static int trys, A, B;
static String r, t;
public static int[] MakeGuessNumber(){
Random r = new Random();
int guess[] = new int[4];
for(int i=0; i<4; i++){
guess[i] = r.nextInt(10);
for(int j=i-1; j>=0; j--){
if(guess[i]==guess[j]){
i--;
break;}
}
}
return guess;
}
public static String getRundom(){
int guess[]=MakeGuessNumber();
return ""+guess[0]+guess[1]+guess[2]+guess[3];
}
public static void messageDialog(Object o){
JOptionPane.showMessageDialog(null, o);
}
public static void guessNumber(){
r=getRundom();
//System.out.println(r);
JFrame jf=new JFrame();
JButton b1=new JButton("新游戏");
JLabel l1=new JLabel("输入:");
final JTextField jtf=new JTextField(10);
JButton b2=new JButton("提交");
final JTextArea jta=new JTextArea(10,10);
jta.append(" "+"Guess"+" "+"Result"+"\n");
JScrollPane scrollPane=new JScrollPane(jta);
JPanel jp1=new JPanel();
jp1.add(l1);
jp1.add(jtf);
jp1.add(b2);
jf.add(b1,BorderLayout.NORTH);
jf.add(jp1,BorderLayout.CENTER);
jf.add(scrollPane,BorderLayout.SOUTH);
b1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
trys=0;
A=0;
B=0;
jta.setText(" "+"Guess"+" "+"Result"+"\n");
jtf.setText("");
r=getRundom();
//System.out.println(r);
}
});
b2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
t=jtf.getText();
A=0;
B=0;
if(t.length()!=4||t.substring(0, 1).equals(t.substring(1, 2))
||t.substring(0, 1).equals(t.substring(2, 3))
||t.substring(0, 1).equals(t.substring(3, 4))
||t.substring(1, 2).equals(t.substring(2, 3))
||t.substring(1, 2).equals(t.substring(3, 4))
||t.substring(2, 3).equals(t.substring(3, 4))
||!t.matches("[0-9]*"))
messageDialog("Wrong Input!");
else{
jtf.setText("");
trys++;
if(t.substring(0, 1).equals(r.substring(0, 1)))
A++;
if(t.substring(0, 1).equals(r.substring(1, 2)))
B++;
if(t.substring(0, 1).equals(r.substring(2, 3)))
B++;
if(t.substring(0, 1).equals(r.substring(3, 4)))
B++;
if(t.substring(1, 2).equals(r.substring(1, 2)))
A++;
if(t.substring(1, 2).equals(r.substring(0, 1)))
B++;
if(t.substring(1, 2).equals(r.substring(2, 3)))
B++;
if(t.substring(1, 2).equals(r.substring(3, 4)))
B++;
if(t.substring(2, 3).equals(r.substring(2, 3)))
A++;
if(t.substring(2, 3).equals(r.substring(0, 1)))
B++;
if(t.substring(2, 3).equals(r.substring(1, 2)))
B++;
if(t.substring(2, 3).equals(r.substring(3, 4)))
B++;
if(t.substring(3, 4).equals(r.substring(3, 4)))
A++;
if(t.substring(3, 4).equals(r.substring(0, 1)))
B++;
if(t.substring(3, 4).equals(r.substring(1, 2)))
B++;
if(t.substring(3, 4).equals(r.substring(2, 3)))
B++;
jta.append(trys+" "+t+" "+A+"A"+B+"B"+"\n");
if(A==4){
if(trys>=4)
messageDialog("You win after "+trys+" trys!");
else if(trys<=3)
messageDialog("You win after only "+trys+" trys!");
}
}
}
});
jf.setSize(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
guessNumber();
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GuessNumber {
static int trys, A, B;
static String r, t;
public static int[] MakeGuessNumber(){
Random r = new Random();
int guess[] = new int[4];
for(int i=0; i<4; i++){
guess[i] = r.nextInt(10);
for(int j=i-1; j>=0; j--){
if(guess[i]==guess[j]){
i--;
break;}
}
}
return guess;
}
public static String getRundom(){
int guess[]=MakeGuessNumber();
return ""+guess[0]+guess[1]+guess[2]+guess[3];
}
public static void messageDialog(Object o){
JOptionPane.showMessageDialog(null, o);
}
public static void guessNumber(){
r=getRundom();
//System.out.println(r);
JFrame jf=new JFrame();
JButton b1=new JButton("新游戏");
JLabel l1=new JLabel("输入:");
final JTextField jtf=new JTextField(10);
JButton b2=new JButton("提交");
final JTextArea jta=new JTextArea(10,10);
jta.append(" "+"Guess"+" "+"Result"+"\n");
JScrollPane scrollPane=new JScrollPane(jta);
JPanel jp1=new JPanel();
jp1.add(l1);
jp1.add(jtf);
jp1.add(b2);
jf.add(b1,BorderLayout.NORTH);
jf.add(jp1,BorderLayout.CENTER);
jf.add(scrollPane,BorderLayout.SOUTH);
b1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
trys=0;
A=0;
B=0;
jta.setText(" "+"Guess"+" "+"Result"+"\n");
jtf.setText("");
r=getRundom();
//System.out.println(r);
}
});
b2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
t=jtf.getText();
A=0;
B=0;
if(t.length()!=4||t.substring(0, 1).equals(t.substring(1, 2))
||t.substring(0, 1).equals(t.substring(2, 3))
||t.substring(0, 1).equals(t.substring(3, 4))
||t.substring(1, 2).equals(t.substring(2, 3))
||t.substring(1, 2).equals(t.substring(3, 4))
||t.substring(2, 3).equals(t.substring(3, 4))
||!t.matches("[0-9]*"))
messageDialog("Wrong Input!");
else{
jtf.setText("");
trys++;
if(t.substring(0, 1).equals(r.substring(0, 1)))
A++;
if(t.substring(0, 1).equals(r.substring(1, 2)))
B++;
if(t.substring(0, 1).equals(r.substring(2, 3)))
B++;
if(t.substring(0, 1).equals(r.substring(3, 4)))
B++;
if(t.substring(1, 2).equals(r.substring(1, 2)))
A++;
if(t.substring(1, 2).equals(r.substring(0, 1)))
B++;
if(t.substring(1, 2).equals(r.substring(2, 3)))
B++;
if(t.substring(1, 2).equals(r.substring(3, 4)))
B++;
if(t.substring(2, 3).equals(r.substring(2, 3)))
A++;
if(t.substring(2, 3).equals(r.substring(0, 1)))
B++;
if(t.substring(2, 3).equals(r.substring(1, 2)))
B++;
if(t.substring(2, 3).equals(r.substring(3, 4)))
B++;
if(t.substring(3, 4).equals(r.substring(3, 4)))
A++;
if(t.substring(3, 4).equals(r.substring(0, 1)))
B++;
if(t.substring(3, 4).equals(r.substring(1, 2)))
B++;
if(t.substring(3, 4).equals(r.substring(2, 3)))
B++;
jta.append(trys+" "+t+" "+A+"A"+B+"B"+"\n");
if(A==4){
if(trys>=4)
messageDialog("You win after "+trys+" trys!");
else if(trys<=3)
messageDialog("You win after only "+trys+" trys!");
}
}
}
});
jf.setSize(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
guessNumber();
}
}
追问
谢谢你的帮助 我需要问我的老师是否可以用这个做课程设计 衷心感谢你的帮助
参考资料: 绝对原创,谢谢。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询