CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2010
    Posts
    1

    Unhappy Hi guys i need some help ...

    Well im new to java and am currently working on for loops heres what i have so far, now what i want to try to figure out is lets say i accept input from a user and they enter a coordinate of a point in this program lets say ... (4,6) at that coordinate in the grid instead of a ( " . ") i'd like a
    (" * ") to be displayed ..... Can u guys help me ... FYI im a network admin and java is new to me im practising these excersises ..... in my book !

    public class try21
    {
    public static void main(String [] args)
    {
    for (int i=1; i<=11; i++) // +1
    {
    System.out.println();
    for (int j=14; j>=i; j--)
    {

    System.out.print(" . ");
    }
    for (int k=1; k<=i; k++)
    {
    System.out.print(" . ");
    }
    }
    System.out.println();
    }
    }

    Out Put is :

    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    . . . . . . . . . . . . . . .
    Press any key to continue . . .

  2. #2
    Join Date
    Jul 2010
    Posts
    73

    Re: Hi guys i need some help ...

    hi try this

    Code:
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            
            //Scanner class for taking input from user
            Scanner sc = new Scanner(System.in);
           
            //Taking int as input
             System.out.println("enter x co-ordinate");
            int x = sc.nextInt();
    
            System.out.println("enter y co-ordinate");
            int y = sc.nextInt();
    
            // Y axis
            for (int i=0; i<8; i++){
               
                //X axis
                for (int j=0; j<14; j++){
                    
                    //Check for y co-ordinate
                    if (i == y){
                       
                        //Check for x co-ordinate
                        if (j == x){
                            System.out.print("*");
                        }
                        
                    }
                    System.out.print(".");
                }
    
                System.out.println();
            }
    
        }
    
    }
    You have to check for correct input means may be someone enter three instead of 3 so in this case you will get input mismatch exception. You can prevent this by asking "sc.hasNextInt()" method instead of "sc.nextInt()".

    But you are a newbie so keep practicing and you will learn a lot of things

    cheers
    Last edited by Basit781; October 7th, 2010 at 02:10 AM.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Hi guys i need some help ...

    @Basit781: Thanks for helping out with answers on the forum but we try not to just provide the answers for people who request homework type help. Most of us here take the approach of explaining the next step to the OP or guiding them in the right direction.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Jul 2010
    Posts
    73

    Thumbs up Re: Hi guys i need some help ...

    next time I'll be careful
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured