| | |
a for loop problem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
What you've got is a good start. Here's how I would do it:
I know you're not dumb, so I'm not going to give you the "don't copy it verbatim" spiel. I haven't written in Java for a while, and I was looking for something to hack out; just scratching an algorithmic itch. Use this as an example. What you had was good, it just wasn't very readable (at least to me). I like to stay away from
"Makes things as simple as possible, but no simpler."
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GuessGame extends JFrame { private static JButton button; private static JLabel label1, label2; private static JTextField textfield; private static Color backgroundColor; private static Container content; private int valueToGuess; // Equivalent to "x" in your code. private int numberOfTrials = 0; public static void main(String[] args) { initializeComponents(); valueToGuess = randomNumber(); // Initialize valueToGuess. finalizeComponents(); } private static void resetTrialCounter() { numberOfTrials = 0; } private static int randomNumber(int min, int max) { return (min + (int)(Math.random() * b)); } private static void compare(int a, int b) { if (numberOfTrials >= 5) return; if (a == b) { label2.setText("Astonishing! The number is actually " + a + "!"); textfield.setEditable(false); resetTrialCounter(); // Set the numberOfTrials back to 0 (zero). } else if (a > b) { label2.setText("Move towards lesser values."); textfield.setText(null); numberOfTrials++; } else { label2.setText("Move towards larger values."); textfield.setText(null); numberOfTrials++; } } private static void initializeComponents() { content = this.getContentPane(); content.setLayout(new BorderLayout()); content.setBackground(backgrounColor); this.setSize(511, 134); label1 = new JLabel("I have a number between 1 and 1000. Can you guess my number? Enter your guess:"); label2 = new JLabel("--------------------------------------------------"); backgroundColor = new Color(255, 250, 240); button = new JButton("New Game"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { label2.setText("--------------------------------------------------"); textfield.setText(null); x = randomNumber(); // Assign x a new "random" value. } }); textfield = new JTextField(20); textfield.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { compare(valueToGuess, Integer.parseInt(textfield.getText())); } }); } private static void finalizeComponents() { content.add(label1); content.add(textfield); content.add(label2); content.add(button); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } }
I know you're not dumb, so I'm not going to give you the "don't copy it verbatim" spiel. I haven't written in Java for a while, and I was looking for something to hack out; just scratching an algorithmic itch. Use this as an example. What you had was good, it just wasn't very readable (at least to me). I like to stay away from
extends and implements hooks when defining a class (there is a time and a place for them, and this is - sort of - one of them). I find it tends to make things a little harder to figure out."Makes things as simple as possible, but no simpler."
Last edited by indienick; Aug 5th, 2007 at 2:16 pm.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg
Don't tell me to "google it" - I already have.
-Ginsburg
Don't tell me to "google it" - I already have.
So?
I learned Java just so I knew at least one, non-Smalltalk-based, OOP language.
I'm still making use of other classes within the program (javax.swing.JFrame, javax.swing.JButton, etc.). I don't see what the big deal is. And my take on that algorithm is *not* avoiding the "point" of OOP. I'm providing a clean, readable program that works.
And thank you for clarifying what main()'s purpose is in this world - here I am thinking it's just some useless, needed function. *rolls eyes*
I think it's stupid to instantiate a class from within itself.
I learned Java just so I knew at least one, non-Smalltalk-based, OOP language.
I'm still making use of other classes within the program (javax.swing.JFrame, javax.swing.JButton, etc.). I don't see what the big deal is. And my take on that algorithm is *not* avoiding the "point" of OOP. I'm providing a clean, readable program that works.
And thank you for clarifying what main()'s purpose is in this world - here I am thinking it's just some useless, needed function. *rolls eyes*
I think it's stupid to instantiate a class from within itself.
Last edited by indienick; Aug 5th, 2007 at 2:39 pm.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg
Don't tell me to "google it" - I already have.
-Ginsburg
Don't tell me to "google it" - I already have.
•
•
•
•
So?
I learned Java just so I knew at least one, non-Smalltalk-based, OOP language.
I'm still making use of other classes within the program (javax.swing.JFrame, javax.swing.JButton, etc.). I don't see what the big deal is. And my take on that algorithm is *not* avoiding the "point" of OOP. I'm providing a clean, readable program that works.

Your program certainly does work, it just demonstrates a methodology that a beginner may learn some bad habits from. Making everything static and calling it directly from main() is not a good standard practice.
•
•
•
•
And thank you for clarifying what main()'s purpose is in this world - here I am thinking it's just some useless, needed function. *rolls eyes*
•
•
•
•
I think it's stupid to instantiate a class from within itself.
I'm sorry if you took my comment as a personal affront, as it wasn't the intention and the code was probably very helpful to the poster. However, those new to Java often aren't yet aware of why things are written the way they are and bad habits can be learned early from sample code they don't fully understand. That was the only reason I felt it necessary to mention the usage of main().
Last edited by Ezzaral; Aug 6th, 2007 at 1:04 pm. Reason: spelling
![]() |
Similar Threads
- nested for loop problem (Java)
- Alright this loop problem is bugging me (Java)
- I think Loop problem (Java)
- Cold Fusion Loop problem (ColdFusion)
- Help w/ for loop. (C++)
- loop problem (C)
- Random Different Number in each loop ?? HLP Me PLZ (c or c++) (C++)
- loop problem (C++)
Other Threads in the Java Forum
- Previous Thread: engg. java project
- Next Thread: From Java beginner:Am I right with answers?:)
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component coordinates database defaultmethod development dice dragging ebook eclipse educational error exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite ingres input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree websites windows



Thanks for your advice and interest. 


