2015-11-24 35 views
0

尝试获取类似于此的程序以在Android平台上工作。麻烦的是我不确定使用xml来显示文本/读取用户输入。我对移动软件开发相当陌生,但从下面的代码中,我只是试图显示打印语句,显然也得到了用户输入。在Android工作室项目中显示文本

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

    public class assignmentMSD 
    { 
     public static void main(String[] args) 
     { 
      //objects(system) 
      Scanner in = new Scanner(System.in); 
      Random rand = new Random(); 

     //Variables used 

     String[] enemies = {"Zombie", "Necromorph", "Soldier", "MadMax"}; 
     int maxEnemyHealth = 75; 
     int enemyAttackDamage = 25; 

     //Player Variables 
     int health = 100; 
     int attackDamage = 50; 
     int numHealthElixers = 3; 
     int healthPotionHeal = 30; 
     int healthPotionDropChance = 50; 

     boolean running = true; 

     System.out.println("Welcome to the Thunderdome"); 

     GAME: 
     while(running) 
     { 
      System.out.println("--------------------------------------------------"); 

      int enemyHealth = rand.nextInt(maxEnemyHealth); 
      String enemy = enemies[rand.nextInt(enemies.length)]; 
      System.out.println(" \t####" + enemy + " has appeared#### \n"); 

      while(enemyHealth > 0) 
      { 
       System.out.println("\t Your Health:" + health); 
       System.out.println("\t" + enemy + "'s Health:" + enemyHealth); 
       System.out.println("\n \t What would you like to do?"); 
       System.out.println("\t 1. Attack"); 
       System.out.println("\t 2. Drink Health Potion"); 
       System.out.println("\t 3. Run"); 

       String input = in.nextLine(); 
           if(input.equals("1")) 
           { 
            int damageGiven = rand.nextInt(attackDamage); 
            int damageTaken = rand.nextInt(enemyAttackDamage); 

            enemyHealth -= damageGiven; 
            health -= damageTaken; 

            System.out.println("\t You strike the " + enemy + " for " + damageGiven + " damage."); 
            System.out.println("\t You take " + damageTaken + " in combat:"); 

            if(health < 1) 
            { 
             System.out.println("\t You have taken too much damage and therefore are too weak to fight!"); 
             break; 

            } 


           } 
           else if(input.equals("2")) 
           { 
            if(numHealthElixers > 0) 
            { 
             health += healthPotionHeal; 
             numHealthElixers--; 
             System.out.println("You drink a heakth potion for " + healthPotionHeal + "." 
                  + "\n\t You now have" + health + " HP." 
                  +"\n\t You have " + numHealthElixers + " Health Elixers left. \n"); 

            } 
            else 
            { 
             System.out.println("\t You have no health Elixer left!, You must defeat enemies for a chance to get more \n"); 

            } 
           } 
           else if(input.equals("3")) 
           { 
            System.out.println("\t You run away from the " + enemy + "!"); 
            continue GAME; 
           } 
           else 
           { 
            System.out.println("\t\n Invalid Command"); 
           } 
      } 

         if(health < 1) 
         { 
          System.out.println("You Flee from the Thunderdome, weak from battle"); 
          break; 
         } 

         System.out.println("-------------------------------"); 
         System.out.println(" # " + enemy + "was defeated! # "); 
         System.out.println(" # You have" + health + " HP left. #"); 
         if(rand.nextInt(100) < healthPotionDropChance) 
         { 
          numHealthElixers++; 
          System.out.println(" # The " + enemy + " dropped a health potion #"); 
          System.out.println(" # You now have " + numHealthElixers + " Health Elixer(s). #"); 

         } 
         System.out.println("-------------------------------"); 
         System.out.println("What would you like to do now?"); 
         System.out.println("1. Continue Fighting"); 
         System.out.println("2. Exit Dungeon"); 

         String input = in.nextLine(); 

         while(!input.equals("1") && !input.equals("2")) 
         { 
          System.out.println("invalid command"); 
          input = in.nextLine(); 

         } 

         if(input.equals("1")) 
         { 
          System.out.println("You continue through the thunderdome"); 
         } 
         else if(input.equals("2")) 
         { 
          System.out.println("You exit the thunderdome a wiser man, sucessful on your quest"); 
          break; 
         } 
     } 

       System.out.println(" -------------------------- "); 
       System.out.println(" ----Thanks for playing----"); 
       System.out.println(" -------------------------- "); 


    } 
} 
+1

只需使用TextView显示文本,然后使用EditText作为用户输入,请查看:http://developer.android.com/guide/topics/ui/controls/text.html。我还推荐一些关于Android基础知识的视频教程:http://www.newthinktank.com/2014/06/android-beginners/ –

+0

您正在上面做一个完整的网格。而且我知道你甚至不会理解文档中的单词。我建议你看看这些https://www.youtube.com/watch?v=gcPgdra10Oc&index=5&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa –

回答

2

是的,你通常使用TextView来显示文本,并编辑文本供用户输入。

这是一个很好的链接查看:http://developer.android.com/training/basics/firstapp/building-ui.html

的developer.android训练的文章都非常好,即使是初学者。 使用res/layout文件夹中的xml文件来处理UI的东西。 Android Studio甚至可以让您看到如果您单击底部的“设计”选项卡,UI的外观。

侧面说明:你会发现它更多的乐趣比的System.out.println()

+0

如果我将文本转换为字符串并将其放在文本视图中怎么办? – Bryan

1

首先在android系统,你需要定义一个布局。用途:

  • EditText更换用户的输入(Scanner
  • TextView更换给用户的输出(System.out.println

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/some_label_text"/> 
<EditText 
    android:id="@+id/example_input" 
    android:hint="@string/some_hint_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

注:标签,如果文本是预定义的,可以在定义中具有文本。

通过这样的id将你的对象引用到你的java应用中。

EditText exampleInput = (EditText)dialogCarro.findViewById(R.id.example_input); 

,你将需要添加ListenersButton S或EditText秒。

+0

我是否必须用新的文本视图替换每个system.out.print,还是都可以从一个 – Bryan

+0

来完成取决于您的请求,但是在一个单独的流程中必须是足够的 –

+0

目前它只是为了获得它在android上工作。我得到了将文本转换为字符串并将其放入文本视图的建议,但这需要多个文本视图。如果现在有办法在一个文本视图中显示所有内容,那么我认为这将是一个更好的选择。 – Bryan