Need help fixing/debugging this program/code: Challenging program

Hi, I have this code that im supposed to fix, and make the program work. Ive been trying hard, but I cant it to work.

BUGGY CODE Given to me as it is(the assingment in other words)

public class Homework {
public static void main( String args[] )
{
int x = 1;
while ( x <= 8 )
{
int y = 1;
if ( x / 2 == 0)
System.out.print( "* " );
y++;
}
System.out.print( " " );
x++;
}
}

// Ok so that was the raw buggy code, Now before I show what I have been able to do, Id like to show how the program is "supposed" to work.
basically, the program is supposed to first ask me "to enter a positive number: " Now Suppose I enter 4 and press ENTER, the program is then supposed to show this.

* * * *
_* * * *
* * * *
_* * * *

// THE UNDERSCORES ( _ ) ARE NOT SUPPOSED TO SHOW, I just put them in here because, this messageboard, wont show a blank space as a first character, so I put it in as a placeholder so to speak.
// As you can see, it shows 4 rows of stars (asterisks) from top to bottom, and left to right. Also notice how the first line begins on the left, and the second line is supposed to be shifted 1 space to the right, then the next line starts on the left again, and the next line shifted one to the right once again. So its supposed to follow that pattern. So, to show another example
if I type 9 and then enter, the program should then show this:

* * * * * * * * *
_* * * * * * * * *
* * * * * * * * *
_* * * * * * * * *
* * * * * * * * *
_* * * * * * * * *
* * * * * * * * *
_* * * * * * * * *
* * * * * * * * *

// So, its 9 rows from top to bottom, and 9 stars in each row from left to right. and the 2nd, 4th, 6th, 8th rows are shifted 1 space to the right once again, when the 1st, 3rd, 5th, 7th, and 9th rows start on the left, following that pattern. remember to ignore the underscores, they arent supposed to show at all, I just typed them here to show clearer what I was trying to explain

// Ok this is what I have been able to work.

import java.io.*;

public class Midterm
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException
{


int number;

System.out.print("Enter a digit here: ");
System.out.flush();
number = Integer.parseInt(keyboard.readLine());
System.out.println();
int x=1;
while (x <=number)
{
int y= 1;
if ( x / 2 >= 0 )
System.out.print(" * ");
while (y <= number )
{
if (y / 2 !=0)
System.out.print(" * ");
y++;
}
System.out.println();
x++;
}
}
}

// What I really do not understand is the if ( x / 2 == 0 ) statement, what does it have to do with this code? I dont see how it fits or makes this work.

If someone can give me tips, and ideas on what to do, maybe a link to a site that will teach this type of particular while loops, Id much appreciated, I wouldnt mind the actual code either, but I do want to learn, so Id first rather get tips on what to look into and do. Thanks alot.

Comments

  • another example, if I were to type in 5. the program should show:

    * * * * *
    _* * * * *
    * * * * *
    _* * * * *
    * * * * *

    // Remember to ignore the UNDERSCORES, they shouldnt show in the program execution nor here. the the blank space in row2 and row4 shuold be there.
  • : another example, if I were to type in 5. the program should show:
    :
    [code]
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    [/code]
    :
    : // Remember to ignore the UNDERSCORES, they shouldnt show in the program execution nor here. the the blank space in row2 and row4 shuold be there.
    :
    [code]
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    [/code]

    The stylecode-instructions below the edit field show you how to enter text and have it appear exactly how you entered it.
    The "y/2 == 0" checks if the value stored in y is 0. Whay you want it to do is to check if it is odd or even. For this the remainder of the division must be 0, not the division result. For this Java defines the %-operator. y % 2 (y modulo 2) gives you the remainder of the division y/2.
  • : Hi, I have this code that im supposed to fix, and make the program work. Ive been trying hard, but I cant it to work.
    :
    : BUGGY CODE Given to me as it is(the assingment in other words)
    :
    : public class Homework {
    : public static void main( String args[] )
    : {
    : int x = 1;
    : while ( x <= 8 )
    : {
    : int y = 1;
    : if ( x / 2 == 0)
    : System.out.print( "* " );
    : y++;
    : }
    : System.out.print( " " );
    : x++;
    : }
    : }
    :
    : // Ok so that was the raw buggy code, Now before I show what I have been able to do, Id like to show how the program is "supposed" to work.
    : basically, the program is supposed to first ask me "to enter a positive number: " Now Suppose I enter 4 and press ENTER, the program is then supposed to show this.
    :
    : * * * *
    : _* * * *
    : * * * *
    : _* * * *
    :
    : // THE UNDERSCORES ( _ ) ARE NOT SUPPOSED TO SHOW, I just put them in here because, this messageboard, wont show a blank space as a first character, so I put it in as a placeholder so to speak.
    : // As you can see, it shows 4 rows of stars (asterisks) from top to bottom, and left to right. Also notice how the first line begins on the left, and the second line is supposed to be shifted 1 space to the right, then the next line starts on the left again, and the next line shifted one to the right once again. So its supposed to follow that pattern. So, to show another example
    : if I type 9 and then enter, the program should then show this:
    :
    : * * * * * * * * *
    : _* * * * * * * * *
    : * * * * * * * * *
    : _* * * * * * * * *
    : * * * * * * * * *
    : _* * * * * * * * *
    : * * * * * * * * *
    : _* * * * * * * * *
    : * * * * * * * * *
    :
    : // So, its 9 rows from top to bottom, and 9 stars in each row from left to right. and the 2nd, 4th, 6th, 8th rows are shifted 1 space to the right once again, when the 1st, 3rd, 5th, 7th, and 9th rows start on the left, following that pattern. remember to ignore the underscores, they arent supposed to show at all, I just typed them here to show clearer what I was trying to explain
    :
    : // Ok this is what I have been able to work.
    :
    : import java.io.*;
    :
    : public class Midterm
    : {
    : static BufferedReader keyboard = new
    : BufferedReader(new InputStreamReader(System.in));
    :
    : public static void main(String[] args) throws IOException
    : {
    :
    :
    : int number;
    :
    : System.out.print("Enter a digit here: ");
    : System.out.flush();
    : number = Integer.parseInt(keyboard.readLine());
    : System.out.println();
    : int x=1;
    : while (x <=number)
    : {
    : int y= 1;
    : if ( x / 2 >= 0 )
    : System.out.print(" * ");
    : while (y <= number )
    : {
    : if (y / 2 !=0)
    : System.out.print(" * ");
    : y++;
    : }
    : System.out.println();
    : x++;
    : }
    : }
    : }
    :
    : // What I really do not understand is the if ( x / 2 == 0 ) statement, what does it have to do with this code? I dont see how it fits or makes this work.
    :
    : If someone can give me tips, and ideas on what to do, maybe a link to a site that will teach this type of particular while loops, Id much appreciated, I wouldnt mind the actual code either, but I do want to learn, so Id first rather get tips on what to look into and do. Thanks alot.
    :





    try this code


    /*
    * star.java
    *
    * Created on November 17, 2006, 1:26 PM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */



    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    /**
    *
    * @author vivek
    */
    public class star {

    /** Creates a new instance of star */
    public static void main(String str[]) throws IOException
    {
    System.out.println("Enter no");
    BufferedReader bfr= new BufferedReader(new InputStreamReader(System.in));
    String _str1=bfr.readLine();
    int x=Integer.parseInt(_str1);
    int n=1;
    while(n<=x)
    {
    for(int i=0;i<x;i++)
    {
    System.out.print("*");
    }
    System.out.println();
    n++;
    }



    }

    }



    it works fine
    if u face any problem
    conact viv_atn@yahoo.co.in


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories