View Single Post
  #1 (permalink)  
Old 07-31-2007, 10:20 PM
cachi cachi is offline
Member
 
Join Date: Jul 2007
Posts: 40
cachi is on a distinguished road
Help, loop with java
Hi, I have a program that produces a scrambled word and the user has to unscramble it. I want to make it like a game where when you get it right the score goes up and when you get it wrong your life goes down. Right now i have everything set up and it adds and subtracts correctly but I'm not sure about how to make it run through more then once i know i need a loop but I'm not sure where to put it.

Code:
import java.io.*; import java.util.*; public class Guess { public static void main(String Args[]) { //instructions for player System.out.println("Try to unscramble the words!! All letters are in lower case."); int score = 0; //goes up if right int life = 5; //goes down if wrong int a = rand(1, 4); //get random number String Sword = random(a); //use random number to get random scrabbled word String Nword = normWord(a); //returns normal word System.out.println("Word to unscramble: " + Sword); //print scrabbled word //next line is the setup for reading stuff in from the user InputStreamReader input = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(input); String line = ""; try { //read the string in from the user System.out.print("Please enter your guess: "); line = (console.readLine()); } catch(IOException e) { System.out.println("Something went wrong with the input"); } if(check(line,Nword)) //if you get guess right this happens { score = score + 1; System.out.println("Your Score = " + score); System.out.println("Your life = " + life); } else //if you guess wrong this happens { life = life - 1; System.out.println("Your Score = " + score); System.out.print("Your life = " + life); } } static String random (int a) //returns scrabled word { String[] x = {"eholl", "hwat", "arlc", "hmatos"}; String Sword = ""; String Nword = ""; if(a == 1) { Sword = x[0]; Nword = "hello"; } if(a == 2) { Sword = x[1]; Nword = "what"; } if(a == 3) { Sword = x[2]; Nword = "carl"; } if(a == 4) { Sword = x[3]; Nword = "thomas"; } return Sword; } static String normWord (int a) //returns normal word { String[] x = {"eholl", "hwat", "arlc", "hmatos"}; String Sword = ""; String Nword = ""; if(a == 1) { Sword = x[0]; Nword = "hello"; } if(a == 2) { Sword = x[1]; Nword = "what"; } if(a == 3) { Sword = x[2]; Nword = "carl"; } if(a == 4) { Sword = x[3]; Nword = "thomas"; } return Nword; } static int rand(int lo, int hi) //gets random number to get random word { Random rn = new Random(); int n = hi - lo + 1; //System.out.println(n); int i = rn.nextInt() % n; //System.out.println(i); if (i < 0) i = -i; return lo + i; } static boolean check (String line, String word) //checks to see if correct { int i = 1; String answer = ""; if(line.equals(word)) i = 1; else i = 0; if(i == 1) { System.out.println("Correct"); return true; } else { System.out.println("Incorrect"); return false; } } }
Thanks.
Reply With Quote
Sponsored Links