英文:
Java code will not quit when using -1 and numbers do not transfer to alphabetic grades
问题
使用 -1 退出时无效,还不能将成绩从数字或字母转换。这只是一个用于大学课程的简单成绩记录本。
public class Assignment3
{
public static void main(String[] args)
{
// 为最小和最大范围定义常量
final int minValue = -1;
final int maxValue = 100;
promptForInt(String message, int minValue, int maxValue);
}
public static void promptForInt(String message, int minValue, int maxValue)
{
System.out.println(message);
convertToLetter(double avg);
// 声明循环中初始值的变量
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
// 哨兵控制的 do-while 循环,不知道会运行多少次
do
{
System.out.printf("为第 %d 位学生输入分数"
+ "(或输入 -1 退出): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
} while (score != -1);
double avgScore = (double) sum / numStudents;
System.out.println("平均分为: " + avgScore
+ ",相当于一个 " + avg);
do
{
// 输入验证
if (score > minValue || score < maxValue)
{
System.err.printf("无效值。可接受范围为"
+ " %d 到 %d 之间\n"
+ "请重试\n", minValue, maxValue);
}
else
{
doneYet = true;
}
} while (doneYet == false);
}
public static void convertToLetter(double avg)
{
// 确定成绩等级的范围
if (avg >= 90)
{
System.out.println("A");
}
else if (avg >= 80)
{
System.out.println("B");
}
else if (avg >= 70)
{
System.out.println("C");
}
else if (avg >= 60)
{
System.out.println("D");
}
else
{
System.out.println("F");
}
}
}
英文:
When using -1 to quit it does not work and also it does not turn the grades from numerical or alphabetic. This is just a simple grade book for a college class.
public class Assignment3
{
public static void main(String[] args)
{`enter code here`
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt(String message, int minValue,int maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{
System.out.println(message);
convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don't know how many times will run
do
{
System.out.printf("Enter the score for student #%d"
+ "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
System.out.println("The average score is: " + avgScore
+ " which equates to a " + avg);
do
{
// Input Validation
if (score > minValue || score < maxValue)
{
System.err.printf("Invalid value. The acceptable range is"
+ "between %d and %d\n"
+ "Please try again\n",minValue, maxValue);
}
else
{
doneYet = true;
}
}while (doneYet == false);
}
public static void convertToLetter(double avg)
{
// Identifying the ranges for the grade letter
if (avg >= 90)
{
System.out.println("A");
}
else if (avg >= 80)
{
System.out.println("B");
}
else if (avg >= 70)
{
System.out.println("C");
}
else if (avg >= 60)
{
System.out.println("D");
}
else
{
System.out.println("F");
}
}
}
答案1
得分: 2
以下是翻译好的代码部分:
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
public class Assignment3
{
public static void main(String[] args)
{
// 定义最小和最大范围的常量
final int minValue = -1;
final int maxValue = 100;
promptForInt("", minValue, maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{
System.out.println(message);
//convertToLetter(double avg);
// 声明循环中的初始值变量
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
// 哨兵控制的 do-while 循环,不知道会运行多少次
do
{
System.out.printf("请输入第 %d 位学生的分数"
+ "(或者输入 -1 退出): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
convertToLetter(avgScore);
System.out.println("平均分数是: " + avgScore
+ ",对应等级是: " + avgScore);
do
{
// 输入验证
if (score > minValue || score < maxValue)
{
System.err.printf("无效的值。可接受范围在 %d 到 %d 之间\n"
+ "请重试\n",minValue, maxValue);
}
else
{
doneYet = true;
}
}while (doneYet == false);
}
public static void convertToLetter(double avg)
{
// 识别成绩等级的范围
if (avg >= 90)
{
System.out.println("A");
}
else if (avg >= 80)
{
System.out.println("B");
}
else if (avg >= 70)
{
System.out.println("C");
}
else if (avg >= 60)
{
System.out.println("D");
}
else
{
System.out.println("F");
}
}
}
请注意,我已经根据你的要求,只提供了代码的翻译部分,没有回答问题或添加其他内容。
英文:
You need to fix you function calls and call the convertToLetter
after the user inputs scores.
Try this code:
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
public class Assignment3
{
public static void main(String[] args)
{
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt("", minValue, maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{
System.out.println(message);
//convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don't know how many times will run
do
{
System.out.printf("Enter the score for student #%d"
+ "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
convertToLetter(avgScore);
System.out.println("The average score is: " + avgScore
+ " which equates to a " + avgScore);
do
{
// Input Validation
if (score > minValue || score < maxValue)
{
System.err.printf("Invalid value. The acceptable range is"
+ "between %d and %d\n"
+ "Please try again\n",minValue, maxValue);
}
else
{
doneYet = true;
}
}while (doneYet == false);
}
public static void convertToLetter(double avg)
{
// Identifying the ranges for the grade letter
if (avg >= 90)
{
System.out.println("A");
}
else if (avg >= 80)
{
System.out.println("B");
}
else if (avg >= 70)
{
System.out.println("C");
}
else if (avg >= 60)
{
System.out.println("D");
}
else
{
System.out.println("F");
}
}
}
答案2
得分: 0
这是一些代码逻辑错误。我已经纠正了代码。
这将会将分数转换为等级,并且不会出现无限循环。如果有任何疑问,请添加注释。
import java.util.Scanner;
public class Assignment3
{
public static void main(String[] args)
{
// 为最小和最大范围定义常量
final int minValue = -1;
final int maxValue = 100;
promptForInt("", minValue, maxValue);
}
public static void promptForInt(String message, int minValue, int maxValue)
{
System.out.println(message);
// 在循环中的初始值声明变量
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
// 哨兵控制的 do-while 循环,不知道会运行多少次
do
{
System.out.printf("Enter the score for student #%d"
+ "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
System.out.println("The average score is: " + avgScore
+ " which equates to a " + convertToLetter(avgScore));
do
{
// 输入验证
if (score < minValue || score > maxValue)
{
System.err.printf("Invalid value. The acceptable range is"
+ " between %d and %d\n"
+ "Please try again\n", minValue, maxValue);
}
else
{
doneYet = true;
}
}while (doneYet == false);
}
public static String convertToLetter(double avg)
{
// 确定等级字母的范围
if (avg >= 90)
{
return "A";
}
else if (avg >= 80)
{
return "B";
}
else if (avg >= 70)
{
return "C";
}
else if (avg >= 60)
{
return "D";
}
else
{
return "F";
}
}
}
英文:
There was some logic error on your part. I have corrected the code.
This will convert the marks into grades and not run into any infinite loops too. Comment if you have any doubts.
import java.util.Scanner;
public class Assignment3
{
public static void main(String[] args)
{
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt("", minValue, maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{
System.out.println(message);
//convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don't know how many times will run
do
{
System.out.printf("Enter the score for student #%d"
+ "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
System.out.println("The average score is: " + avgScore
+ " which equates to a " + convertToLetter(avgScore));
do
{
// Input Validation
if (score < minValue || score > maxValue)
{
System.err.printf("Invalid value. The acceptable range is"
+ "between %d and %d\n"
+ "Please try again\n",minValue, maxValue);
}
else
{
doneYet = true;
}
}while (doneYet == false);
}
public static String convertToLetter(double avg)
{
// Identifying the ranges for the grade letter
if (avg >= 90)
{
return "A";
}
else if (avg >= 80)
{
return "B";
}
else if (avg >= 70)
{
return "C";
}
else if (avg >= 60)
{
return "D";
}
else
{
return "F";
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论