JAVA: ¡¿Si / Else / For Methods ?! ¿QUÉ?

I can't even do the lo esencial. ¿Qué estoy haciendo mal?

Necesito:

  1. Draw a "X" made up of stars (*). I must prompt for the width of the X in stars.

My requirements for this assignment is:
+1 - prompt for size of X
+4 - draw X of stars (receive +2 if can draw solid square of stars)

Estoy usando eclipsar a propósito!

import java.util.Scanner; 

/*
* 
*
* Description: Draws a X.
*/

public class Tutorial1
{   
    public static void main(String[] args)
    {   
        Scanner sc = new Scanner(System.in);
        int i,j;
        System.out.print("Enter size of box: 4 ");
        int size = sc.nextInt();

        for (i=0; i < size; i++)
        {
            for (j=0; j < size; j++)
            {
                if ( (i == 0)    // First row
                  || (i == size-1)   // Last row
                  || (j == 0)    // First column
                  || (j == size-1) )     // Last column
                    System.out.print("*");  // Draw star
                else
                    System.out.print(" ");  // Draw space
            }
            System.out.println();
        }
    }
} //

preguntado el 09 de marzo de 12 a las 15:03

Please elaborate on what exactly is confusing you. -

What specific problems are you having? What errors are you seeing? Consider removing some of the unnecessary text at the beginning of your question and replacing it with information that could actually help us answer you. -

@Vienne Sung at stackoverflow the majority of people just want the question. Spend less time with the introduction (which most will not care about) and more on your coding problem. This way you are more likely to keep people happy. -

Please also change the title of your question to something more descriptive. -

I would make sure you understand how the existing code works first. If you don't, ask specific questions about it here and make it clear what you've already done (for example, if you puzzled by the Scanner class and you've tried reading the Scanner javadoc, say that). If you understand the existing code but are stuck on making it draw an X, explain what you've tried and what happened. Welcome to Stackoverflow -

3 Respuestas

Your program draws a box correctly.

Enter size of box: 4 7
*******
*     *
*     *
*     *
*     *
*     *
*******

You need to change your code so it draws a cross instead. The code is actually simpler as you have just two lines instead of four.

Quitaría el 4 from the prompt as it confusing.

Enter size of box: 7
*     *
 *   * 
  * *  
   *   
  * *  
 *   * 
*     *

respondido 09 mar '12, 15:03

Tú ya sabes qué you problem. You stated it yourself: "I can't even do the Conceptos básicos".

Then learn the basics. There is no way around QUE.

This site is not a "write me a piece of code that does X" service. People will help you only with specific questions on a specific problem. Your task is actually beginner stuff that is pretty simple una vez you grasped the basic concepts. Failing that, any solution we may provide here will be useless to you, since you don't even understand how the problem was solved. Worse, your teach will most likely notice pretty fast that you did not write that on your own. That screws you double - you get charged of cheating and still haven't learned cualquier cosa.

respondido 09 mar '12, 15:03

Here is the skeleton of what you need. The for loops will iterate through the table. The hard part is coming up with the algorithm for deciding which character to print.

public class Tutorial1
{ 
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int i,j;
        System.out.print("Enter size of box: ");
        size = sc.nextInt();

        Tutorial1 t = new Tutorial1();
        t.printX(size);
    }

    private int _size = 0;

    public void printX(int size) {       
        _size = size;
        for(int row = 0; row < _size;row++) {
            for(int col = 0; col< _size;col++) {
              System.out.print(getChar(row,col));
            }
            System.out.println();
        }
    }

    private String getChar(int row, int col) {
        //TODO: create char algorithm
        //As a pointer, think about the lines of the X independently and
        //how they increment/decrement with the rows
    }
}

respondido 09 mar '12, 17:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.