2013-10-21 128 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