java - Can I better use memory in this program? -
the code below works way want work. wanting see if using necessary amount of memory , not of it. want go programming career nice hear of professionals out there.
import java.util.*; public class guessinggame { public static void main(string[] args) { int counter = 0, number, guess, yes = 1, no =2, answer; scanner input = new scanner(system.in); number = ((int) (math.random() * 100)); system.out.println("would play guessing game?"); system.out.println("enter " + yes + " yes, , enter " + no + " no"); answer = input.nextint(); if(answer == no) { return; } else { while (answer == yes) { system.out.println("welcome guessing game"); system.out.println("there has been number between 1 , 100 picked \n have 5 chances guess number"); system.out.print("please enter guess >> "); guess = input.nextint(); while (guess != number) { if (guess != number) { counter = ++counter; } if (counter == 5) { system.out.println("you have used guesses"); system.out.println("the number " + number); system.out.println("would play again?"); system.out.println("enter " + yes + " yes , " + no + " no"); answer = input.nextint(); counter = -1; if (answer == no) return; } else if (guess < number) { system.out.println("you need guess higher"); system.out.println("you have used " + counter + " guess(es)"); system.out.println("please enter new guess"); guess = input.nextint(); } else if (guess > number) { system.out.println("you need guess lower"); system.out.println("you have used " + counter + " guess(es)"); system.out.println("please enter new guess"); guess = input.nextint(); } } system.out.println("you win"); system.out.println("would play again"); system.out.println("enter " + yes + " yes , " + no + " no"); answer = input.nextint(); if(answer == no) return; } } } }
i wouldn't worry memory optimisation unless have memory profiled application , has found problem. time money , 1 gb of memory cheaper hour of time, 1 mb worth few seconds , 1 kb isn't worth time takes press 1 key, , not using 1 kb of objects.
in bigger program suggest use jmc
profile application, , in particular @ allocation rate , think how can decrease it.
Comments
Post a Comment