如何在java中打印此数组 [英] How do I make this array print in java

查看:70
本文介绍了如何在java中打印此数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试制作一个简单的游戏但我不知道为什么但是我的阵列没有打印。我还在学习,所以我无法弄明白为什么。我使用NetBeans IDE进行编码。这里的代码(当我运行它时,玩家阵列不打印)



TextBattle.java



Hello I am trying to make a simple game and I don't know why but my array isn't printing. I'm still learning so I can't figure out why. I code using the NetBeans IDE. Here the code (When I run it, the array of players isn't printing)

TextBattle.java

package textbattle;
import java.util.Arrays;
import java.util.Scanner;

public class TextBattle {
    
    public static void main(String[] args) {
        characterCreate();
    }
    
    public static void characterCreate(){
        
            String playerType;

            System.out.println("Create a character");
            Scanner beginCharScan = new Scanner(System.in);
            System.out.println("What is the name of your character: ");
            String addPlayer = beginCharScan.nextLine();
            System.out.println("What element is your character(water, fire, or grass)");
            playerType = beginCharScan.nextLine();
            Players player = new Players();
            switch(playerType){
                case "fire":
                    playerType = beginCharScan.nextLine();
                    player.Player(addPlayer, playerType);
                    break;
                case "water":
                    playerType = beginCharScan.nextLine();
                    player.Player(addPlayer, playerType);
                    break;
                case "grass":
                    playerType = beginCharScan.nextLine();
                    player.Player(addPlayer, playerType);
                    break;
                default:
                    System.out.println("That is not a valid type");
                    characterCreate();                        
            }
            
        }
    
        public static void Game(){
            
            System.out.println("What do you want to do?");
            Scanner choiceScan = new Scanner(System.in);
            System.out .println("Create another character (type 'Create')");
            System.out .println("Attack a different character (type 'Attack')");
            String choice = choiceScan.nextLine();
            
            switch(choice){
                case "Create":
                    characterCreate();
                    break;
                case "Attack":
                    break;
                default:
                    System.out.println("That is not a valid choice");
                    Game();
            }
        }
        
        public static void printPlayers(String playersArray[]){
            String printText = "Players exsisiting: " + Arrays.toString(playersArray);
        }
    
}





Players.java





Players.java

package textbattle;

import java.util.Arrays;

public class Players {
    
    int playerNum = 0;
    int setPlayerNum;
    
    public void Player(String addPlayer, String playerType){
        String playersArray[] = new String[4];
        String typeArray[] = new String[4];
        
        playersArray[playerNum] = addPlayer;
        typeArray[playerNum] = playerType;
        
        playerNum++;
        setPlayerNum = playerNum;
        
        TextBattle bat = new TextBattle();
        bat.printPlayers(playersArray);
        
    }
    
}





提前致谢!!



我尝试了什么:



我试过查找视频,谷歌搜索它,尽我所能。



Thanks in advance!!

What I have tried:

I have tried looking up videos, "googling it", and as much as I could.

推荐答案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较do。

当代码没有达到预期效果时,你就接近了一个bug。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于如何在java中打印此数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