Java源程序附后。
目前成都創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、覃塘網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
本程序的特點是:
(1) 文本框只能輸入純數(shù)字;
(2) 界面較美觀;
(3) 代碼可讀性較好,有適當(dāng)?shù)淖⑨專?/p>
(4) 窗體一出現(xiàn)就在桌面居中。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class GuessNumber {
private static final long serialVersionUID = 1L;
JFrame frame;
JTextField txtNum; //文本框
JButton btnStart; //按鈕
JLabel lblPrompt;
JLabel lblMessage;
static int source = 0;
static Random rand = new Random();
public GuessNumber(){
frame = new JFrame("Guess Number");
JPanel pnl1, pnl2, pnl3, pnl4;
pnl1 = new JPanel();
pnl1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl2 = new JPanel();
pnl2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl3 = new JPanel();
pnl3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl4 = new JPanel();
pnl4.setLayout(new FlowLayout(FlowLayout.LEFT));
txtNum = new JTextField(10);
btnStart = new JButton("開始");
lblPrompt = new JLabel("htmlbodyI have a number between 1 and 1000 can you guess my number?br/Please enter your first guess./body/html");
lblMessage = new JLabel();
pnl1.add(lblPrompt);
pnl2.add(txtNum);
pnl3.add(lblMessage);
pnl4.add(btnStart);
frame.setLayout(new GridLayout(4, 1));
frame.add(pnl1);
frame.add(pnl2);
frame.add(pnl3);
frame.add(pnl4);
txtNum.addActionListener(this.new TextAction());
txtNum.addKeyListener(this.new KeyAction());
btnStart.addActionListener(this.new ButtonAction());
frame.setSize(400, 200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
public static void main(String[] args) {
new GuessNumber();
while((source=rand.nextInt(1000))==0);
}
//按鈕單擊后的事件處理
class ButtonAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn == btnStart){
while((source=rand.nextInt(1000))==0);
txtNum.setEditable(true);
}
}
}
//文本框按回車后的事件處理
class TextAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JTextField txt = (JTextField)e.getSource();
if(txt != txtNum){
return;
}
int num = Integer.parseInt(txtNum.getText());
if(num == source){
lblMessage.setText("Correct!");
txtNum.setEditable(false);
txtNum.setBackground(frame.getBackground());
}
else if(num source){
lblMessage.setText("Too High");
txtNum.setBackground(Color.red);
}
else{
lblMessage.setText("Too Low");
txtNum.setBackground(Color.blue);
}
}
}
//限制文本框只能輸入數(shù)字
class KeyAction implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
int k = e.getKeyChar();
String text = ((JTextField)e.getSource()).getText();
if(!((k47 k 58) || (k==8 || k==KeyEvent.VK_PERIOD))){ //限制只能輸入數(shù)字
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
if(text.length() 4){ //限制數(shù)值的長度
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
}
}
}
import?java.util.Random;
import?java.util.Scanner;
public?class?caishu?{
static?int?i?=?0;
static?int?p;
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
p?=?(int)?(Math.random()?*?100);
//System.out.println(p);
while?(i??5)?{
try?{
System.out.println("請輸入你要猜的數(shù):");
Scanner?scanner?=?new?Scanner(System.in);
int?s?=?scanner.nextInt();
if?(p?!=?s)?{
if?(p??s)?{
System.out.println("你猜得數(shù)小了");
}?else?{
System.out.println("你猜得數(shù)大了");
}
}?else?{
System.out.println("恭喜你猜對了!");
return;
}
i++;
}?catch?(Exception?ex)?{
System.out.println("輸入的數(shù)據(jù)有誤!");
}
}
System.out.println("你的猜數(shù)次數(shù)已經(jīng)用完了!");
}
}
運行結(jié)果
public class GuessNumber {
public static void main(String[] args) {
System.out.println("給你一個0至100之間的整數(shù),請猜測這個數(shù)");
//隨機生成一個0至100之間的隨機整數(shù)
int realNumber=(int)Math.round(Math.random()*100);
//從鍵盤上讀入你的猜測;
System.out.print("請輸入你的猜測:");
int yourGuess=System.in.read();
//是否猜對的標(biāo)識
boolean isRight=false;
//循環(huán)校驗,直到猜對了為止
while(!isRight){
if(yourGuess realNumber){
System.out.print("猜大了,再輸入你的猜測:");
yourGuess= System.in.read();
}
else if(yourGuess realNumber){
System.out.print("猜小了,再輸入你的猜測:");
yourGuess=System.in.read();
}
else{
isRight=true;
System.out.print("恭喜你,猜對了");
}
}
}
}
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
//隨機生成一個1-100的數(shù)
int randomNumber = (int) (Math.random() * 100 + 1);
System.out.println("輸入一個number");
//鍵盤輸入一個數(shù)
Scanner sc = new Scanner(System.in);
int guessNumber = sc.nextInt();
//
while(guessNumber != randomNumber)
{
if(guessNumber randomNumber)
{
System.out.println("猜大了,請繼續(xù)……");
}
else
{
System.out.println("猜小了,請繼續(xù)……");
}
guessNumber = sc.nextInt();
}
System.out.println("恭喜你,猜測正確!是否繼續(xù)猜數(shù)");
}
}
格式有點丑陋。