2015-09-23 176 views
1

只有我上課的第三週(新編程)。 我正在用Java編寫一個基於文本的故事,但我在這個過程中遇到了一個問題。我有一個名爲「static String dogName;」的靜態變量我試圖改變(只有一次)的價值。在比賽開始時,用戶可以選擇給他們的狗命名。當我嘗試命名狗時,由於靜態字符串dogName,代碼會跳過命名提示。在JAVA中將非靜態變量轉換爲靜態?

  1. 我想給用戶選擇他們的狗的名字。
  2. 如果在我的代碼中有更好的方法來做事情,請告訴我。代碼

部分可能不完全一樣的決定......

public static Scanner keyboard = new Scanner(System.in); 
public static int choice; 


// dogName is Dogs name forever a hundred times rick & morty 
static String dogName; 

public static void main(String[] args) { 


    int karma = 0; 

    // Dog stuff... 
    Dog Dandy; 
    Dandy = new Dog(); 



    // Prologue 
    System.out.println("You're walking through an alley late at night " 
      + ", you see a stray dog. What do you do? "); 

    System.out.println("[1] Approach"); 
    System.out.println("[2] Attempt to touch"); 
    System.out.println("[3] Give treat"); 

    boolean running = true; 

    GAME: 
    while (running) { 


     choice = keyboard.nextInt(); 


     switch (choice) { 

      case 1: 
       System.out.println("The dog became alarmed!"); 
       Dandy.bark(); 
       break; 

      case 2: 
       System.out.println("The dog becomes aggressive!"); 
       Dandy.bite(); 
       break; 

      case 3: 
       System.out.println("The dog comes in peace"); 
       Dandy.sit(); 
       break; 
     } 

     if (choice == 1) { 
      System.out.println("You stand back in caution. You cannot risk being bitten."); 
     } 

     if (choice == 2) { 
      System.out.print(""); 
      karma--; 
     } 

     if (choice == 3) { 
      System.out.println("You give the dog a treat. It wags its tail in excitement"); 
      karma++; 
     } 

     // Chapter 1.1 - Man's best friend 

     System.out.println("\nThe dog will live a harsh life in the outside world. What would you like to do? " 
       + "\n[1] Adopt dog\n[2] Leave dog\n[3] Quit game! You're bored..."); 

     choice = keyboard.nextInt(); 
     switch (choice) { 

      case 1: 
       System.out.println("\nYou welcome your new companion"); 
       System.out.println("\nWould you like to give him a name?\n[1] No\n[2] Yes"); 
       choice = keyboard.nextInt(); 


       switch (choice){ 

        case 1: 
         System.out.println("You see a shiny object beneath his foot, it's a dog collar." 
           + "\nYou pick up the dog collar and see the name Todd on it." 
           + "\nYes, because you did not choose a name for your dog, we gave him the most basic name ever. " 
           + "You're welcome."); 

         dogName = "Todd"; //RIP doge 
         karma--; 
         break; 

        case 2: 
         dogName = keyboard.nextLine(); 
         karma++; 


      } 

     } 

     // Good guy player gives his dog a name 



     // Chapter 1.2 - Home sweet home 
     System.out.println("\n" + dogName + " crawls up to your leg and lets out a whimper.\n" 
         + "Is " + dogName + " just afraid of the dark, or is he hungry?" 
         + "\nYou don't know the last time he ate. What will you do?"); 

     System.out.println("\n[1] Go home\n[2] Find a store\n[3] Search the area"); 
     choice = keyboard.nextInt(); 


     if (choice == 1){ 
      System.out.println("\nYou head home with " + dogName + " as fast as you can.\n" 
          +"On the way back, " + dogName + " seems extremely happy to be with" 
          + " his new owner.\nGoing out you had no idea you'd bring home a new friend."); 
      karma++; 
     } 

     if (choice == 2){ 
      System.out.println(""); 
      System.out.println(""); 
     } 

     if (choice == 3){ 

     } 


    } 

    // GAME ENDING 
    if (karma > 0) { 

     System.out.println("\nYou ended with " + karma + " karma. Good job!"); 
    } 
    else if (karma == 0){ 
      System.out.println("\nYou ended with " + karma + " karma. Neither good nor bad, a neutral state."); 
    }else{ 
     System.out.println("\nYou ended with " + karma + " karma. Bad job!"); 
    } 

    // CREDITS 
    System.out.println("\n\t# THANK YOU FOR PLAYING #"); 
    System.out.println("\t# Game created by aliens from outer space #"); 
} 

}

