Java基础实验1-猜数字

程序不写就生疏,思维会固化……又是一个真理。继续折腾。做完实验三用javafx重写一遍。

import javax.swing.JOptionPane;
import java.util.regex.Pattern;


public class GuessNumber{

	//pattern to judge input string is a number or not
	public static boolean isNumeric (String str ){ 

		//patten regulation 0-9,and return a boolean
   		Pattern pattern = Pattern.compile("[0-9]"); 
    	return pattern.matcher(str).matches();    
 	} 

	public static void main (String[] args){
		
		//count how many time have been tried!
		int counter = 0;

		//a random number
		int rNum = (int) (Math.random()*10);
		
		//boolean isCorrect = false;
		int option = JOptionPane.YES_OPTION;
		
		//
		while(option == JOptionPane.YES_OPTION){
			String dataString = JOptionPane.showInputDialog("Enter an int value:");
			
			//judge NAN
			while(!isNumeric(dataString))
			{
				dataString = JOptionPane.showInputDialog("Enter error!Please an int value:");
			}
			

			int data = Integer.parseInt(dataString);
			

			if(data == rNum){
				counter++;
				break;
			}
			else{
				counter++;
				option = JOptionPane.showConfirmDialog(null,"False number! Continue?");
			}
		}
		//output the result
		JOptionPane.showMessageDialog(null,"The correct num is " + rNum + " !You have guessed " + counter + " times with " +
		 (option==JOptionPane.YES_OPTION?--counter:counter) + " false!");
	}
}



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.util.regex.Pattern;

public class GuessNum extends JFrame
	implements ActionListener{
	private JButton jbtOK = new JButton("OK");
	private JButton jbtCancel = new JButton("Quit");
	private JLabel label1 = new JLabel("Please enter the Integer(0-9):");
	private JTextField textfield1 = new JTextField(30);
	public int rNum = (int) (Math.random()*10);
	public int counter = 0;
	public int data = 0;
	public boolean isCorrect = true;
	public boolean isNum = false;


	public static boolean isNumeric (String str ){ 

		//patten regulation 0-9,and return a boolean
   		Pattern pattern = Pattern.compile("[0-9]"); 
    	return pattern.matcher(str).matches();    
 	} 

	public GuessNum()
	{
		JPanel panel1 = new JPanel();
		panel1.add(jbtOK);
		panel1.add(jbtCancel);

		JPanel panel2 = new JPanel();
		panel2.add(label1);
		panel2.add(textfield1);

		add(panel1,BorderLayout.SOUTH);
		add(panel2,BorderLayout.CENTER);

		jbtOK.addActionListener(this);
		jbtCancel.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e){

			if (e.getSource() == jbtCancel) {
				isCorrect = false;
				JOptionPane.showMessageDialog(null,"The correct num is " + rNum + " !You have guessed " + counter + " times with " +
		 			(isCorrect == true?--counter:counter) + " false!");	
				System.exit(0);
			} else if (e.getSource() == jbtOK) {
				isCorrect = true;
				while(isNum == false) {

					String dataString = textfield1.getText();
					if(isNumeric(dataString))
						isNum = true;
					else{
						JOptionPane.showMessageDialog(null,"Please enter a Integer between 0 and 9!");
						textfield1.setText("");
					}
					data = Integer.parseInt(dataString);
				}
				if (data == rNum) {
					counter++;
					JOptionPane.showMessageDialog(null,"The correct num is " + rNum + " !You have guessed " + counter + " times with " +
						(isCorrect == true?--counter:counter) + " false!");
					System.exit(1);
				}
				else {
					counter++;
					JOptionPane.showMessageDialog(null,"False!Go ahead!");
					textfield1.setText("");
				}
			}
	}


	

	
	public static void main (String[] args){
		JFrame frame = new GuessNum();
		frame.setTitle("Guess Number");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值