有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么是系统。nanoTime()返回这么大的数字,而这么长的时间还没有过去?

所以我对Java非常陌生,但我正在为CompSci制作一个文本冒险游戏,这是我的关卡代码

public static int level(int exp, Long time, int levelnum) {
  //Time is the time elapsed since the program started
    time = System.nanoTime();
    //I divi  de by 10,000,000 twice because that sqared is 100 trillion, the conversion factor between nano and second
   exp = (int)(Math.round(time/10000000));
   exp = Math.round(exp/10000000);
    //The exp, or experience, is the percent, under 100, of the way to the next level. 1 is 10%, 2 is 20, etc.
    while (exp > 10){
        //This loop will check to make sure exp is under 10. If not, it will add one to the level number, and then subtract 10 from the exp and check again.
        levelnum++;
        exp = exp - 10;
    } 
    
   int bar;
   bar =1; 
   //Bar is here because originally, I planned on this all being one method, the next one and this, and so I placed it in meaning for it to act as the return value. It has stayed the return value.
   System.out.println(levelnum + "\n" + exp + "\n" + time);
   //That was for debugging purposes, so I could see the levels data as it processed.
   progBar(levelnum, exp);
    return bar;

    
}
public static void progBar(int levelnum, int exp){
    char barOpen, barClose;
    String bar = "";
    String emptyBar;
    //I realize now that I could have just "[" + bar + "]", but at the time i didnt think of that
    barOpen = '[';
    barClose = ']'; 
    if (exp > 1){
        //ok so if the experience is greater than 1, then we repeat the = that many times. That way, we don't repeat it when we have one
    bar = "=".repeat(exp);
    }else if (exp <= 1){
    bar = "=";
    }
    //This makes sure we have the adequate space between the experience and the bar close
    emptyBar = " ".repeat(10-exp);
    System.out.println("You are currently level " + levelnum + "\n" + barOpen + bar + emptyBar + barClose);
     
}

当我昨天运行这个时,它成功地实现了水平限制。然而,今天的系统。nanoTime()已经开始给出非常大的数字,即使是在不同的机器中,它们都不能准确地表示所经过的时间。我怎样才能解决这个问题


共 (1) 个答案

  1. # 1 楼答案

    //Time is the time elapsed since the program started
    time = System.nanoTime();
    

    不,不是

    nanoTime返回的实际数字没有具体含义。它只能用于测量对nanoTime的两次调用之间经过的持续时间。这些调用必须在同一台机器上运行的同一个程序中。即使在同一台机器上,不同运行之间也无法进行比较