Question

My best friend's girlfriend texted me and thought it would be a good idea to put some code on his cake saying happy birthday. I figured I would make an array for the alphabet, and use some serious math equations that he has to type in for it to say happy birthday and make some shapes for a simple version of his face.

Currently I have

class josh
{
public static void main (String[] args)
{

        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'};
        char c;
       for (c = 0 ; c < 26; c++)//for statement was just a text to make sure it worked
        {
            System.out.println (alphabet[c]);
        }       
}

}

All i want to do is for the birthday cake's sake is to compress the a-z in the alphabet array initializer, just use something instead of typing them all out that keeps it simple. I thought of a for statement but wasn't sure how to use it like that. Any suggestions?

EDIT: she can print off a picture so I can fit more code in. if there's too much for the cake i can cut it down to something simpler.

Was it helpful?

Solution

you can use ascii numbers and loop it. for example declare a char chAlpha then initialize that with these codes:

char[] chAlpha = new char[26];
for (int i = 0; i < 26; ++i) {
   char[i] = (char) (i + 65);
}//note that 65-131 is decimal value of the alphabet

and for the small/lowercase letters:

char[] chAlpha = new char[26];
for (int i = 0; i < 26; ++i) {
       char[i] = (char) (i + 97);
}

OTHER TIPS

You can initialize the alphabet like so:

char[] alphabet = new char[26];
for (int i = 0; i < 26; ++i) {
   char[i] = (char) (i + 65);
}

What about:

String s1= "day!"; 
String s2= "Hap";
String s3= "py ";
String s4= "Birth";
System.out.println(s2+s3+s4+s1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top