normally we don't give you the code here, but as it's a simple program it might be easier to explain everything by code
This is a console application, but it shows you how to do this kind of programsCode:import java.util.Random; import java.util.Scanner; public class Alphabet { //We declare the random generator globally private Random generator = new Random(); //Here we will remember what the index was of the letter we asked private int askedLetterIndex; //Here we declare the alphabet private char[] alphabet = {'a','b','c','d','e','f','g','h' ,'i','j','k','l','m','n','o','p','q' ,'r','s','t','u','v','w','x','y','z'}; //This function will get you a random number from our alphabet private char getRandomLetter(){ askedLetterIndex = generator.nextInt(24); return alphabet[askedLetterIndex]; } //This will check the answer of the user, it returns True if the answer was correct and False if it was incorrect private Boolean checkAnswer(char answer){ if (alphabet[askedLetterIndex + 1] == answer){ return true; }else{ return false; } } //This method will do the test //This is a console application, so for a GUI application you'll have to rewrite this method public void doTest(){ //The scanner will be used to read the answer of the user Scanner in = new Scanner(System.in); //Here we will stock the answer of the user char answer; //This will ask the user a letter for 10 times for(int i = 0; i < 10; i++){ System.out.println("What letter comes afther '" + getRandomLetter() + "' ?"); answer = in.next().charAt(0); if(checkAnswer(answer)){ System.out.println("Correct!"); }else System.out.println("Wrong!"); } } //Run the program public static void main(String[] args) { Alphabet a = new Alphabet(); a.doTest(); } }
this is the part of code only right ?but i not sure how to use gui application for this question ,can you help me pls
hello can you use GUI application to write out the code for this question ?pls help me !
Common, show some effort.
Give us the code you would use to create a GUI class. it will basicly need 2 components, a JLabel and a JTextfield. Every System.out.println() should be turned into a method like FrameClass.setLabelText(text)
And the "in.next()" is replaced by a JTextfield.
To let the textfield react on the user's 'ENTER' use:
Or you could use a button insteadCode:textfield.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){/*Empty*/ } public void keyReleased(KeyEvent e){/*Empty*/} public void keyTyped(KeyEvent e){ if(e.getKeyChar() == KeyEvent.VK_ENTER){ String text = textfield.getText(); alphabet.checkAnswer(text) } } });
hello may i know is this a full source code for gui application in this question ?thanks for helping me ...
Code:textfield.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){/*Empty*/ } public void keyReleased(KeyEvent e){/*Empty*/} public void keyTyped(KeyEvent e){ if(e.getKeyChar() == KeyEvent.VK_ENTER){ String text = textfield.getText(); alphabet.checkAnswer(text) } } });
Last edited by ZekeDragon; 03-22-2010 at 06:30 PM. Reason: Please use [code] tags (the # button) when posting code.
No, it only adds a listener to a textfield so when you are typing in the textfield and push 'Enter' something happens.
can i have the full code of it ?because i really dunoo how to do this question ,shaddix
hello may i know the solution of gui application ?i really need the source code of this question .pls help me shaddix
hello ,the code u given are tested .but the alphabet is repeated for some .and the question request that no alphabet is to be repeated .so how 2 solve this problem ?shaddix thanks
Got this from another post on the forums. Fits so perfectly:
Code:While( !User.hasAnswer ) { User.askQuestion(); if( User.postCode && User.putForthEffort ) { CodeCallMembers.Help(); User.hasAnswer = true; } else { CodeCallMembers.Laugh(); } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks