Results 1 to 9 of 9
  1. #1
    marylanddem is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default Need help with code

    I'm looking for help with the following problem. In this problem, As is the # of darts thrown and is entered by the user. I want to create code where for every dart thrown, assign a random x and y. If x and y are less than or equal to one. increase area of dart board (Ac) by 1. Then use Ac to calculate Pi.
    I can't get this to compile. Any help?:confused:

    import java.io.*;
    import java.util.Random;

    public class DartsCircle
    {
    public static void main(String[] args) throws IOException
    { //listing of variables
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    double x, y, Pi, As, Ac;
    String strAs;


    for (As
    x = Math.random();
    y = Math.random();
    Ac=0)

    if 1 <= ((x*x) + (y*y)); Ac++;

    System.out.println("Enter the number of darts");
    strAs = dataIn.readLine();
    As = Double.parseDouble(strAs);
    Pi = (4*Ac)/As;


    System.out.println("Pi =" + Pi);

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by marylanddem View Post
    ... I can't get this to compile. Any help?:confused:
    What errors are you getting and which line is causing the error? Also, please check out my first signature link about using code tags. Luck!

  3. #3
    marylanddem is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    I'm getting "(" and start of illegal expression for line 18.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    It seems to me that your code won't compile.

    Work slowly - one line or block of code at a time - compiling as you go, so that compiler messages don't build up. If you get a compiler message that you can't understand copy and the post the whole thing and someone will have a go at explaining what it means.

    (It is a very good idea to choose variable names wisely. Variable start with a lower case letter and, if they're composed of multiple words they use camelCase. The names should reflect what the variables are. Pi is pi and is well known: but As and Ac might be better chosen.)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by marylanddem View Post
    I'm getting "(" and start of illegal expression for line 18.
    Your line 18 is not the same as ours. Which line is #18 for you?

  6. #6
    marylanddem is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    oops. Sorry! line 18 is this: if 1 <= ((x*x) + (y*y)); Ac++;

    They are expecting a ( at the 1 and says that the ; after )) is an illegal expression

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by marylanddem View Post
    oops. Sorry! line 18 is this: if 1 <= ((x*x) + (y*y)); Ac++;

    They are expecting a ( at the 1 and says that the ; after )) is an illegal expression
    It's telling you that you're writing your if blocks wrong. You'll need to re-study the format for how to write these in your texts/notes/tutorials and you'll see where you're doing this wrong. Also you'll not want the semicolon in there as it ruins the if statement. Again, the tutorials/books will show you this.

    I second pbrockway's recommendation though. You need to compile your code often and fix all compilation errors before adding any more code, else you'll have a program with too many errors to fix.

    Luck!

  8. #8
    marylanddem is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    Okay. I re-read the chapter and came up with this. However, it says that the { before the x random code is illegal anyidea why?
    import java.io.*;
    import java.util.Random;

    public class DartsCircle1
    {
    public static void main(String[] args) throws IOException
    { //listing of variables
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    double x, y, P, Darts, Circle;
    String strDarts;
    Circle=0;



    System.out.println("Enter the number of darts");
    strDarts = dataIn.readLine();
    Darts = Double.parseDouble(strDarts);


    for (Double.parseDouble(strDarts);
    {
    x = Math.random();
    y = Math.random();
    }


    if ((x*x) + (y*y) <= 1) Circle++;

    P = (4*Circle)/Darts)


    System.out.println("P =" + P);

    }

    }

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Likewise the for loop syntax is wrong. Again hit the books and write a correct for loop (it's the thing with three parts to it).

    As with the if statement the ; at the end of the line is also wrong.

    And think about what you want to happen inside the for loop. You don't just want to assign some values to x and y. (If you don't understand how the program is going about calculating pi you should ask about that.)

Similar Threads

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 05:21 AM
  2. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

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