This site requires JavaScript, please enable it in your browser!
Greenfoot back
JarJarBanks7
JarJarBanks7 wrote ...

2016/10/14

Random letter generator

JarJarBanks7 JarJarBanks7

2016/10/14

#
Im trying to get a random letter and assign that letter to a variable then display that variables value in the middle of the screen. Whats wrong with my code. If you really want to be helpful provide some counter-code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Letters here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Letters extends Actor
{
    private String Letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private int Char;
    private String Letter;
    /**
     * Act - do whatever the Letters wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Char=Greenfoot.getRandomNumber(26) ;
        Letter=Letters.substring(Char);
        getWorld().showText(Letter ,400, 300);
         
         
    }   
}
danpost danpost

2016/10/14

#
The 'substring' method you are using uses not only the character you point to, but all the remaining characters within the string. In other words, the parameter is the starting point and the ending point is the length of the string. You could try this:
1
Letter = ""+Letters.charAt(Char); // getting the one character
or
1
Letter = Letters.substring(Char, Char+1); // getting a substring with the length of one
JarJarBanks7 JarJarBanks7

2016/10/17

#
This should work right? because it isn'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
/**
 * Write a description of class Letters here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Letters extends Actor
{
    private String letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private int Char;
    private String letter;
    /**
     * Act - do whatever the Letters wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        getLetter();
          
          
    }
    public void getLetter(){
        Char=Greenfoot.getRandomNumber(26) ;
        letter = ""+ letters.charAt(Char);
        getWorld().showText(letter ,400, 300);
         
    }
         
}
t.
danpost danpost

2016/10/17

#
JarJarBanks7 wrote...
This should work right? because it isn't. < Code Omitted >
How is it not working, other than it changing the character displayed at a hyper rate? Explain what needs changed as far as what it currently does and what it should be changed to do.
JarJarBanks7 JarJarBanks7

2016/10/17

#
I need it to display a letter at random but it shows nothing when i run it. Its Completely blank
danpost danpost

2016/10/17

#
JarJarBanks7 wrote...
I need it to display a letter at random but it shows nothing when i run it. Its Completely blank
Well, I tested the class and found that it works like I stated above (I slowed down the scenario speed so that the letter displayed changes will some delay between them). How long should one random letter stay displayed for? or, how much time between changes would you like to allow? or, what condition should trigger a change?
JarJarBanks7 JarJarBanks7

2016/10/18

#
Id like it to switch letters upon hitting the key it displays. For isntance if it displayed,"k", pressing k would switch it to the next letter
danpost danpost

2016/10/18

#
JarJarBanks7 wrote...
Id like it to switch letters upon hitting the key it displays. For isntance if it displayed,"k", pressing k would switch it to the next letter
Then you need to put a condition on calling 'getLetter'. You could use a little trick, here. Only get the letter when the 'letter' field is 'null' and when a letter is matched with keyboard input, set the field back to 'null' so the next act cycle will get a new letter:
1
2
3
4
5
public void act()
{
    if (letter == null) getLetter();
    if (letter.equals(Greenfoot.getKey())) letter = null;
}
Because of the 'letters' string being all uppercase, the user will have to shift or have caps lock on to produce a match.
JarJarBanks7 JarJarBanks7

2016/10/18

#
how would i put this in my code?
danpost danpost

2016/10/19

#
JarJarBanks7 wrote...
how would i put this in my code?
Replace line 18 through 23 of your Letters class, as given above, with it.
You need to login to post a reply.