Results 1 to 4 of 4
Thread: Need help to write Hangman
- 04-11-2012, 01:57 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
-
Re: Need help to write Hangman
This post shows no evidence of your current efforts, nor does it show a specific answerable question. You're going to have to do better if you want to increase your chances of getting help here. You would do well to read the link below on how to ask smart questions.
- 04-11-2012, 03:20 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help to write Hangman
That is going to be a 'knitting' program, i.e. everything crammed in one huge method (particularly your main( ... ) method). I wrote a hangman class once; it has six small methods (all less than 10 lines of code). I'm willing to show it to you after you've shown at least a bit of design effort; all you have now is a random selection of a word (in a quite clumsy way, I must say). Your turn.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-11-2012, 04:30 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Re: Need help to write Hangman
Ok, i have wrote:
But in output it shows this:import java.util.Scanner;
import java.lang.*;
public class Hangman{
public static void main(String[]args){
Scanner scn =new Scanner(System.in);
System.out.println("Would you like to play Hangman. Press y to play n to quit!!!");
String ch="";
int length=ch.length();
String start = scn.nextLine();
if(start.equals("N") || start.equals("n")){
System.exit(1);
}
else if(start.equals("Y") || start.equals("y")){
int series=(int)(Math.random()*6)+1;
while(start.equals("Y") || start.equals("y")){
switch(series){
case 1:
ch=("thecloser"); break;
case 2:
ch=("survivor"); break;
case 3:
ch=("merlin"); break;
case 4:
ch=("house"); break;
case 5:
ch=("simpsons"); break;
case 6:
ch=("dexter"); break;
case 7:
ch=("friends"); break;
}
length=ch.length();
for(int i=1; i<=length; i++){
System.out.print("_ ");
StringBuffer guess=new StringBuffer(length);
String g2 = "";
String guessCh="";
System.out.println(" guess");
guessCh=scn.next();
if(ch.charAt(i) == guessCh.charAt(0) )
{
g2 = g2+ch.charAt(i);
guess.setCharAt(2*i,guessCh.charAt(0));
}
g2 ="";
System.out.print(guess.toString());
}
}
System.out.println("(You have 10 tries remaining)");
System.out.println(length+" letter");
System.out.println("Please enter your guess");
String guess=scn.next();
}}}
http://imageupload.org/en/file/214864/adsapz.png.html
i fixed it, too. But this crash shows up now :/
http://imageupload.org/en/file/214882/adsapz.png.html
for this code
import java.util.Scanner;
import java.lang.*;
public class Hangman{
public static void main(String[]args){
Scanner scn =new Scanner(System.in);
System.out.println("Would you like to play Hangman. Press y to play n to quit!!!");
String ch="";
int length=ch.length();
int i=0;
String start = scn.nextLine();
if(start.equals("N") || start.equals("n")){
System.exit(1);
}
else if(start.equals("Y") || start.equals("y")){
int series=(int)(Math.random()*6)+1;
while(start.equals("Y") || start.equals("y")){
switch(series){
case 1:
ch=("thecloser"); break;
case 2:
ch=("survivor"); break;
case 3:
ch=("merlin"); break;
case 4:
ch=("house"); break;
case 5:
ch=("simpsons"); break;
case 6:
ch=("dexter"); break;
case 7:
ch=("friends"); break;
}
length=ch.length();
for(i=1; i<=length; i++){
System.out.print("_ ");
}
StringBuffer guess=new StringBuffer(length);
String g2 = "";
String guessCh="";
System.out.println(" guess");
guessCh=scn.next();
if(ch.charAt(i) == guessCh.charAt(0) )
{
g2 = g2+ch.charAt(i);
guess.setCharAt(2*i,guessCh.charAt(0));
}
g2 ="";
System.out.print(guess.toString());
}
System.out.println("(You have 10 tries remaining)");
System.out.println(length+" letter");
System.out.println("Please enter your guess");
String guess=scn.next();
}}}Last edited by Bremin; 04-11-2012 at 05:59 PM.
Similar Threads
-
Hangman with GUİ
By KSBeyaz in forum New To JavaReplies: 9Last Post: 11-20-2011, 06:45 PM -
Hangman
By Feriscool in forum New To JavaReplies: 23Last Post: 05-17-2011, 06:54 PM -
trying to write a program for hangman word game
By durdanto in forum New To JavaReplies: 1Last Post: 02-12-2011, 02:53 AM -
hangman
By coltragon in forum New To JavaReplies: 2Last Post: 01-16-2010, 09:56 AM -
Hangman Help!!!
By chinasome in forum New To JavaReplies: 5Last Post: 11-08-2008, 02:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks