2013-10-21 129 views
-2

我想写一个程序,当用户输入“y”或“是”它会循环。虽然循环在Java

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我不断收到错误

+0

没有足够的信息来帮助:什么是'choice'?什么是'y'? - 看起来像“选择”是一个整数,但你期望它是一个字符? – John3136

+0

同样是一个studentID是一个奇怪的 –

+2

请给我们的错误。可能更多的代码会很好。 – sdanzig

回答

1

有很多错误的时间在这里...

  1. 什么的Y?您已经编写了代码,好像有一个变量 y。根据choice的类型,while (choice == y)行中的y需要使用单引号或双引号。

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

  3. 什么是nextInt()的退货类型?提示:int :-)这会影响你是否用单引号或双引号包装y。看着成char与字符串在Java中,以及它们是如何比较?

1

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

实施例:

String choice ="y"

//and so on...

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

//your stuffs here

}

PS

"y"是什么数据类型?我假设你宣布它为Integer cus你使用了一个"=="签署你不能使用"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 {

​​

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

+0

像@ Nameka之类的说。 – errkuh