程序不写就生疏,思维会固化……又是一个真理。继续折腾。做完实验三用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);
}
}