简体   繁体   English

如何使Java获得一个数字和一个新数字并将它们加在一起

[英]How to make java get a number and a new number and add them together

I Have been trying for some time now to get java to get and number and add a neww number to it but have had no luck. 我已经尝试了一段时间,以获取java并进行编号,并向其中添加neww编号,但是没有运气。

package me.FL.PlayTime;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import org.bukkit.entity.Player;

public class PlayTimePlayer {
    long join = 0;
    Player player = null;

    private File f;
    public PlayTimePlayer(Player player){
        this.player = player;
        this.join = System.currentTimeMillis();

    }

    public void hold(){
        int seconds = (int) ((getMiliPlayTime() / (1000)) % 60);
        int minutes = (int) ((getMiliPlayTime() / (1000*60)) % 60);
        int hours   = (int) ((getMiliPlayTime() / (1000*60*60)) % 24);
        int days    = (int) ((getMiliPlayTime() / (1000*60*60*24)) % 24);
    }

    public int getMiliPlayTime(){
        return (int) (System.currentTimeMillis() - join);
    }

    public int getSeconds(){
        return(int) (getMiliPlayTime() / 1000) % 60 ;
    }

    public int getMinutes(){
        return (int) ((getMiliPlayTime() / (1000*60)) % 60);    
    }

    public int getHours(){
        return (int) ((getMiliPlayTime() / (1000*60*60)) % 24);
    }

    public int getDays(){
        return (int) ((getMiliPlayTime() / (1000*60*60*24)) % 24);
    }

    public String getTime(){
        String ptime = getDays() + ":" + getHours() + ":" + getMinutes() + ":" + getSeconds();
        System.out.println(ptime);
        return ptime;
    }

    public void save() throws IOException{
        if (Playtime.hascreatedfolder = false){
            Scanner s = new Scanner(new File("Fileloc"));
            while(s.hasNextLine()){
            String time = s.nextLine();
            String[] split = time.split(":");

                int days = Integer.parseInt(split[0]);
                int newday = getDays();
                int newTimeday = newday + days;

                int hours = Integer.parseInt(split[1]);
                int newhour = getHours();
                int newTimeHour = newhour + hours;

                int minutes = Integer.parseInt(split[2]);
                int newminute = getMinutes();
                int newTimeminute = newminute + minutes;

                int seconds = Integer.parseInt(split[3]);
                int newsecond = getSeconds();
                int newTimesecond = newsecond + seconds;


                File f = new File("plugins/PlayerTime/");
                f.mkdirs();

                File file = new File(player.getDisplayName() + ".txt");
                if (f.exists());

                f.createNewFile();

                try {
                    BufferedWriter w = new BufferedWriter(new FileWriter(file));
                    w.write(getTime());
                    w.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

i want it to get a time for example lets say it had 1:0:0 and i wanted to add 30 minutes i will like it to get 1:0:0 from a txt file and add 30 minutes from the server i should get 1:30:0 but i get 0:30:0 instead also it overwrited the oldnumbers txt file i dont want it to do this. 我想让它有时间,例如,假设它有1:0:0,我想增加30分钟,我希望它从txt文件中获得1:0:0,并从服务器中增加30分钟,我应该得到1:30:0,但我得到0:30:0,但它也覆盖了oldnumbers txt文件,我不希望它这样做。 please let me know how to fix this. 请让我知道如何解决此问题。

The issue is that here: 问题是在这里:

try {
    BufferedWriter w = new BufferedWriter(new FileWriter(file));
    w.write(getTime());
    w.close();
} catch (IOException e) {
    e.printStackTrace();
}

You're only writing the new times in; 您只是在写新的时代。 you've not passed your newTimeincrements anywhere or done anything with them. 您尚未在任何地方传递newTimeincrement或对它们进行任何操作。 if you were to change it to, say, w.write(newTimeday + ":" + newTimehour + ":" + newTimeminute + ":" + newTimesecond); 如果您将其更改为w.write(newTimeday + ":" + newTimehour + ":" + newTimeminute + ":" + newTimesecond); you would be writing your new values in. If there's a ClassCastException there, you can also use something like 您将在其中写入新值。如果那里有ClassCastException,您也可以使用类似

w.write(String.valueOf(newTimeday) + ":" + String.valueOf(newTimehour) + ":" + String.valueOf(newTimeminute) + ":" + String.valueOf(newTimesecond));

I don't use Buffered writers, so I don't know if it'll simply accept the integers in the string without conversion. 我不使用缓冲编写器,所以我不知道它是否仅接受字符串中的整数而不进行转换。 Second option is just in case it doesn't. 第二种选择是为了防万一。

Hard to tell from looking at your code and reading your brief description what exactly you're trying to achieve, so here are a few notes that might help you to get going: 通过查看代码和阅读简短描述很难分辨出您到底要实现什么目标,因此这里有一些注意事项可能对您有所帮助:

  • You have an incorrect boolean condition hascreatedfolder = false , I think you want to use == unless it's meant to be an assignment? 你有一个不正确的布尔条件hascreatedfolder = false ,我想你想使用== ,除非它的意思是赋值?
  • you have no validation but the code seems to be expecting the format "days:hours:minutes:seconds" with four colon-separated values, whereas from your description you seem to have three ("hours:minutes:seconds"). 您没有进行验证,但是代码似乎期望格式为“ days:hours:minutes:seconds”,带有四个冒号分隔的值,而根据您的描述,您似乎拥有三个(“ hours:minutes:seconds”)。
  • You have several unnecessary casts from int to int (not a problem per se, just being pedantic) 您从int到int有一些不必要的强制转换(本质上不是问题,只是just脚)
  • unused instance variable File f (again just being pedantic) 未使用的实例变量File f(再次是pedantic)

I'd recommend you step through your code with a debugger and see where it's doing something different from what you're expecting. 我建议您使用调试器逐步检查代码,看看它在做什么与您的期望有所不同。

Edit: As another general tip for what I think you're trying to do, have a look at joda-time and use that to add times/dates. 编辑:作为我认为您要尝试做的另一项一般性提示,请看一下joda-time并使用它来添加时间/日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM