-2

I am trying to write a program that will loop when the user inputs a "y" or "yes" it.

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

Please help every time I type in y or yes I keep getting errors

4

2 回答 2

1

在使用while循环时,您应该首先在声明中声明它的初始值。

例子:

String choice ="y"

//and so on...

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

//your stuffs here

}

附言

你的数据类型是"y"什么?我假设你声明它是因为Integer你使用了一个"=="你不能"double-equals"String. 您应该将其更改为String".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。行上的 ywhile (choice == y)需要使用单引号,或者可能是双引号,具体取决于choice.

  2. choice第一次通过while循环是什么?它在哪里声明?它的类型是什么?

  3. 的返回类型是nextInt()什么?提示:int :-) 这将影响您是否将 y 括在单引号或双引号中。查看 Java 中的 char 与 String,以及它们的比较方式......

于 2013-10-21T00:10:58.293 回答