10 Replies - 359 Views - Last Post: 03 April 2019 - 02:31 PM Rate Topic: -----

#1 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Error: not a statment.

Posted 03 April 2019 - 03:50 AM

Hello!

I am back again a few days older and hopefully a little wiser.
Today I found a new error that I have not encountered before and It has me puzzled.
For background, this is what I am trying to achieve and the perimeters set.

Use (int) ( Math.random () * 6) +1; to make a dice game.
Have the program calculate the result several thousand times.
Calculate the average of all the throws.
Declare the median of all the throws
Calculate how many times each result happens.
Have the results print.
Amount of throws:
Average: x.x
Median: x.x
Amount of throws giving:
1 2 3 4 5 6
X x x x x x

To get the average we take all the throws and combine them, take that sum and divide with the number of throws. Remember to use x.0 amount of throws to avoid loss.
The median is decided by sorting the numbers and taking the number that is in the middle. (size-1)/2 (only works of the number of throws is un-even), or taking the two numbers in the middle and adding them together and dividing the result in 2.
To sort the values, use Arrays.sort.
Save the results in an array with 6 elements (0-5) with the different sides of a die, that gets increased by 1 every time the number gets thrown.
Switch/case to filter the throws.
This is my code so far:
public class Lab5_4 {
  public static void main(String[] args){
    //creat the Array
    int size =6;
    int[] intArray;
    intArray =new int[size];
    for (int i=0; i<size; i++){
      intArray[i] =i;
    }
    // creat the code for the dice game and the switch
    for (int step=0; step <=5000; step++){
      int throw =(Math.random () * 6) + 1;
        switch (throw){
            case 1:
            intArray[0]++;
            case 2:
            intArray[1]++;
            case 3:
            intArray[2]++;
            case 4:
            intArray[3]++;
            case 5:
            intArray[4]++;
            case 6:
            intArray[5]++;
          }
        }
        system.out.println(intArray.charAt[0]);
    }
  }



Posted Image

It is primarily the Error: not a statement I would like help with.
What I wanted my code to do was to calculate the throw 5000 times and sort the results into the appropriate array spaces.
I do not believe my code does this currently. I can't think of a way to implement the step without multiplying it with the throw result. I figure a way around this would be to have it multiply by the step and then divide the answer to get the original throw but it does seem like a roundabout way of doing things. Tips on a more simple way would be appreciated.

As I evidently failed at posting the image here is a link to the pasteboard with the error image Link to error

Is This A Good Question/Topic? 0
  • +

Replies To: Error: not a statment.

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: Error: not a statment.

Posted 03 April 2019 - 04:01 AM

You can't use 'throw' as the name of a variable - it's a reserved word

You can visualize your array with

System.out.println(java.util.Arrays.toString(intArray));


When you do that, you will find the sum of the distribution adds up to more than 5001. There are two reasons for that - see if you can find them

This post has been edited by g00se: 03 April 2019 - 04:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: Error: not a statment.

Posted 03 April 2019 - 04:17 AM

Oh did not know throw was a reserved word. Will see if I can figure out why it goes wonkey 😊
Was This Post Helpful? 0
  • +
  • -

#4 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: Error: not a statment.

Posted 03 April 2019 - 04:47 AM

So far what I can tell is that instead of increasing the value of array thing ( What are they called? sorry I am learning in my native language and some things do not translate well), it is storing the value of the roll ( the int formally known as throw, and adding the following rolls together.

Still working on finding the second one!
I hope it's ok that I give updates like this if not do tell :)
Was This Post Helpful? 0
  • +
  • -

#5 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: Error: not a statment.

Posted 03 April 2019 - 05:21 AM

So it rolled 500+ times per case it would appear. Fixed that with adding Break; after each case. NOw to figure out why it is rolling 5016 times instead of 5000.
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: Error: not a statment.

Posted 03 April 2019 - 05:23 AM

OK, so if you don't know what the code is doing, you need to break it down and insert debugging sysouts to see what's happening
Was This Post Helpful? 0
  • +
  • -

#7 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: Error: not a statment.

Posted 03 April 2019 - 05:36 AM

Ohhhhhh!!! I see what it is doing! It is still doing the step 5000 times per case and giving how many results came up true for that specific case and whats why it's not coming up as an exact 5000 throws!

or not... as I set the steps to go up to 3 and I still somehow managed to get 6 as a result -.-
Was This Post Helpful? 0
  • +
  • -

#8 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: Error: not a statment.

Posted 03 April 2019 - 05:48 AM

goes to read up on switch case loops as clearly. *grumbles about wanting to use if/else loops instead*
Was This Post Helpful? 0
  • +
  • -

#9 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: Error: not a statment.

Posted 03 April 2019 - 11:17 AM

I had to set the array elements to 0!
5000 rolls 5000 results! :D
Was This Post Helpful? 0
  • +
  • -

#10 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: Error: not a statment.

Posted 03 April 2019 - 01:50 PM

OK. Please post corrected code
Was This Post Helpful? 0
  • +
  • -

#11 Convalla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-March 19

Re: Error: not a statment.

Posted 03 April 2019 - 02:31 PM

Here it is in all its unfinished glory!
/* make a dice game that throws the dice 5000 times
have the program show the:
amount of throws
the average of the sum of the throws
the median of the number of the throws
Amount of throws giving:
1	2	3	4	5	6
X	x	x	x	x	x

2-3-2019 */
public class Lab5_4 {
  public static void main(String[] args){
    //creat the Array
    int size =6;
    int[] intArray;
    intArray =new int[size];
    for (int i=0; i<size; i++){
      intArray[i] =0;
    }

    // declare how many rolls.
    int rolls = 5000;
    // creat the code for the dice game and the switch
    for (int step=1; step <=rolls; step++){
      int roll = (int)(Math.random () * 6) + 1;
        switch (roll){
            case 1:
            intArray[0]++;
            break;
            case 2:
            intArray[1]++;
            break;
            case 3:
            intArray[2]++;
            break;
            case 4:
            intArray[3]++;
            break;
            case 5:
            intArray[4]++;
            break;
            case 6:
            intArray[5]++;
            break;

          }
        }
        //calculate the average numbre of the throws

        for (int i=0; i<intArray.length; i++){
        int sum=intArray[i]*(i+1);
               System.out.println("sum of throws: "+sum);
              
        }
       
        System.out.println("Number of dice throws: " +rolls);
        System.out.println("Average: ");
        System.out.println("Median: ");
        System.out.println(java.util.Arrays.toString(intArray));




    }
  }




I am now working on getting the value of each element multiplied with the side of the die it represents. It's a struggle I am thinking I may need a second array to store then new elements in and remove the int sum and have it be something like int [] Intarray2= intarray[i]*(i+1).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1