我需要帮助编写一个for循环,它会根据用户输入的“sides”变量多次打印一个语句。我的while循环似乎也有问题,如果我的语法关闭,请告诉我。
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!
}
}
答案 0 :(得分:0)
while (sides > 3 || < 12 )
应该是
while (sides < 3 || sides > 12 ){
System.out.println("Please enter a number from 3 to 12: ");
sides = scan.nextInt();
}
这个块
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!
}
应该是:
System.out.printf("\nA polygon with"+ sides + "sides is called" + poly);
不需要使用for循环来打印上述语句。
答案 1 :(得分:0)
使用此
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!
}
}
输出
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.
答案 2 :(得分:0)
使用开关而不是多个条件。在while循环中,您还需要阅读新输入:
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);
}
答案 3 :(得分:0)
您的代码存在许多问题。考虑到它的质量,我认为这是作业。
while (sides > 3 || < 12 ){
System.out.println("Please enter a number from 3 to 12: ")
}
即使你纠正了这个条件,这也会导致无限循环,因为一旦输入循环,条件的值就不会改变。
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!
}
请先阅读有关for循环语法的内容。您可以发布具体问题。但是这看起来像你写了一个if
- 语句,校正器告诉你使用for
- 循环代替。老实说,这不是他的意思。请尽量花时间了解您的计划应该如何运作。
答案 4 :(得分:0)
由于您对答案进行了硬编码,为什么不使用具有正确答案的数组呢?
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]);
}
}
答案 5 :(得分:0)
首先你需要改变
while (sides > 3 || < 12 ){
System.out.println("Please enter a number from 3 to 12: ")
}
到
while (sides < 3 || sides > 12) {
System.out.println("Please enter a number from 3 to 12: ");
sides = scan.nextInt();
}
然后改变这个
for (sides >= 3 || <= 12){
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);
到
for (int i = 0; i < sides; i++)
System.out.printf("\nA polygon with %d sides is called a(n) %s.", sides, poly);
这将打印声明,有副本的次数。
输出:
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.
这看起来像家庭作业......你不想被抓到在这里寻求答案......