2013-05-02 81 views
-5

我需要帮助编写一个for循环,它将根据用户输入的“sides”变量多次打印语句。我的while循环似乎也有问题,请让我知道我的语法是否关闭。我需要使用for循环多次执行println()

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! 
     } 


} 
+3

*“我需要帮助” *什么帮助,特别?请注意,SO不是“修复/完成我的代码”服务。除非你问一个具体的问题(最好只是***一个问题),否则这可能会被关闭。 – 2013-05-02 13:54:12

+0

我很抱歉,正如我下面所说的,我是新来的SO,并且认为只要求完整的答案就会被忽视。 – amwindso 2013-05-02 14:21:59

回答

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. 

这看起来像功课虽然...你不想被抓住,询问有关答案在这里...

+0

感谢您的帮助,我为打破规则而道歉,我是新来的,并认为要求完整的解决方案是不被接受的。它不会再发生! – amwindso 2013-05-02 14:13:14

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); 

ñ o需要使用for循环来打印上面的语句。

+1

不,它应该是while(双方<3 ||方面> 12) – filipko 2013-05-02 13:57:33

+1

@filipko我没有看到你的评论和答案中的代码之间的区别。 – 2013-05-02 14:00:26

+0

@Aleks G它被编辑过,之前写得不对。 – filipko 2013-05-02 14:05:32

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. 
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); 
} 
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 -loop代替。老实说,这不是他的意思。请尽量花时间了解您的程序应该如何工作。

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]); 
    } 
}