#java #loops #do-while
Вопрос:
У меня проблема с тем, что он зацикливает один и тот же вывод, даже если я ввел правильный ввод, чтобы завершить цикл. Это результат, который постоянно повторялся Invalid input. Please enter again! Would you like to enter another student's mark? [Y/N]
. Я просмотрел код на предмет возможной ошибки, но не смог понять, где код пошел не так, чтобы произошла ошибка. Ниже приведен полный код.
import java.util.Scanner;
public class tutorial {
@SuppressWarnings("empty-statement")
public static void main (String args[]){
Scanner input = new Scanner(System.in);
String id, inpTutorial, inpAssignment, inpTest, inpProject, inpFinalExam, reenter = "", enter = "", reenterAgain = "", enterAgain = "";
int A = 80, B = 65, C = 50, D = 40, F = 0, bPlus = B 5, cPlus = C 5;
Double tutorial = 0.0, assignment = 0.0, test = 0.0, project = 0.0, finalExam = 0.0, tutorialMarks = 0.0, assignmentMarks =0.0, testMarks = 0.0, projectMarks = 0.0, finalExamMarks = 0.0;
double totalMarks = 0.0;
do {
do {
System.out.println("Please enter ID");
id = input.next();
if(id.isEmpty()) {
System.out.println("ID should not be empty!n");
}
else {
do {
System.out.println("Please enter tutorial marks");
inpTutorial = input.next();
tutorial = Double.parseDouble(inpTutorial);
if (tutorial < 0) {
System.out.println("Tutorial marks cannot be a negative!n");
}
else if (tutorial > 100) {
System.out.println("Tutorial marks can only be 100%n");
}
else if (tutorial == null) {
System.out.println("Tutorial marks must have a value!");
}
else {
do {
System.out.println("Please enter assignment marks");
inpAssignment = input.next();
assignment = Double.parseDouble(inpAssignment);
if (assignment < 0) {
System.out.println("Assignment marks cannot be a negative!n");
}
else if (assignment > 100) {
System.out.println("Assignment marks can only be 100%n");
}
else if (assignment == null) {
System.out.println("Assignment marks must have a value!");
}
else {
do {
System.out.println("Please enter test marks");
inpTest = input.next();
test = Double.parseDouble(inpTest);
if (test < 0) {
System.out.println("Test marks cannot be a negative!n");
}
else if (test > 100) {
System.out.println("Test marks can only be 100%n");
}
else if (test == null) {
System.out.println("Test marks must have a value!");
}
else {
do {
System.out.println("Please enter project marks");
inpProject = input.next();
project = Double.parseDouble(inpProject);
if (project < 0) {
System.out.println("Project marks cannot be a negative!n");
}
else if (project > 100) {
System.out.println("Project marks can only be 100%n");
}
else if (project == null) {
System.out.println("Project marks must have a value!");
}
else {
do {
System.out.println("Please enter final examination marks");
inpFinalExam = input.next();
finalExam = Double.parseDouble(inpFinalExam);
if (finalExam < 0) {
System.out.println("Final examination marks cannot be a negative!n");
}
else if (finalExam > 100) {
System.out.println("Final examination marks can only be 100%n");
}
else if (finalExam == null) {
System.out.println("Final examination marks must have a value!");
}
else {
tutorialMarks = (tutorial/100)*10;
assignmentMarks = (assignment/100)*10;
testMarks = (test/100)*20;
projectMarks = (project/100)*20;
finalExamMarks = (finalExam/100)*40;
totalMarks = tutorialMarks assignmentMarks testMarks projectMarks finalExamMarks;
System.out.println("Your ID is: " id);
int marksTotal= (int) Math.round(totalMarks);
System.out.printf("Your total marks is %d%%n", marksTotal);
if(marksTotal < 100 amp;amp; marksTotal >= 80) {
System.out.println("Your grade is A");
}
else if (marksTotal >= 70) {
System.out.println("Your grade is B ");
}
else if (marksTotal >= 65) {
System.out.println("Your grade is B");
}
else if (marksTotal >= 55) {
System.out.println("Your grade is C ");
}
else if (marksTotal >= 50) {
System.out.println("Your grade is C");
}
else if (marksTotal >= 40) {
System.out.println("Your grade is D");
}
else if (marksTotal >= 0) {
System.out.println("Your grade is F");
}
else {
System.out.println("Invalid grade!");
}
if (marksTotal > 77) {
int gradeA = A - marksTotal;
if (gradeA == 1){
System.out.printf("Total marks: %.1f. After round up is %d. %d-%d is %d mark. Borderlinen",totalMarks,marksTotal,A,marksTotal,gradeA);
}
else if (gradeA != 1 amp;amp; gradeA != 0){
System.out.printf("Total marks: %.1f. After round up is %d. %d-%d is %d mark. Not borderlinen",totalMarks,marksTotal,A,marksTotal,gradeA);
}
else {
System.out.printf("Total marks: %.1f. After round up is %d. %d-%d is %d mark.n",totalMarks,marksTotal,A,marksTotal,gradeA);
}
}
else if (marksTotal > 67) {
int gradeBPlus = bPlus - marksTotal;
if (gradeBPlus == 1){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " bPlus "-" marksTotal " is " gradeBPlus " mark. Borderline");
}
else if (gradeBPlus != 1 amp;amp; gradeBPlus != 0){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " bPlus "-" marksTotal " is " gradeBPlus " mark. Not borderline");
}
else {
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " bPlus "-" marksTotal " is " gradeBPlus " mark.");
}
}
else if (marksTotal > 62) {
int gradeB = B - marksTotal;
if (gradeB == 1){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " B "-" marksTotal " is " gradeB " mark. Borderline");
}
else if (gradeB != 1 amp;amp; gradeB != 0){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " B "-" marksTotal " is " gradeB " mark. Not borderline");
}
else {
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " B "-" marksTotal " is " gradeB " mark.");
}
}
else if (marksTotal > 52) {
int gradeCPlus = cPlus - marksTotal;
if (gradeCPlus == 1){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " cPlus "-" marksTotal " is " gradeCPlus " mark. Borderline");
}
else if (gradeCPlus != 1 amp;amp; gradeCPlus != 0){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " cPlus "-" marksTotal " is " gradeCPlus " mark. Not borderline");
}
else {
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " cPlus "-" marksTotal " is " gradeCPlus " mark.");
}
}
else if (marksTotal > 47) {
int gradeC = C - marksTotal;
if (gradeC == 1){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " C "-" marksTotal " is " gradeC " mark. Borderline");
}
else if (gradeC != 1 amp;amp; gradeC != 0){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " C "-" marksTotal " is " gradeC " mark. Not borderline");
}
else {
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " C "-" marksTotal " is " gradeC " mark.");
}
}
else if (marksTotal > 37) {
int gradeD = D - marksTotal;
if (gradeD == 1){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " D "-" marksTotal " is " gradeD " mark. Borderline");
}
else if (gradeD != 1 amp;amp; gradeD != 0){
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " D "-" marksTotal " is " gradeD " mark. Not borderline");
}
else {
System.out.println("Total marks: " totalMarks ". After round up is " marksTotal ". " D "-" marksTotal " is " gradeD " mark.");
}
}
else {
System.out.print("Invalid total marks!");
}
}
}while(finalExam > 100 || finalExam < 0);
}
}while(project > 100 || project < 0);
}
}while(test > 100 || test < 0);
}
}while(assignment > 100 || assignment < 0);
}
}while(tutorial > 100 || tutorial < 0);
}
}while(id.isEmpty());
System.out.println("Would you like to enter another student's mark? [Y/N]");
reenter = input.next();
enter = reenter.toLowerCase();
while(!"Y".equals(enter) || !"N".equals(enter)) {
System.out.println("Invalid input. Please enter again!nWould you like to enter another student's mark? [Y/N]");
reenterAgain = input.next();
enterAgain = reenterAgain.toLowerCase();
}
}while(enterAgain.equalsIgnoreCase("Y"));
}//end main
}//end class
Вот та конкретная часть, с которой у меня возникли проблемы.
System.out.println("Would you like to enter another student's mark? [Y/N]");
reenter = input.next();
enter = reenter.toLowerCase();
while(!"Y".equals(enter) || !"N".equals(enter)) {
System.out.println("Invalid input. Please enter again!nWould you like to enter another student's mark? [Y/N]");
reenterAgain = input.next();
enterAgain = reenterAgain.toLowerCase();
}
Комментарии:
1.
enter = reenter.toLowerCase();
ты не имеешь в видуtoUpperCase();
?2. И вы никогда не выйдете с
enterAgain = reenterAgain.toUpperCase();
…while
циклическими тестамиenter
, нетenterAgain
. (И вам нужно узнать о методах и многих других основных вещах, поскольку код чрезвычайно многословен и повторяющийся.)