回答

2

當我嘗試命名狗時,由於靜態字符串dogName,代碼跳過了命名提示。

不,問題與dogName是無關的。相反,問題出在您使用Scanner的方式。當您執行keyboard.nextInt()時,它只讀取足夠的數據以便能夠返回int。因此,如果用戶輸入2和Enter,掃描儀將讀取2並將其作爲int返回,並將換行符保留在輸入緩衝區中。

然後,當您用dogName = keyboard.nextLine();讀取狗的名字時,已存在的換行符會立即爲狗的名稱返回一個空字符串,而不是等待任何用戶輸入。

case 2: 
    keyboard.nextLine(); 
    dogName = keyboard.nextLine(); 
    karma++; 

第一nextLine()吃了從以前的數換行符這是類型,第二nextLine()返回線路:

你可以做的是另keyboard.nextLine()你問狗的名字之前解決這個問題文字(無換行符),可以分配到dogName

但是,您會遇到其他問題,您將遇到Scanner。如果玩家輸入的不是數字,則nextInt()會輸出InputMismatchException。可以解決這些問題,但最終會讓你頭疼。

您可能最好每次都使用keyboard.nextLine()從播放器獲取一行,然後檢查它是否包含數字並解析該數字。

此外,Java中的慣例是使用小寫字母來開始變量名稱,因此您的變量Dandy應該被命名爲dandy。其他人已經提出了一些合理的建議,關於將你的程序拆分爲多個部分,而不是採用一種單片方法。

+1

有了這篇文章,我想感謝所有幫助我的人,他們給了我建議並向我介紹新的做事方式。在您的幫助下,我修復了我的錯誤,並且我也在學習如何將所有內容連接到彼此,以便下次我的代碼更加高效。當我回到課堂時,不能等待展示我的同學:)。 –

1

幾件事情。

  1. 從您的Main方法類中分離出您的邏輯。
  2. 爲Dog創建一個POJO(我認爲你已經有一個)在構造函數參數中使用名稱的類。

    公共類犬{

    private String dogName; 
    
    public Dog(String dogName) 
    this.dogName = dogName; 
    } 
    
    //getters setters.. 
    
    } 
    
1

而不是把一切都在你的主,你就必須刪除static關鍵字,使得應用程序的新實例,像這樣:

public static void main(String[] args) { 
    Game game = new Game(); 
    game.start(); 
} 

然後choicedogName變量不再必須是靜態的。

對於一般的評論:首先將你的代碼分成多個方法,每個方法按功能分組,例如,一種處理用戶輸入的方法,一種用於打印選項的方法等。這樣,你的代碼將變得更少一團糟,並允許您稍後重構爲更輕鬆的不同類。

0

@DavidConrad's answer涵蓋了您實際的錯誤,但我想我會添加更多的「你可以做什麼更好的」

public static Scanner keyboard = new Scanner(System.in); 
public static int choice; 


// dogName is Dogs name forever a hundred times rick & morty 
static String dogName; 

注意所有這些領域怎麼是靜態的?這只是必需的,因爲您試圖從static方法(方法是main)使用它們。只有您發佈的代碼,您似乎可以將這些字段移動到main方法中,例如您創建Dog的位置。

而且,我的一個小小的寵物怨恨,但它實際上並不是一個規則或任何東西,是「過度」間距 - (像插圖中choicedogName)。一個空間很好。兩個「太」很多,我喜歡。 ;)

public static void main(String[] args) { 


int karma = 0; 

// Dog stuff... 
Dog Dandy; 
Dandy = new Dog(); 

Grr,more spaces!但更重要的是關於Dog Dandy的行。您的變量名稱爲大寫,最好使用小寫的變量名稱(,例如karma正確)。你也應該申報,並在同一行初始化Dog,像這樣:
Dog dandy = new Dog();

隨着prologuechapters,你可能會考慮這些分離成一個單獨的類。您可能會注意到每章中的模式。

  1. 一些文本給出
  2. 選項給出的選項
  3. 結果顯示

您可以通過創建這可能採取了一些introText類大大提高代碼的整體可讀性, options,然後根據choice顯示option。如果這看起來太過頭了,那麼我不會擔心它 - 這只是你的第三週,所以我不希望你有相當的差距classes,methods,和fields下降。如果這是你真正感興趣的東西,你可以找到涵蓋這些東西的所有類型的教程,當你真正理解他們能做什麼以及他們如何相互作用時,這將是非常有益的。