import java.util.Random;

public class Card
{

public String cardGeneration()
{

Random generator = new Random();

int x; // random integer representing suit
int y; // random integer for card value
String mysuit, mynumber; //assigning names, these will be changed 
int suits = 4, numbers = 13; //depending what card is picked


x = generator.nextInt(suits); // random integer between 0 & 3 inclusive

if (x == 0)
mysuit = "Clubs";
else if (x == 1)
mysuit = "Diamonds";
else if (x == 2)
mysuit = "Hearts";
else if (x == 3)
mysuit = "Spades";
else
mysuit = "NA";

y = generator.nextInt(numbers) + 1; // add 1 so that y is between 1 & 13 inclusive

if (y == 1)
mynumber = "Ace";
else if (y == 2)
mynumber = "Two";
else if (y == 3)
mynumber = "Three";
else if (y == 4)
mynumber = "Four";
else if (y == 5)
mynumber = "Five";
else if (y == 6)
mynumber = "Six";
else if (y == 7)
mynumber = "Seven";
else if (y == 8)
mynumber = "Eight";
else if (y == 9)
mynumber = "Nine";
else if (y == 10)
mynumber = "Ten";
else if (y == 11)
mynumber = "Jack";
else if (y == 12)
mynumber = "Queen";
else if (y == 13)
mynumber = "King";
else
mynumber = "NA";

String result = mynumber + " of " + mysuit; 
return result;


} 


}

All this time I haven't been indenting, I've seen examples but I don't know how to do it for this one...

Recommended Answers

All 8 Replies

http://en.wikipedia.org/wiki/Indent_style

Indenting (in Java, C, C++, C#) is pretty much making your code more readable.
It implies levels of execution and logic, but is purely a code management technique.

Some other languages actually use indention to control execution.


I love homework.

import java.util.Random;

public class Card
{

    public String cardGeneration()
    {

        Random generator = new Random();

        int x; // random integer representing suit
        int y; // random integer for card value
        String mysuit, mynumber; //assigning names, these will be changed
        int suits = 4, numbers = 13; //depending what card is picked


        x = generator.nextInt(suits); // random integer between 0 & 3 inclusive

        if (x == 0)
            mysuit = "Clubs";
        else if (x == 1)
            mysuit = "Diamonds";
        else if (x == 2)
            mysuit = "Hearts";
        else if (x == 3)
            mysuit = "Spades";
        else
            mysuit = "NA";

        y = generator.nextInt(numbers) + 1; // add 1 so that y is between 1 & 13 inclusive

        if (y == 1)
            mynumber = "Ace";
        else if (y == 2)
            mynumber = "Two";
        else if (y == 3)
            mynumber = "Three";
        else if (y == 4)
            mynumber = "Four";
        else if (y == 5)
            mynumber = "Five";
        else if (y == 6)
            mynumber = "Six";
        else if (y == 7)
            mynumber = "Seven";
        else if (y == 8)
            mynumber = "Eight";
        else if (y == 9)
            mynumber = "Nine";
        else if (y == 10)
            mynumber = "Ten";
        else if (y == 11)
            mynumber = "Jack";
        else if (y == 12)
            mynumber = "Queen";
        else if (y == 13)
            mynumber = "King";
        else
            mynumber = "NA";

        String result = mynumber + " of " + mysuit;

        return result;
    }
}

Is this correct?

public class printCards
{

    public static void main (String []args)
    {
    int x = 0; // keeps count of how many card
    final int MAXCARDS = 20; // maximum number of cards

    while (x < MAXCARDS)
    {

    Card card = new Card(); //creates a card called card
    String result = card.cardGeneration(); //generates a random card
    System.out.println(result); //prints name of card
    x++;
    }
    }
}

you need to indent everything inside the brackets for example

bracket1{
     inBracket1{
          variableInBracket1
     }
     variableBracket1
}

so your code should look like this

public class printCards
{
 
    public static void main (String []args)
    {
         int x = 0; // keeps count of how many card
         final int MAXCARDS = 20; // maximum number of cards
 
         while (x < MAXCARDS)
         {
 
              Card card = new Card(); //creates a card called card
              String result = card.cardGeneration(); //generates a random card
              System.out.println(result); //prints name of card
              x++;
         }
    }
}

and this way you can see what code is in which area.

the code sirlink pasted does NOT conform to the standard Java coding style guidelines as published by Sun.
That of course does not mean they may not be applicable for some situations where the person determining what style to use has decided to use that specific pattern...

Member Avatar for hfx642

Just for reference, you shouldn't use the tab character.
It gets translated differently, depending on what tool/editor you are using.
I prefer to use 3 spaces. This makes it much easier to read.
I find that 2 spaces is still difficult to read.
4 is "acceptable", but!?!?!?
5 or more makes it difficult to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.