Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Python Program converted into Java

Sooo I started taking my second computer science class ever! For my first class we used python and for this class we're using Java. Our first assignment (pretty much just practice) is to convert this craps program from Python to Java and I'm just having a hell of a time.

Could someone please help with what I've done and umm give me some advice? Maybe a good site for a beginner.... Someone that kinda knows Python (only from a first CS course perspective).

1) In python

def winCraps():
roll = rollDice()
if roll == 7 or roll == 11:
    return True
elif roll == 2 or roll == 3 or roll == 12:
    return False
else:
    return rollForPoint(roll)

This is my attempt at the conversion of it over to java

public int winCraps{
    roll = rollDice();
    if (roll = 7 && 11){
    return (true);
    }
    else (roll =2 && 3 && 12){
    return(false);
    }
    else{
    return rollforPoint(roll);
}
}

2) Python

def rollDice():
raw_input("Press <Enter> to roll...")
die1 = randrange(1,7)
die2 = randrange(1,7)
sum = die1 + die2
print "You rolled", die1, "+", die2, "=", sum
return sum

This one confused the hell out of me. What would randrange be in Java??

Java

static int rollDice(){
System.out.print("Press <Enter> to roll...");
double die1 = Math.random();
double die2 = Math.random();
die1 = (int) die1*6+1;
 die2 = (int) die2*6+1;
 int sum = (int)die1 + (int)die2;
 System.out.println("You rolled  "+die1+ " + "+die2+ " = "+sum+".");
 return sum;
 }

*please bear in mind that I'm just learning this stuff lol

Answer*

Draft saved
Draft discarded

Required fields are marked with *

Cancel
0

default