So I'm pretty new at Java, but I'm making a text adventure game for CompSci, and this is my code for levels.
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 + "
" + exp + "
" + 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 + "
" + barOpen + bar + emptyBar + barClose);
}
When I ran this yesterday, it succeeded in the level barring. However, today System.nanoTime() has begun to give extremely large numbers, even in different machines, none of which accurately represent the time which has elapsed. How could I fix this?
question from:
https://stackoverflow.com/questions/65896593/why-is-system-nanotime-returning-such-a-large-number-when-that-much-time-has 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…