2016-09-13 66 views
0

我做了一個文本冒險,變成基於我喜歡的動畫的紙牌遊戲,基本上我將所有冗長的纏繞文本移動到私有靜態空洞中,以便「清理」主要方法。首先這是最好的方式去做呢?其次,這是我的主要問題,在公共靜態字符串jojiFunai我問用戶的名字。但是我想在整個代碼中使用這個變量,我該如何解決這個問題?對不起,這是一口,我只是不知道該去哪裏。如何從方法中檢索變量

import java.util.Scanner; 
    import java.util.Random; 

    public class Ecard { 

    public static void main(String[] args) { 
     Scanner intKeyboard = new Scanner(System.in); 
     boolean emperor = false; 
     boolean slave = false; 

     // Player 1 Deck 
     int emperorPlayer1 = 1; 
     final int citizenPlayer1 = 4; 
     // Player 2 Deck 

     // opening options 

     // activates which deck player chooses 
     // boolean emperor= false, slave= false; 

     int deckChoice; 

     wakeUp(); 
     jojiFunai(); 

     System.out.println(name); 
     gameRules(); 
     deckChoice(); 
//This portion will be turned into a class Deck 
//  Random computer = new Random(); 
// 
//  while (emperor = true && emperorPlayer1 != 0) { 
//   System.out.println("What card will you put down ?"); 
//   System.out.println("Options: \n 1. emperor card(" + emperorPlayer1 
//     + ")"); 
//   System.out.println("2. citizen card(" + citizenPlayer1 + ")"); 
//   int computerChoice = computer.nextInt(2) + 1; 
//   deckChoice = intKeyboard.nextInt(); 
//   if (deckChoice == 1) { 
//    emperorPlayer1--; 
//    System.out.println("You have placed a emperor card down."); 
//    if (deckChoice == computerChoice) { 
//     System.out.println("It looks like you just lost,HAHAHA"); 
       } 
      } 
     } 

     // This will talk about the rules of each deck and what deck has 
     // what 
     // final int emperorPlayer1 = 1; 
     // final int citizenPlayer1 = 4; 
     // Player 2 Deck 
     // final int slavePlayer2 = 1; 
     // final int citizenPlayer2 = 4; 
     // 

    } 

    private static void wakeUp() { 
     System.out.println("You awaken at the sound of your alarm clock," 
       + " do you go turn off the alarm? Y/N"); 
     Scanner keyboard = new Scanner(System.in); 
     String alarm = keyboard.nextLine(); 
     if (alarm.equalsIgnoreCase("y")) { 
      System.out.println("As you hit the awake button you hear the" 
        + " sound of knocking at your front door."); 
     } else if (alarm.equalsIgnoreCase("n")) { 
      System.out.println("You hit the snooze button" 
        + " and fall back into a deep sleep," 
        + " avoiding what would have happened next. END"); 
      System.exit(0); 

     } 

    } 

    public static String jojiFunai() { 
     Scanner keyboard = new Scanner(System.in); 
     Scanner intKeyboard = new Scanner(System.in); 
     System.out`enter code here` 
       .println("As you reach for the door, you are suddenly hit from behind......"); 
     System.out 
       .println("You hear the faint words coming from the figure 'We have him, we are talking him to the boat', darkness however surrounds you. "); 
     System.out 
       .println("................................................................"); 
     System.out 
       .println("................................................................"); 
     System.out 
       .println("................................................................"); 
     System.out 
       .println("................................................................"); 
     System.out.println("'Hey buddy, wake up!!!!'"); 
     System.out.println("'Hey im talking to you! What is your name?'"); 
     String name = keyboard.nextLine(); 
     System.out 
       .println("'Nice to meet you " 
         + name 
         + ". My name is Joji Funai and from what those people told me," 
         + " looks like your my opponent for the card game we will be playing.'"); 
     System.out.println("OPTIONS:"); 
     System.out.println("1. Where am I?"); 
     System.out.println("2. Refuse."); 
     System.out.println("3. Accept."); 
     int funaiQuestion = intKeyboard.nextInt(); 
     switch (funaiQuestion) { 
     case 1: 
      System.out 
        .println("Your in a ship, seems that you signed a contract in which you failed to uphold, the only way you can pay up is through gambling.. so lets get started."); 
      break; 
     case 2: 
      System.out 
        .println("I wish you didnt say that' Joji instantly pulls out a pistol ending your life before you knew what happened"); 
      System.exit(0); 
     case 3: 
      System.out 
        .println("Without asking any questions? Someone has some confidence, I like that! now let me explain the rules...."); 
      break; 

     } 
     return name; 

    } 

    private static void gameRules() { 
     System.out.println("///////////////////////////////GAME" 
       + " RULES//////////////////////////////////////////////////"); 

     System.out.println("'This game is called E-Card, each player" 
       + " takes turns playing three rounds" 
       + " on the 'emperor' side and the 'slave' side.'"); 
     System.out.println("The game is starts with each player holding" 
       + " five cards, four of which are Citizens" 
       + " and the last being either a Slave or an Emperor." 
       + " Who holds which card is decided beforehand," 
       + " and is played like that for three turns before switching."); 
     System.out.println("The game lasts for a total of twelve turns," 
       + " meaning each side gets to play Emperor twice. "); 
     System.out.println("The players choose one card from their hand" 
       + " and place them face-down on the table." 
       + " They are then flipped over to reveal the winner " 
       + "of the match.The three card types are Citizen," 
       + " Emperor and Slave."); 
     System.out.println("The Citizen card represents the common man," 
       + " and cannot defeat the Emperor who sits at" 
       + " the top. It can, however, defeat the Slave," 
       + " who resides at the very bottom of the system." 
       + " Two citizen against each other results in a tie."); 
     System.out.println("The Emperor represents the one at the top of" 
       + " society. This card can defeat the citizen," 
       + " but will lose to the Slave."); 
     System.out.println("The Slave is presented as the one at the very" 
       + " bottom of society. Seeing as how it has nothing" 
       + " to lose, it can overthrow the Emperor in one last" 
       + " attempt at revenge. This card will lose to a Citizen," 
       + " but will win against the Emperor card."); 
     System.out.println("//////////////////////////////////////////////" 
       + "GAME RULES///////////////////////////////////"); 

     System.out.println("Sorry for being so long winded, anyways, ill let" 
       + " you choose first, would you like the \n 1.emperor" 
       + " deck or the \n 2.slave deck?"); 

    } 

    private static void deckChoice() { 
     Scanner intKeyboard = new Scanner(System.in); 
     int deckChoice = intKeyboard.nextInt(); 

     if (deckChoice == 1) { 
      System.out.println("It looks like you chose the risky deck " 
        + " I like that!"); 
      boolean emperor = true; 
     } else if (deckChoice == 2) { 
      System.out.println("I can tell your frightened" 
        + ". Well i dont blame ya, HAHA"); 
      boolean slave = true; 
     } 
     System.out.println("Funai slams the deck on the table."); 

    } 

回答

0

你的方法已經返回值,但你只需要抓住它

name = jojiFunai(); 
    System.out.println(name); 
+0

它看起來像它現在再次循環jojiFunai但它打印的名字從它循環 – twokdavey

+0

有第二次在你的代碼中沒有循環,而jojiFunal方法只被調用一次。我要做的一件重要事情不是創建多個掃描器對象,只需執行一次,並通過將掃描器對象作爲字段或將掃描器對象作爲參數傳遞給方法,在方法中重新使用該變量。 –

+0

我相信當我分配字符串名稱jojiFunai();它再次讓它循環,我想知道它爲什麼這樣做。 – twokdavey