如何循环回我的方法?(Java)

时间:2016-11-08 00:45:32

标签: java loops

好的,我想循环回来

我想使用字母'y'和'n'使用用户'y'的输入循环回来'n'来停止程序。我是否可以通过使用带有布尔值的while循环来实现此目的? (我很抱歉我是菜鸟。)

public static void main(String[]args)
{
    int gn; //gradenumber input
    Scanner reader = new Scanner(System.in);
    System.out.println("Type in a whole number from 0-100 for a grade output: ");
    gn = reader.nextInt();


    if (gn >= 96 && gn <=100)
    {
        System.out.println("A+");
    }
    else if (gn >= 92 && gn <= 96)
    {
        System.out.println("A");
    }
    else if (gn >= 90 && gn <= 91)
    {
        System.out.println("A-");
    }

    else if (gn >= 86 && gn <= 89)
    {
        System.out.println("B+");
    }

    else if (gn >= 82 && gn <= 85)
    {
        System.out.println("B");
    }

    else if (gn >= 80 && gn <= 81)
    {
        System.out.println("B-");
    }
    else if (gn >= 76 && gn <= 79)
    {
        System.out.println("C+");
    }
    else if (gn >= 72 && gn <= 75)
    {
        System.out.println("C");
    }
    else if (gn >= 70 && gn <= 71)
    {
        System.out.println("C-");
    }
    else if (gn >= 66 && gn <= 69)
    {
        System.out.println("D+");
    }
    else if (gn >= 62 && gn <= 65)
    {
        System.out.println("D");
    }
    else if (gn >= 60 && gn <= 61)
    {
        System.out.println("D-");
    }
    else if (gn >= 0 && gn <= 59)
    {
        System.out.println("F");
    }
    else if (gn > 100)
    {
        System.out.println("Error");
    }
    else if (gn < 0)
    {
        System.out.println("Error");
    }

1 个答案:

答案 0 :(得分:2)

创建一个char变量,让用户选择输入(或不输入)更多成绩(y / n),并将成绩输入和标记输出放在do/while循环中。

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        int gn; // gradenumber input
        char choice; // choice input (y/n)

        Scanner reader = new Scanner(System.in);

        do
        {
            System.out.println("Type in a whole number from 0-100 for a grade output: ");
            gn = reader.nextInt();

            if (gn >= 96 && gn <= 100) System.out.println("A+");
            else if (gn >= 92 && gn <= 96) System.out.println("A");
            else if (gn >= 90 && gn <= 91) System.out.println("A-");
            else if (gn >= 86 && gn <= 89) System.out.println("B+");    
            else if (gn >= 82 && gn <= 85) System.out.println("B");    
            else if (gn >= 80 && gn <= 81) System.out.println("B-");
            else if (gn >= 76 && gn <= 79) System.out.println("C+");
            else if (gn >= 72 && gn <= 75) System.out.println("C");
            else if (gn >= 70 && gn <= 71) System.out.println("C-");
            else if (gn >= 66 && gn <= 69) System.out.println("D+");
            else if (gn >= 62 && gn <= 65) System.out.println("D");
            else if (gn >= 60 && gn <= 61) System.out.println("D-");
            else if (gn >= 0 && gn <= 59) System.out.println("F");
            else if (gn < 0 || gn > 100)  System.out.println("Error");

            System.out.println("Do you want to input more grades (y/n)?");
            choice = reader.next().charAt(0);

        } while (choice != 'n'); // repeat until user enters 'n'

        reader.close();
    }
}

此外,请不要忘记close()您的Scanner对象,以避免Resource Leak警告。