Java 使用方法防止代码(?)或对象重复&;上课?

Java 使用方法防止代码(?)或对象重复&;上课?,java,class,object,exception,methods,Java,Class,Object,Exception,Methods,您好,我正在尝试用代码重新创建hangman,除非我有问题。我希望用户能够选择级别和类别,因此,我相应地“导出”不同的文件。除了之后的步骤是常见的。。。(隐藏代码,猜测)。因此,我想找到一种方法,不必将代码粘贴12次,并以某种方式调用代码位。这可能吗?? 非常感谢:) 抱歉代码中的缩进太差,从netbeans复制 package term1; import java.io.FileNotFoundException; import java.io.FileReader; import java

您好,我正在尝试用代码重新创建hangman,除非我有问题。我希望用户能够选择级别和类别,因此,我相应地“导出”不同的文件。除了之后的步骤是常见的。。。(隐藏代码,猜测)。因此,我想找到一种方法,不必将代码粘贴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
package term1;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.util.Scanner;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公开课绞刑决赛{
公共静态void main(字符串[]args){
试一试{
扫描仪播放器,级别,类别,内嵌,kb;
player=新扫描仪(System.in);
级别=新扫描仪(系统英寸);
类别=新扫描仪(System.in);
kb=新扫描仪(System.in);
char再次='n';
串秘密;
字符串缓冲破折号;
最终int MAXPARTS=6;
int车身部件;
布尔完成;
字符串猜测;
字符串猜测;
字符字母;
int playerchoice、levelchoice、CategoryAnchoice、categoryhardchoice、categorymediumchoice;
//字符串:
//-------------------------
System.out.println(“H A N G M A N”);
System.out.println(“****************”);
System.out.println(“选择播放器类型:”);
System.out.println(“1.1播放器”);
System.out.println(“2.2播放器”);
playerchoice=player.nextInt();//用于选择播放器类型的扫描仪
开关(播放器选择){//播放器类型开关
案例(1)://1名玩家
System.out.println(“1名玩家”);
System.out.println(“如何播放:”);
计算机会给你一个单词,你必须猜它
+“\n将指示该单词由多少个字母组成。”
+“\n您可以在word中输入您认为可能存在的字母。”
+\n如果您认为您知道整个单词,可以输入它
+“\n祝你好运!:)”;
System.out.println(“选择级别:”);
System.out.println(“1.Easy”);
System.out.println(“2.Medium”);
System.out.println(“3.Hard”);
levelchoice=level.nextInt();
开关(水平选择)
{
案例(1)://简单
System.out.println(“简单目录选项:”);
System.out.println(“1.Jobs”);
系统输出打印(“2.颜色”);
System.out.println(“3.水果”);
系统输出打印(“4.衣服”);
System.out.println(“选择一个类别:”);
categoryeaschoice=category.nextInt();
开关(类别选择){
案例(1):
infle=newscanner(newfilereader(“/Users/name/Desktop/jobsasy.txt”);//此处
打破
案例(2):
infle=new Scanner(new FileReader(“/Users/name/Desktop/colorseasy.txt”);//此处
打破
案例(3):
inflee=newscanner(newfilereader(“/Users/name/Desktop/fruitseasy.txt”);//此处
打破
案例(4):
infle=new Scanner(new FileReader(“/Users/name/Desktop/clotheseasy.txt”);//此处
打破
违约:
System.out.println(+categoryeaschoice+“不是有效选项”);
}
打破
案例(2)://中等
System.out.println(“中等类别选项:”);
System.out.println(“1.Jobs”);
系统输出打印(“2.颜色”);
System.out.println(“3.水果”);
系统输出打印(“4.衣服”);
System.out.println(“选择一个类别:”);
categorymediumchoice=category.nextInt();
开关(类别媒体选择){
案例(1):
infle=newscanner(newfilereader(“/Users/name/Desktop/jobsmedium.txt”);//此处
打破
案例(2):
infle=new Scanner(new FileReader(“/Users/name/Desktop/colorsmedium.txt”);//此处
打破
案例(3):
infle=newscanner(newfilereader(“/Users/name/Desktop/fruitsmedium.txt”);//此处
打破
案例(4):
infle=new Scanner(new FileReader(“/Users/name/Desktop/clothesmedium.txt”);//此处
打破
违约:
System.out.println(+categorymediumchoice+“不是有效选项”);
打破
}//分类媒体
案例(3)://困难
System.out.println(“硬目录选项:”);
System.out.println(“1.Jobs”);
系统输出打印(“2.颜色”);
System.out.println(“3.水果”);
系统输出打印(“4.衣服”);
System.out.prin
    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'));


*/