我有這個程序工作,但我只有一個問題:它不斷循環。這是我的代碼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);
}
}
正如你所看到的,它可以在輸出中反覆循環。我需要它只做一次,然後停止。任何幫助新的程序員?
YOu永遠不會引用掃描儀,sc。那麼你的類型價值如何改變? – OldProgrammer
你從來沒有實現那些評論的地方,說「把......放在這裏」等等。 – forgivenson