HI,所以我试图在代码中重新创建hangman,除非我有问题。我希望用户能够选择级别和类别,因此,我" export"相应的不同文件。除了之后的步骤是常见的...(隐藏代码,猜测)。所以我想找到一种方法,我不必粘贴代码12次,并以某种方式调用代码。那可能吗?? 非常感谢:)
为代码中的不良缩进道歉,从netbeans复制
package term1;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class HangmanFINAL {
public static void main(String[] args) {
try {
Scanner player, level, category,infile,kb;
player = new Scanner (System.in);
level = new Scanner (System.in);
category = new Scanner (System.in);
kb = new Scanner(System.in);
char again = 'n';
String secret;
StringBuffer dashes;
final int MAXPARTS = 6;
int bodyparts;
boolean done;
String guess;
String guesses;
char letter;
int playerchoice, levelchoice, categoryeasychoice, categoryhardchoice,categorymediumchoice;
//String:
//-------------------------
System.out.println("H A N G M A N ");
System.out.println("**************");
System.out.println("Choose player type: ");
System.out.println("1. 1 Player");
System.out.println("2. 2 player");
playerchoice = player.nextInt();// Scanner for player type choice
switch (playerchoice) { //player type switch
case (1): //1 Player
System.out.println("1 PLAYER");
System.out.println(" How to play:");
System.out.println(" The computer will give think of a word and you have to guess it."
+ "\n You will be indicated how many letter the word is made up of. "
+ "\n You can enter possible letters you think may be in the word. "
+ "\n If you think you know the entire word, you can enter it!"
+ "\n Good luck! :)");
System.out.println("Choose level:");
System.out.println("1. Easy");
System.out.println("2. Medium");
System.out.println("3. Hard");
levelchoice=level.nextInt();
switch (levelchoice)
{
case(1): //easy
System.out.println("Easy cateogry options:");
System.out.println("1. Jobs");
System.out.println("2. Colours");
System.out.println("3. Fruits");
System.out.println("4. Clothes");
System.out.println("Choose a category:");
categoryeasychoice=category.nextInt();
switch (categoryeasychoice){
case (1):
infile = new Scanner(new FileReader("/Users/name/Desktop/jobseasy.txt"));//HERE
break;
case(2):
infile = new Scanner(new FileReader("/Users/name/Desktop/colourseasy.txt"));//HERE
break;
case(3):
infile = new Scanner(new FileReader("/Users/name/Desktop/fruitseasy.txt"));//HERE
break;
case(4):
infile = new Scanner(new FileReader("/Users/name/Desktop/clotheseasy.txt"));//HERE
break;
default:
System.out.println(+categoryeasychoice +"is not a valid option");
}
break;
case(2): //medium
System.out.println("Medium cateogry options:");
System.out.println("1. Jobs");
System.out.println("2. Colours");
System.out.println("3. Fruits");
System.out.println("4. Clothes");
System.out.println("Choose a category:");
categorymediumchoice=category.nextInt();
switch (categorymediumchoice){
case (1):
infile = new Scanner(new FileReader("/Users/name/Desktop/jobsmedium.txt"));//HERE
break;
case(2):
infile = new Scanner(new FileReader("/Users/name/Desktop/coloursmedium.txt"));//HERE
break;
case(3):
infile = new Scanner(new FileReader("/Users/name/Desktop/fruitsmedium.txt"));//HERE
break;
case(4):
infile = new Scanner(new FileReader("/Users/name/Desktop/clothesmedium.txt"));//HERE
break;
default:
System.out.println(+categorymediumchoice +"is not a valid option");
break;
} //categorymedium
case(3): //hard
System.out.println("Hard cateogry options:");
System.out.println("1. Jobs");
System.out.println("2. Colours");
System.out.println("3. Fruits");
System.out.println("4. Clothes");
System.out.println("Choose a category:");
categoryhardchoice=category.nextInt();
switch (categoryhardchoice){
case (1):
infile = new Scanner(new FileReader("/Users/name/Desktop/jobshard.txt"));//HERE
break;
case(2):
infile = new Scanner(new FileReader("/Users/name/Desktop/colourshard.txt"));//HERE
break;
case(3):
infile = new Scanner(new FileReader("/Users/name/Desktop/fruitshard.txt"));//HERE
break;
case(4):
infile = new Scanner(new FileReader("/Users/name/Desktop/clotheshard.txt"));//HERE
break;
default:
System.out.println(+categoryhardchoice +"is not a valid option");
break;
}
break;
default: //invalid levelchoice
System.out.println(levelchoice + "is not a valid level. "
+ "\n Choose between (1) Easy, (2) Medium, (3) Hard");
} //levelchoice switch
case (2): //2 player
System.out.println("2 PLAYER");
System.out.println("Instructions:");
System.out.println(" ");
System.out.print(" Enter names:");
break;
default: //player incorrect playerchoice
System.out.println(playerchoice +"is not an option. Choose between player 1 and 2");
} //player choice switch
} //main
catch (FileNotFoundException ex) {
Logger.getLogger(HangmanFINAL.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void matchLetter(String secret, StringBuffer dashes, char letter)
{
for (int index = 0; index < secret.length(); index++)
if (secret.charAt(index) == letter)
dashes.setCharAt(index, letter);
System.out.print("good guess - ");
}
public static StringBuffer makeDashes(String s)
{
StringBuffer dashes = new StringBuffer(s.length());
for (int count=0; count < s.length(); count++)
dashes.append('-');
return dashes;
}
}//class
这个位: / *此代码需要重复:
do {
// play a game
secret = infile.next();
guesses = "";
done = false;
bodyparts = MAXPARTS;
// make dashes
dashes = makeDashes(secret);
while (! done)
{
System.out.println("Here is your word: " + dashes);
System.out.println("Guesses so far: " + guesses);
System.out.print("enter a guess (letter or word): ");
guess = kb.next();
// process the guess
if (guess.length() > 1) // process word guess
{
if (guess.equals(secret))
System.out.println("you win!");
else
System.out.println("you lose");
done=true;
}
else // process single letter guess
{
letter = guess.charAt(0);
guesses += letter;
if (secret.indexOf(letter) < 0) // not there
{ --bodyparts;
System.out.print("bad guess - ");
}
else // letter is in the secret
{
// put it in dashes where it belongs
matchLetter(secret, dashes, letter);
}
System.out.println(bodyparts + " bodyparts are left");
if (bodyparts == 0)
{ System.out.println("you lose");
done = true;
}
if (secret.equals(dashes.toString()))
{ System.out.println("you win!");
done = true;
}
} // process single letter guess
} // process whole guess
if (infile.hasNext())
{
System.out.print("play again (y/n)?: ");
again = kb.next().charAt(0);
}
else
System.out.println("thanks for playing (no more words)");
} while (infile.hasNext() && (again == 'Y' || again == 'y'));
*/