2013-11-20 41 views
-2

我有這個程序工作,但我只有一個問題:它不斷循環。這是我的代碼Java - 程序保持循環,如何停止它?

import java.util.*; 
public class Main { 

public static Scanner sc = new Scanner(System.in); 

public static void main(String[] args) { 
    int type; 

do 
{ 
    System.out.println("MAIN MENU"); 
    System.out.println("========="); 
    System.out.println("1. Convert to a Letter grade"); 
    System.out.println("2. Convert to a Level grade"); 
    System.out.println("3. Exit \n"); 
    System.out.println("Please enter your selection: "); 
    type=(1);             // Put type in here ******************************************************************** 

    if (type==1) percentageToAlpha(); 
    if (type==2) percentageToLevel(); 
} 
while (type != 3); 
} 
public static void percentageToAlpha() 
{ 
int grade; 
String alphaGrade; 
String messageOut; 

System.out.println("Please enter the numeric grade: "); 
grade=(78);              // Put grade in here ********************************************************************* 

if (grade < 50) { 
    alphaGrade="F"; 
}else if ((grade >= 50) && (grade <=52)) 
{ 
    alphaGrade = "D-"; 
}else if ((grade >= 53) && (grade <=56)) 
{ 
    alphaGrade = "D"; 
}else if ((grade >= 57) && (grade <=59)) 
{ 
    alphaGrade = "D+"; 
}else if ((grade >= 60) && (grade <=62)) 
{ 
    alphaGrade = "C-"; 
}else if ((grade >= 63) && (grade <=66)) 
{ 
    alphaGrade = "C"; 
}else if ((grade >= 67) && (grade <=69)) 
{ 
    alphaGrade = "C+"; 
}else if ((grade >= 70) && (grade <=72)) 
{ 
    alphaGrade = "B-"; 
}else if ((grade >= 73) && (grade <=76)) 
{ 
    alphaGrade = "B"; 
}else if ((grade >= 77) && (grade <=79)) 
{ 
    alphaGrade = "B+"; 
}else if ((grade >= 80) && (grade <=84)) 
{ 
    alphaGrade = "A-"; 
}else if ((grade >= 85) && (grade <=89)) 
{ 
    alphaGrade = "A"; 
}else if ((grade >= 90) && (grade <=100)) 
{ 
    alphaGrade = "A+"; 
}else 
    { 
      alphaGrade = "Error - invalid mark, try again!"; 
} 

messageOut=(grade + "% = " + alphaGrade); 
System.out.println(messageOut); 
} 

// Conversion function percentage to level 
public static void percentageToLevel() 

{ 
int grade; 
String levelGrade; 
String messageOut; 

System.out.println("Please enter the numeric grade: "); 
grade=sc.nextInt(78);              // Put grade in here ********************************************************************* 

if (grade < 50) { 
    levelGrade="R"; 
}else if ((grade >= 50) && (grade <=52)) 
{ 
    levelGrade="L1-"; 
}else if ((grade >= 53) && (grade <=56)) 
{ 
    levelGrade="L1"; 
}else if ((grade >= 57) && (grade <=59)) 
{ 
    levelGrade="L1+"; 
}else if ((grade >= 60) && (grade <=62)) 
{ 
    levelGrade="L2-"; 
}else if ((grade >= 63) && (grade <=66)) 
{ 
    levelGrade="L2"; 
}else if ((grade >= 67) && (grade <=69)) 
{ 
    levelGrade="L2+"; 
}else if ((grade >= 70) && (grade <=72)) 
{ 
    levelGrade="L3-"; 
}else if ((grade >= 73) && (grade <=76)) 
{ 
    levelGrade="L3"; 
}else if ((grade >= 77) && (grade <=79)) 
{ 
    levelGrade="L3+"; 
}else if ((grade >= 80) && (grade <=84)) 
{ 
    levelGrade="L4-"; 
}else if ((grade >= 85) && (grade <=89)) 
{ 
    levelGrade="L4"; 
}else if ((grade >= 90) && (grade <=100)) 
{ 
    levelGrade="L4+"; 
}else{ 
    levelGrade="Error - invalid mark, try again!"; 
} 

messageOut=(grade + "% = " + levelGrade); 
System.out.println(messageOut); 
} 

} 

正如你所看到的,它可以在輸出中反覆循環。我需要它只做一次,然後停止。任何幫助新的程序員?

+0

YOu永遠不會引用掃描儀,sc。那麼你的類型價值如何改變? – OldProgrammer

+0

你從來沒有實現那些評論的地方,說「把......放在這裏」等等。 – forgivenson

回答

0

在你的while循環中,你的type變量總是設置爲1.而你的while條件是while (type != 3)。由於type始終爲1,percentageToAlpha被調用,並且它永不改變type,所以它仍然是1.由於type總是不等於3,因此會發生無限循環。

如果您想獲得用戶輸入,請使用掃描儀。例如,在你的代碼中,這應該是:

Scanner s = new Scanner(System.in); // use scanner to scan for user input 
do 
{ 
    System.out.println("MAIN MENU"); 
    System.out.println("========="); 
    System.out.println("1. Convert to a Letter grade"); 
    System.out.println("2. Convert to a Level grade"); 
    System.out.println("3. Exit \n"); 
    System.out.println("Please enter your selection: "); 
    type= s.nextInt(); // get what the user enters       

    if (type==1) percentageToAlpha(); 
    if (type==2) percentageToLevel(); 

} 
while (type != 3); 
+0

那麼我是否會改變這種情況?如果是s,我應該怎樣改變它?我想要1個輸入來執行percentageToAlpha,2個要做percentageToLevel – user2984158

+0

如果你想得到用戶輸入,使用'Scanner'。掃描儀掃描用戶輸入,並讓你用它做事。 – slider

+0

這是我的編程類,我需要調試這個程序。它需要接受一個輸入,並沒有說在哪裏,並且執行必要的行動 – user2984158

2

你不應該像你那樣對類型變量進行硬編碼。通常在一個while循環中,或者在這種情況下do while循環中,你將要增加一些值。在此基礎上增加你會想測試例如

int i = 0; 
while(i < 5) { 
System.out.println("This is such a good answer"); 
i++; //central idea in the while loop 
} 
1

您不要在運行時改變type值。所以它保持循環。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    do 
    { 
    System.out.println("MAIN MENU"); 
    System.out.println("========="); 
    System.out.println("1. Convert to a Letter grade"); 
    System.out.println("2. Convert to a Level grade"); 
    System.out.println("3. Exit \n"); 
    System.out.println("Please enter your selection: "); 
    type= Integer.parseInt(br.readLine()); 

    if (type==1) percentageToAlpha(); 
    if (type==2) percentageToLevel(); 
} 
while (type != 3); 

並保留在您的函數中。

public static void main(String[] args) throws Exception