After the user inputs their choices, the program seems to offer the same choices again, followed by it completely freezing. It’s only set on there to give them the option once (I believe) and then follow the selections below. Instead it seems to revert back to the top, and then just freeze for some odd reason. Tried figuring it out for a while, but no avail.
public void init() //Initialize method {
setSize(1000, 800); //Set size
Container c = getContentPane();
c.setBackground(Color.GREEN); //Set background
}
public void paint(Graphics g) {
super.paint(g); //Start paint method
g.setFont(new Font("Veranda", Font.PLAIN, 20));
g.setColor(Color.BLACK);
g.drawString("Hello", 250, 25); //top display message
String number = JOptionPane.showInputDialog("Would you like a custom loop count or an infinite? 1. Custom 2. Infinite"); //test choice
n = Integer.parseInt(number);
while (n>0 || n<2)
if (n==1) {
String number2 = JOptionPane.showInputDialog("How many times would you like to loop?");
integer = Integer.parseInt(number2);
while (integer>0)
while (x < integer) {
g.drawString("hi", 200, y);
x+=1;
y = y+40; //test
}//end while
}//end if
else if (n==2) {
while (true); //im aware this is an endless loop. Can't find anything more stable for an endless one, so its just there for now..
}//end if
else;
g.drawString("Please pick a valid choice", 200, 200);
}//end paint
}//end
This is what I understood
The inner while ends when x = integer
but integer is always > 0
you have to do
integer --at the end of the outer loopSo it can go to 0 at some point
Please use proper indentation… it helps
I edited the code… see where I put the ‘integer –‘
try converting your
while(n>0 || n<2)toif(n>0 && n<2)Try not using
whileinstead ofif, it creates less confusionAdd brackets
{}for all your code blocks, easier to read, hence easier to debug, maybe you’ll figure it out by just doing thatAlso is the
;after the lastelseintended?