Java - 程序保持循环,如何阻止它?

时间:2013-11-20 17:49:23

标签: java loops

我已经有了这个程序,但我只有一个问题:它不断循环。这是我的代码

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

}

正如您所看到的,它会在输出中一遍又一遍地循环。我需要它只做一次,然后停止。对新程序员的任何帮助?

3 个答案:

答案 0 :(得分:2)

你不应该按照你的方式对类型变量进行硬编码。通常在while循环内部或在这种情况下执行while循环,您将要增加一些值。基于该增量,您将要测试例如

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

答案 1 :(得分: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

答案 2 :(得分: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);