java - 必须使用此布局,如何打印成绩?
问题描述
爪哇。必须使用这种布局,我怎样才能得到打印的成绩。我的教授希望我们使用这三种不同的方法,我不知道如何打印。我可以让程序提示输入数字分数,但它会停止
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);
}
}
解决方案
代码停止,因为您在阅读分数后没有做任何事情。
代码中的另一个问题是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;
}
}
推荐阅读
- entity-framework - 具有自身外键的 Net Core ApplicationUser
- python - 如何改变熊猫的周期
- sql - 为什么我的 T-SQL 查询在不同的窗口中返回不同?
- javascript - 更改数据表单元格背景功能
- java - Java:Autowired.Value 在哪里寻找设置?
- c# - 当我有很多异步操作(以前的存储过程)时,使用 dbContext 的最佳方法是什么?
- visual-studio - WinForms“设置”设计器中的同步按钮正在错误的“Url”位置查找 user.config
- python - csv 文件在 pandas csv 中错误加载
- python - 越界纳秒时间戳:1-01-01 00:00:00
- vba - 使用 VBA 为整个演示文稿设置相同的字体类型