问题描述:

I need help writing a for loop that will print a statement multiple times based off of a user-input 'sides' variable. There also seems to be a problem with my while loop, please let me know if my syntax is 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 a number from 3 to 12: ");

sides = scan.nextInt();

while (sides > 3 || < 12 ){

System.out.println("Please enter a number from 3 to 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";

}

for (sides >= 3 || <= 12){

System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);//TODO:Use a 'for loop' here!

}

}

First you need to change

```
while (sides > 3 || < 12 ){
System.out.println("Please enter a number from 3 to 12: ")
}
```

to

```
while (sides < 3 || sides > 12) {
System.out.println("Please enter a number from 3 to 12: ");
sides = scan.nextInt();
}
```

Then change this

```
for (sides >= 3 || <= 12){
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);
```

to

```
for (int i = 0; i < sides; i++)
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);
```

That will print the statement, the number of times there are sides.

Output:

```
Enter a number from 3 to 12: 1
Please enter a number from 3 to 12: 2
Please enter a number from 3 to 12: 3
A polygon with 3 sides is called a(n) Triangle.
A polygon with 3 sides is called a(n) Triangle.
A polygon with 3 sides is called a(n) Triangle.
```

This looks like homework though... You don't want to get caught asking for answers on here...

Use this

```
Scanner scan = new Scanner(System.in);
int sides = 0;
String poly = "ERROR!!!!!!";
System.out.print("Enter a number from 3 to 12: ");
sides = scan.nextInt();
while (sides < 3 || sides > 12 ){
System.out.println("Please enter a number from 3 to 12: ");
sides = scan.nextInt();
}
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";
}
if (sides >= 3 || sides <= 12){
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);//TODO:Use a 'for loop' here!
}
}
```

Output

```
Enter a number from 3 to 12: 0
Please enter a number from 3 to 12: 1
Please enter a number from 3 to 12: 5
A polygon with 5 sides is called a(n) Pentagon.
```

```
while (sides > 3 || < 12 )
```

should be

```
while (sides < 3 || sides > 12 ){
System.out.println("Please enter a number from 3 to 12: ");
sides = scan.nextInt();
}
```

And this block

```
for (sides >= 3 || <= 12){
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);//TODO:Use a 'for loop' here!
}
```

Should be:

```
System.out.printf("\nA polygon with"+ sides + "sides is called" + poly);
```

No need to use for loop just to print the above statement.

Use switch instead of multiple conditions. In while cycle you need also to read new input:

```
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 a number from 3 to 12: ");
sides = scan.nextInt();
while (sides < 3 || sides > 12 ){
System.out.println("Please enter a number from 3 to 12: ")
sides = scan.nextInt();
}
switch (sides){
case 3:
poly = "Triangle";
break;
case 4:
poly = "Quadrilaterl";
break;
case 5:
poly = "Pentagon";
break;
case 6:
poly = "Hexagon";
break;
case 7:
poly = "Heptagon";
break;
case 8:
poly = "Octagon";
break;
case 9:
poly = "Nonagon";
break;
case 10:
poly = "Decagon";
break;
case 11:
poly = "Elevengon";
break;
case 12:
poly = "Dodecagon";
break;
}
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);
}
```

There are many problems with your code. Given its quality, I assume this is homework.

```
while (sides > 3 || < 12 ){
System.out.println("Please enter a number from 3 to 12: ")
}
```

Even if you correct the condition, this will lead to an infinite loop because the conditions's value will not change once the loop is entered.

```
for (sides >= 3 || <= 12){
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);//TODO:Use a 'for loop' here!
}
```

Please read about for-loop syntax first. You can post specific questions then. But this looks like you wrote an `if`

-statement and the corrector told you to use a `for`

-loop instead. Honestly, this is not what he meant. Please try to take the time understanding how your program should work in the first place.

Since you hardcode the answers, why not just use an array with the correct answers?

```
import java.util.Scanner;
public class Lab6 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int sides = 0;
String[] polygonNames = {"invalid", "invalid", "Triangle", "Quadrilaterl", "Pentagon", "Hexagon", "Heptagon", "Octagon", "Nonagon", "Decagon", "Elevengon", "Dodecagon"};
System.out.print("Enter a number from 3 to 12: ");
sides = scan.nextInt();
while (sides < 3 || sides > 12 ){
System.out.println("Please enter a number from 3 to 12: ")
sides = scan.nextInt();
}
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, polygonNames[sides-1]);
}
}
```