[JAVA]输入三门课程成绩,并计算平均数; 用函数判断:如果平均数>=60,返回合格,否则返回不合格
/*输入三门课程成绩,并计算平均数; 用函数判断:如果平均数>=60,合格,否则不合格; */ import java.util.Scanner; public class ReturnString { public static void main(String[] args) { Scanner userInput = new Scanner(System.in); double math, english, science; System.out.println("input three scores(math, english, science respectively), using space to separate each score: "); math = userInput.nextDouble(); english = userInput.nextDouble(); science = userInput.nextDouble(); double average = (math + english + science) / 3; System.out.println("average score is " + Math.ceil(average)); System.out.println("status: " + decide(average)); } static String decide(double average) { String result; //保证return值是单一出口 if (average < 60) result = "fail"; else result = "pass"; return result; } }
注意:java中,函数可以返回String类