| | |
convert python to java
![]() |
Why do that? Let your Python code run in Java:
http://www.jython.org/
It's a python interpreter written in pure Java! Snazzy.
http://www.jython.org/
It's a python interpreter written in pure Java! Snazzy.
Alex Cavnar, aka alc6379
•
•
Join Date: Apr 2005
Posts: 8
Reputation:
Solved Threads: 0
HERE IS THE CODE
import javax.swing.JOptionPane;
//=== CONSTANTS ====
public class version21
{
//decalaration
private int LOWER_EASY = 0;
private int UPPER_EASY = 100;
private boolean FIRST = true;
private boolean OTHER = false;
String numG;
private int numGoes, totalGoes, numCorrect;
private boolean start, carryOn;
private int num;
private int guess,getGuess;
private boolean playAgain;
private boolean tryAgain;
private boolean retValue;
private boolean numGuesses;
public static void main(String [] args)
{
// === declare and initialise main's local variables ===
numGoes = 0; //number of times the game is played
totalGoes = 0; // total number of guesses made
start = true;
numCorrect = 0;
// === BEGIN MAIN LOOP FOR THE GAME ===
while (start){
numGuesses = 0;
boolean carryOn = true;
String s;
int guess = Integer.parseInt(JOptionPane.showInputDialog(null,"pick a number between 1 and 100"));
int num = Random.nextInt(UPPER_EASY-LOWER_EASY)+LOWER_EASY; //range between LOWER_EASY and UPPER_EASY
System.out.println("For testing purposes the num generated is: "+num);
numGuesses++;
}//while (start)
// === GET USERS GUESSES. LOOP 'TIL THEY EITHER GET IT RIGHT OR GIVE UP ===
while (guess != num && carryOn)
{
carryOn = tryAgain(guess, num);
if (carryOn)
{
guess = getGuess(OTHER);
numGuesses += 1;
}//if (carryOn)
}//while guess
if (guess == num)
{
numG = numGuesses;
String sInput = JOptionPane.showMessageDialog ("Great! You got it right in " + numG + " goes!");
numCorrect += 1;
totalGoes += numGuesses;
start = playAgain();
}//if
// === END MAIN-LOOP
outputResults(numCorrect, numGoes, totalGoes);
public static int getGuess()
{
System.out.println ( "... getting guess");
guess = 0; // some dummy value to pass back
JOptionPane.showInputDialog(null, "guess");
}//getguess()
public static String tryAgain()
{
System.out.printLn(" .... seeing if user wants to guess again");
retValue = false; // some dummy value to pass back
return JOptionPane.showMessageDialog(null, "retValue");
}//tryagain()
public static String playAgain()
{
retValue = false; // some dummy value to pass back
System.out.printLn( "That Game's over.... checking for play again ... ");
return JOptionPane.showMessageDialog(null, "retValue");
}//playagain()
public static int outputResults(String correct, int timesPlayed, int totalAttempts)
{
System.out.printLn( "Thanks for playing.. hope you had fun...");
System.out.printLn( "You played" + timesPlayed +"times.");
System.out.printLn( "You guessed correctly" + correct + "times, and you gave up" + timesPlayed - correct + "times");
}//outputresults()
public static String validResponse( )
{
System.out.println( "...checking that value supplied by user is in the right range");
ok = true; // some dummy value to pass back
return JOptionPane.showMessageDialog(null, "ok");
}//validresponse()
}//Version21
import javax.swing.JOptionPane;
//=== CONSTANTS ====
public class version21
{
//decalaration
private int LOWER_EASY = 0;
private int UPPER_EASY = 100;
private boolean FIRST = true;
private boolean OTHER = false;
String numG;
private int numGoes, totalGoes, numCorrect;
private boolean start, carryOn;
private int num;
private int guess,getGuess;
private boolean playAgain;
private boolean tryAgain;
private boolean retValue;
private boolean numGuesses;
public static void main(String [] args)
{
// === declare and initialise main's local variables ===
numGoes = 0; //number of times the game is played
totalGoes = 0; // total number of guesses made
start = true;
numCorrect = 0;
// === BEGIN MAIN LOOP FOR THE GAME ===
while (start){
numGuesses = 0;
boolean carryOn = true;
String s;
int guess = Integer.parseInt(JOptionPane.showInputDialog(null,"pick a number between 1 and 100"));
int num = Random.nextInt(UPPER_EASY-LOWER_EASY)+LOWER_EASY; //range between LOWER_EASY and UPPER_EASY
System.out.println("For testing purposes the num generated is: "+num);
numGuesses++;
}//while (start)
// === GET USERS GUESSES. LOOP 'TIL THEY EITHER GET IT RIGHT OR GIVE UP ===
while (guess != num && carryOn)
{
carryOn = tryAgain(guess, num);
if (carryOn)
{
guess = getGuess(OTHER);
numGuesses += 1;
}//if (carryOn)
}//while guess
if (guess == num)
{
numG = numGuesses;
String sInput = JOptionPane.showMessageDialog ("Great! You got it right in " + numG + " goes!");
numCorrect += 1;
totalGoes += numGuesses;
start = playAgain();
}//if
// === END MAIN-LOOP
outputResults(numCorrect, numGoes, totalGoes);
public static int getGuess()
{
System.out.println ( "... getting guess");
guess = 0; // some dummy value to pass back
JOptionPane.showInputDialog(null, "guess");
}//getguess()
public static String tryAgain()
{
System.out.printLn(" .... seeing if user wants to guess again");
retValue = false; // some dummy value to pass back
return JOptionPane.showMessageDialog(null, "retValue");
}//tryagain()
public static String playAgain()
{
retValue = false; // some dummy value to pass back
System.out.printLn( "That Game's over.... checking for play again ... ");
return JOptionPane.showMessageDialog(null, "retValue");
}//playagain()
public static int outputResults(String correct, int timesPlayed, int totalAttempts)
{
System.out.printLn( "Thanks for playing.. hope you had fun...");
System.out.printLn( "You played" + timesPlayed +"times.");
System.out.printLn( "You guessed correctly" + correct + "times, and you gave up" + timesPlayed - correct + "times");
}//outputresults()
public static String validResponse( )
{
System.out.println( "...checking that value supplied by user is in the right range");
ok = true; // some dummy value to pass back
return JOptionPane.showMessageDialog(null, "ok");
}//validresponse()
}//Version21
![]() |
Similar Threads
- Convert python code to C++ (C++)
- Python with Java? (Python)
- converting python to java (Java)
Other Threads in the Python Forum
- Previous Thread: Help with programming assignment
- Next Thread: Sort an Inventory List (Python)
| Thread Tools | Search this Thread |
alarm anydbm app beginner cipher cmd coordinates curves cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp handling homework http images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion recursive schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver terminal text threading time tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia wxpython xlwt






