首页 > 解决方案 > 必须使用此布局,如何打印成绩?

问题描述

爪哇。必须使用这种布局,我怎样才能得到打印的成绩。我的教授希望我们使用这三种不同的方法,我不知道如何打印。我可以让程序提示输入数字分数,但它会停止

import java.util.Scanner;
public class gradeConverter
{
    public static void main (String [] args)
    {
    Scanner scan = new Scanner(System.in);
    int score = 0;
    int minimum = 0;
    int maximum = 100;
    int grade = 0;
    
    
       System.out.println("Enter letter grade 0-100: "); 
       score = scan.nextInt();
       if (score < 0)
       {
           System.exit(0);
       }
    }
    private static boolean isValidNumber(int number, int minimum, int maximum)
{
    boolean isValidNumber = false;

    //Do something with number or test it for a condition
    if( number > 0 )
        isValidNumber = false;
    if ( minimum > 0 )
        isValidNumber = true;
    if (maximum < 100);
        isValidNumber = false;
   return isValidNumber;
}
    private static String getLetterGrade (int score)
    {
        if(score >=80 && score <90)
               System.out.println("B");//java code tells you that your letter grade is a B if you input a score that is between and includes 90 and 80
            if(score <= 100 && score >= 90)
               System.out.println("A");//java code tells you that your letter grade is a A if you input a score that is between and includes 100 and 90
            if(60>score)
               System.out.println("F");
             return getLetterGrade(0);
    }
    
}

标签: javamethods

解决方案


代码停止,因为您在阅读分数后没有做任何事情。

代码中的另一个问题是getLetterGrade方法是递归调用。

请使用以下附加代码:

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

            Scanner scan = new Scanner(System.in);
            int score = 0;
            int minimum = 0;
            int maximum = 100;
            int grade = 0;

            System.out.println("Enter letter grade 0-100: ");
            score = scan.nextInt();
            if (!isValidNumber(score,1,100))
            {
                System.exit(0);
            }
        getLetterGrade(score);
        }
        private static boolean isValidNumber(int number, int minimum, int maximum)
        {
            return number > minimum && number<=maximum;
        }
        private static String getLetterGrade(int score)
        {
            String grade=null;
            if(score >=80 && score <90) {
                System.out.println("B");//java code tells you that your letter grade is a B if you input a score that is between and includes 90 and 80
                grade = "B";
            }
            else  if(score <= 100 && score >= 90) {
                System.out.println("A");//java code tells you that your letter grade is a A if you input a score that is between and includes 100 and 90
                grade = "A";
            }else if(60>score){
                System.out.println("F");
                grade = "F";
            }
            return grade;
        }

}

推荐阅读