prosta gre za duzo za malo- program losowo generuje liczbe a my zgadujemy wpisaujac swoja liczbe- i program pisze podpowiedzi- za duzo za malo- to jest ta liczba
jak to napisa??
prosta gre za duzo za malo- program losowo generuje liczbe a my zgadujemy wpisaujac swoja liczbe- i program pisze podpowiedzi- za duzo za malo- to jest ta liczba
jak to napisa??
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.text.NumberFormatter;
import java.text.ParseException;
import java.text.NumberFormat;
import java.util.Random;
import java.util.Locale;
public class ZaDuzoZaMalo extends JFrame {
public final static int MAXIMUM = 99;
private JFormattedTextField liczba;
private JLabel liczba_lab;
private JTextArea messages;
private JScrollPane messages_sp;
private JButton restart;
private JButton ok;
private int proby = 1;
private ActionListener aListener;
private int losowa_liczba = 0;
public ZaDuzoZaMalo() throws ParseException {
super("Za Dużo/Za mało");
this.setSize(300,250);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
initComponents();
addNPlace();
this.setVisible(true);
}
private void initComponents() throws ParseException {
Random rnd = new Random();
losowa_liczba = rnd.nextInt(MAXIMUM);
NumberFormat formatter = NumberFormat.getIntegerInstance();
formatter.setGroupingUsed(false);
liczba = new JFormattedTextField(formatter);
liczba_lab = new JLabel("Liczba: ");
messages = new JTextArea("");
messages.setText("Maksymalna liczba to: "+MAXIMUM+"\n");
messages.setEditable(false);
messages.setBackground(Color.WHITE);
messages_sp = new JScrollPane(messages);
aListener = new ZaDuzoZaMaloListener(this);
restart = new JButton("Restart");
restart.setActionCommand("restart");
restart.addActionListener(aListener);
ok = new JButton("ok");
ok.setActionCommand("ok");
ok.addActionListener(aListener);
}
private void addNPlace() {
restart.setBounds(5, 5, restart.getPreferredSize().width, 25);
liczba_lab.setBounds(5, 35, 50, 25);
liczba.setBounds(55, 35, 80, 25);
ok.setBounds(145, 35, ok.getPreferredSize().width, 25);
messages_sp.setBounds(5, 70, 195, 100);
this.add(restart);
this.add(liczba_lab);
this.add(liczba);
this.add(ok);
this.add(messages_sp);
}
private class ZaDuzoZaMaloListener implements ActionListener {
private JFrame frame;
protected ZaDuzoZaMaloListener(JFrame frame) {
this.frame=frame;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase("restart")) {
messages.setText("Maksymalna liczba to: "+MAXIMUM+"\n");
liczba.setText("");
Random rnd = new Random();
losowa_liczba = rnd.nextInt(99);
proby = 1;
}else if(e.getActionCommand().equalsIgnoreCase("ok")) {
if(liczba.getText().equals("")) return;
int wybrana_liczba = Integer.parseInt( liczba.getText() );
if(wybrana_liczba == losowa_liczba) {
JOptionPane.showMessageDialog(frame, "Wygrana!\nIlość prób:"+proby,"Wygrana",JOptionPane.INFORMATION_MESSAGE);
restart.doClick();
} else if(wybrana_liczba<losowa_liczba) {
messages.append("Próba: "+proby+" [nieudana]\n>>ZA MAŁO\n");
proby++;
messages.selectAll();
int h = messages.getSelectionEnd();
messages.select(0,h);
liczba.setText("");
} else if(wybrana_liczba>losowa_liczba) {
messages.append("Próba: "+proby+" [nieudana]\n>>ZA DUŻO\n");
proby++;
messages.selectAll();
int h = messages.getSelectionEnd();
messages.select(0,h);
liczba.setText("");
}
}
}
}
public static void main(String[] args) {
try {
new ZaDuzoZaMalo();
}catch(Exception e) {
System.out.println("Wystąpił wyjątek:\n"+e.getClass().getCanonicalName()+"\nCause: "+e.getCause());
System.out.println("\nStackTrace:\n");
e.printStackTrace();
System.exit(-1);
}
}
}
Lub w konsoli:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Random;
public class ZaDuzoZaMalo {
public static void main(String[] args) throws IOException {
String line;
boolean bool = true;
Random ran = new Random(new Date().getTime());
int number = ran.nextInt() % 100;
number=number<0?-number:number;
while (bool) {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.print("Podaj liczbe: ");
line = br.readLine();
int yourNumber = Integer.parseInt(line);
if (yourNumber == number) {
System.out.println("Wygrałeś!!!");
bool=false;
} else if (yourNumber > number) {
System.out.println("Za dużo!!!");
} else {
System.out.println("Za mało!!!");
}
}
}
}