java - 在 android studio 项目中显示文本

标签 java android xml android-studio

试图让一个类似于此的程序在 android 平台上运行。问题是我不确定使用 xml 来显示文本/读取用户输入。我是移动软件开发的新手,但从下面的代码来看,我只是想显示打印语句,显然也获取用户输入。

    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(" -------------------------- ");


    }
}

最佳答案

是的,一般都是用TextView来显示文本,编辑文本供用户输入的文本。

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

developer.android 培训文章非常好,即使对于初学者也是如此。 将 res/layout 文件夹中的 xml 文件用于 UI 内容。如果您点击底部的“设计”选项卡,Android Studio 甚至可以让您看到您的 UI 的外观。

旁注:您会发现它比 System.out.println() 有趣得多

关于java - 在 android studio 项目中显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33891344/

相关文章:

java - 比较 2 个整数,奇怪的行为

java - tomcat升级后并行流不设置Thread.contextClassLoader

android - SVM 不适用于 android studio

android - Chrome 手机 : URL bar causes trouble with footer

java - Android TextView 左右对齐文本

android - Eclipse新建项目后加载XML布局的AppTheme失败,显示找不到对应的样式

xml - 如何在 ActionScript 中针对 XML 架构测试 XML 的有效性?

java - 如何在 windows 7 上为 tomcat 添加 jvm 选项

Java 正则表达式异常?

xml - 直接加载到浏览器中时,XSL 不会呈现 XML