-5

ユーザー入力の「sides」変数に基づいてステートメントを複数回出力する for ループを作成するのに助けが必要です。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!
        }


}
4

6 に答える 6

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.
于 2013-05-02T13:59:39.780 に答える
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代わりに - ループを使用するように言いました。正直なところ、これは彼が意図したものではありません。最初に、プログラムがどのように動作するかを理解するために時間をかけてください。

于 2013-05-02T14:03:41.223 に答える
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);
}
于 2013-05-02T14:02:32.757 に答える
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]);
    }
}
于 2013-05-02T14:07:08.270 に答える
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.

これは宿題のように見えますが...ここで答えを求められたくありません...

于 2013-05-02T14:07:18.510 に答える
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 ループを使用する必要はありません。

于 2013-05-02T13:56:00.560 に答える