-2

ユーザーが「y」または「yes」を入力するとループするプログラムを作成しようとしています。

System.out.println("Tuition Wasted Based on Studnet Absences and its effacton GPA");

System.out.println("Enter the number of students to consider: ");

students = keyboard.nextInt(); 

while (choice == y)
{

if (students >= 1 && students <= 5)

{
for(int i = 0; i < students; i++)
{
    System.out.print("\nEnter the student ID for student: ");
    studentID = keyboard.nextDouble();

    System.out.println("Do you want to run the program again?");

    choice = keyboard.nextInt();
}

y または yes と入力するたびに助けてください。エラーが発生し続けます

4

2 に答える 2

1

ループを使用するwhile場合、最初に宣言でその初期値を宣言する必要があります。

例:

String choice ="y"

//and so on...

while (choice.equals("y")) {

//your stuffs here

}

PS

のデータ型は何"y"ですか? 私はあなたが記号を使用できない記号をInteger使用したとしてそれを宣言したと仮定しました。に変更して、代わりに使用する必要があります。"==""double-equals"StringString".equals"

do-whileループの使用を検討してみてください

元:

//your declarations..and so on

String choice;

System.out.println("Tuition Wasted Based on Studnet Absences and its effacton GPA");

System.out.println("Enter the number of students to consider: ");

students = keyboard.nextInt();

do {

if (students >= 1 && students <= 5)

{ for(int i = 0; i < students; i++) {

System.out.print("\nEnter the student ID for student: ");
studentID = keyboard.nextDouble();`

System.out.println("Do you want to run the program again?");`

choice = keyboard.next();

}while (choice.equals("y") || choice.equals("yes"));

于 2013-10-21T00:28:25.653 に答える
1

ここには多くの間違いがあります...

  1. なに?変数yがあるかのようにコードを記述しました。行の y はwhile (choice == y)、のタイプに応じて、一重引用符または二重引用符で囲む必要がありchoiceます。

  2. choicewhile ループの最初のパスは何ですか? どこで宣言されていますか?そのタイプは何ですか?

  3. の戻り値の型はnextInt()何ですか? ヒント: int :-) これは、その y を一重引用符または二重引用符で囲むかどうかに影響します。Java の char と String を調べて、それらがどのように比較されているか...

于 2013-10-21T00:10:58.293 に答える