使用方法中的代码的Java不起作用 [英] Java using code from method doesn't work

查看:65
本文介绍了使用方法中的代码的Java不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用特定的代码,但是由于某些原因,它将无法正常工作.我必须在同一个类中使用方法:

I'm trying to use a specific code but it won't work for some reason. I have to methods in the same class:

public void InputEnter()
    {       
        if(Input.GetKey(getCoords)) {
            Move(GetTransform().GetPos());
        System.out.println((GetTransform().GetPos()));
        }
}

一旦按下回车键,此方法将为我提供Vector3f的一些坐标.其他代码将写入文件.

this method gives me some coordinates of Vector3f once I hit enter. The other code writes to a file.

public void ProcessText()
     {
        System.out.println("ProcessText Operational");

        String file_name = "C:/Users/Server/Desktop/textText.txt";

        try
        {           
            ProcessCoords file = new ProcessCoords(file_name);
            String[] aryLines = file.OpenFile();

            int i;
            for (i = 0; i < aryLines.length; i++)
            {
                System.out.println(aryLines[i]);    

                 if(aryLines[i].startsWith("makeGrass:")) {
                        String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
                        String[] ArgArray = Arguments.split(",");

                        this.makeGrass(Double.parseDouble(ArgArray[0]), 
                                   Double.parseDouble(ArgArray[1]), 
                                   Double.parseDouble(ArgArray[2]));                    
                }
            }

            ProcessCoords data = new ProcessCoords(file_name);      
            data.writeToFile("makeGrass:");

            System.out.println("Coordinates Saved!");

        } catch(IOException e) {            
            System.out.println(e.getMessage());
        }
     }

我想做的是在ProcessText方法中使用InputEnter方法,所以我删除了InputEnter并在ProcessText方法中使用了Input代码:

What I wanted to do is to use the InputEnter method in the ProcessText method so I just deleted InputEnter and used the Input code in the ProcessText method:

public void ProcessText()
     {
        System.out.println("ProcessText Operational");

        String file_name = "C:/Users/Server/Desktop/textText.txt";

        try
        {           
            ProcessCoords file = new ProcessCoords(file_name);
            String[] aryLines = file.OpenFile();

            int i;
            for (i = 0; i < aryLines.length; i++)
            {
                System.out.println(aryLines[i]);    

                 if(aryLines[i].startsWith("makeGrass:")) {
                        String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
                        String[] ArgArray = Arguments.split(",");

                        this.makeGrass(Double.parseDouble(ArgArray[0]), 
                                   Double.parseDouble(ArgArray[1]), 
                                   Double.parseDouble(ArgArray[2]));                    
                }
            }

            if(Input.GetKey(getCoords)) {
                Move(GetTransform().GetPos());
            ProcessCoords data = new ProcessCoords(file_name);                  
            data.writeToFile("makeGrass:");

            System.out.println("pressing enter doesn't work!!");

            System.out.println((GetTransform().GetPos()));
            }

            System.out.println("Input.GetKey doesn't work anymore, but why and how to fix it??");
        } catch(IOException e) {            
            System.out.println(e.getMessage());
        }
     }

但是现在,按Enter键不再像以前那样为我提供坐标,我真的不明白为什么,我需要一些帮助.

however now, pressing enter does no longer give me the coordinates as it did before, I really do not understand why and I would need some help.

非常感谢!

推荐答案

好的,我花了一段时间,但我已经弄清楚了,它实际上很简单:

Okay it took me a while but I've figured it out, It's actually very simple:

正如您在ProcessText()中看到的那样,我同时包含了从文件读取的代码和写入文件的代码.

As you can see in ProcessText() I've included both the code that reads from a file and the code that writes to a file.

ProcessCoords data = new ProcessCoords(file_name);      
data.writeToFile("makeGrass:");

System.out.println("Coordinates Saved!");

然后我的想法是将Input方法放入ProcessText方法中,如您在此处看到的那样:

My idea was then to put the Input method into the ProcessText method as you can see here:

if(Input.GetKey(getCoords)) {
Move(GetTransform().GetPos());
ProcessCoords data = new ProcessCoords(file_name);                  
data.writeToFile("makeGrass:");
System.out.println("pressing enter doesn't work!!");
System.out.println((GetTransform().GetPos()));

这几乎是正确的,但是很好..为了使gameObject具有输入功能,我需要将Input类添加为组件:

This is almost correct but well.. to have the input work for a gameObject I need to add the Input class as a component:

gameObject.addComponent(new InputClass());

gameObject.addComponent(new InputClass());

我要做的就是从ProcessText方法中将其取出,然后将其移至Input类中,如下所示:

All I had to do instead is to take it out from my ProcessText method and move it into my Input class so it looks like this:

public void Input(float delta)
{           
String file_name = "C:/Users/Server/Desktop/textText.txt";

try
{           
    ProcessCoords data = new ProcessCoords(file_name);

    if(Input.GetKey(getCoords)) {   
    data.writeToFile("makeGrass:" + (GetTransform().GetPos()));

    System.out.println("Coordinates Saved!");   
    System.out.println((GetTransform().GetPos()));
    }   

    } catch(IOException e) {
        System.out.println(e.getMessage());
    }
}

此后,我才能够实际使用各个gameObject的输入,并且仅在按Enter时,显然可以将适当的坐标写入文本文件中.

After that I was able to actually use the input for the respective gameObject and obviously get the apropriate coordinates writen to the text file only if I press enter.

结果如下: http://www.pic-upload .de/view-27748157/AnotherExample.png.html

我希望我的回答将来能对其他人有所帮助!

I hope my answer will help someone else in the future!

这篇关于使用方法中的代码的Java不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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