![]() Int spacing = longest + spacingSeparation // total spacingįor (int i = 0 i < cards.This chapter introduces the java.awt classes that are used to work with different fonts and colors. Int longest = "Queen of Diamonds".length() // length of the widest column Int spacingSeparation = 3 // number of spaces between columns Here is a sample program: static String cards = Then you add whatever separation you want, for example a tab ( "\t"). ![]() The way you would do this is, for each current string, add spaces to it until its length matches the length of "Queen of Diamonds". Since you know the longest String you will have is "Queen of Diamonds", you can base your separation on its length. Easy way to solve this is, if your output is a String, use s, if it's and int (I don't think you will need it, I just had to for my example cuz mine were ints), use d. In this case, one says s, the other says d. Next is the letter, that letter, as you can see, changes between the 2 output statements. I see the longest one possible would be Queen of Diamonds so anything bigger than 19 should do it, I chose 22 cuz it looked better IMO. The only catch is, make sure that the number, 22, is longer than the maximum possible String. Long story short, it will plainly and simply make sure each output starts and ends at the same place. The 17's you see are how many spaces there will be subtracted from the length of the output. Since it is negative, it will be on the right side, (FYI, if there was no sign there, padding would be on the left) so it will print out your output, then add spaces to the right. HERE- is a good place to learn more but I'll give you the basic rundown.įirst, the negative sign specifies which side will receive padding, padding is the space between the columns that makes them look pretty. You see how there are letters and numbers and - after the %? Those all serve purposes too. Lastly, let's look some more at that special action thing I mentioned earlier. Do you see how it starts by specifying the format with "%-22s%-22s%-22s\n"? Then, it actually outputs them, separated by commas? That's IMO the easiest way to format. Then, the actual outputs are the following parameters. The way () works is by specifying the format of the output in the first parameter. Now, let's look at the parameters of the method. First, whenever you see a % sign in (), it means a special (formatting in this case) action is about to follow. Let's explain what is going on here, there's a lot of stuff happening. Well, the easy way to solve for that would be like this. ![]() It looks like you want to print out something that looks like this. It looks like you are printing out using () right? If so, you can use () along with %#. You use something similar to what you are probably using right now. I greatly appreciate any help, being I am new to Java. If you want I can post the entire code, but I was trying not to post a lot. * Declare a 7 by 3 array to receive the cards dealt to play the trick */ * Declare a 52 element array of cards */ The output looks like this: Column 0 Column 1 Column 2ģ of Spades 3 of Diamonds Ace of Diamondsĩ of Diamonds 2 of Diamonds 10 of Diamonds I know there is a way to format it but am unsure. My teacher said to use spaces, but I have tried this and does not work. As of right now, when it prints the fist column is even but the 2nd and 3rd are not. I am only having one issue, and that is formatting the output. In my java class we wrote a card program in which you choose a "secret card", and at the end it tells you what your secret card was.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |