java - I need to to use a for loop to execute println() multiple times -
i need writing loop print statement multiple times based off of user-input 'sides' variable. there seems problem while loop, please let me know if syntax off.
import java.util.scanner; public class lab6 { public static void main(string[] args) { scanner scan = new scanner(system.in); int sides = 0; string poly = "error!!!!!!"; system.out.print("enter number 3 12: "); sides = scan.nextint(); while (sides > 3 || < 12 ){ system.out.println("please enter number 3 12: ") } if(sides == 3) { poly = "triangle"; } else if(sides == 4) { poly = "quadrilaterl"; } else if(sides == 5) { poly = "pentagon"; } else if(sides == 6) { poly = "hexagon"; } else if(sides == 7) { poly = "heptagon"; } else if(sides == 8) { poly = "octagon"; } else if(sides == 9) { poly = "nonagon"; } else if(sides == 10) { poly = "decagon"; } else if(sides == 12) { poly = "dodecagon"; } (sides >= 3 || <= 12){ system.out.printf("\na polygon %d sides called a(n) %s.", sides, poly);//todo:use 'for loop' here! } }
first need change
while (sides > 3 || < 12 ){ system.out.println("please enter number 3 12: ") }
to
while (sides < 3 || sides > 12) { system.out.println("please enter number 3 12: "); sides = scan.nextint(); }
then change this
for (sides >= 3 || <= 12){ system.out.printf("\na polygon %d sides called a(n) %s.", sides, poly);
to
for (int = 0; < sides; i++) system.out.printf("\na polygon %d sides called a(n) %s.", sides, poly);
that print statement, number of times there sides.
output:
enter number 3 12: 1 please enter number 3 12: 2 please enter number 3 12: 3 polygon 3 sides called a(n) triangle. polygon 3 sides called a(n) triangle. polygon 3 sides called a(n) triangle.
this looks homework though... don't want caught asking answers on here...
Comments
Post a Comment