Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBenji Beck
648 PointsHow to run this Java code in IntelliJ?
The code makes sense to me, but I'm not sure how to run this code. It's my first time creating functions in IntelliJ. Your thoughts?
Error:(5, 9) java: illegal start of expression Error:(5, 28) java: ';' expected Error:(5, 39) java: ';' expected Error:(24, 9) java: illegal start of expression Error:(24, 32) java: ';' expected
package com.company;
public class Main {
public static int main(String[] args) {
public int diceRoll (int sides){
//This expression generates a random double in the interval
//[0, sides). That is, a double greate than or equal to 0
//and less than sides.
double randomNumber = Math.random() * sides;
//Our random number is now in the interval [1, sides +1)
randomNumber = (randomNumber + 1);
//Casting the random number to an integer will round it down to an
//integer in the 1 to sides range
return (int) randomNumber;
}
/**
* The function monopolyRoll will simulate two dice rolls,
* It will then store the sum
* Check to see if rollOne and rollTwo are equal
* If so, roll again and add new sums to previous total
* If not, simply return sum of the first two rolls
*/
public int monopolyRoll () {
int rollOne = diceRoll(6);
int rollTwo = diceRoll(6);
int total = rollOne + rollTwo;
if (rollOne == rollTwo) {
int rollThree = diceRoll(6);
int rollFour = diceRoll(6);
total = total + rollThree + rollFour;
}
return total;
}
}
}
1 Answer
Alexander Davison
65,469 PointsApparently, you wrote all of your methods within the main
method