我正在尝试做一个简单的程序,根据使用while
循环和do-while
循环输入的分数来验证和分配学生的成绩。我理解这很简单,代码在没有验证的情况下工作正常,但循环不循环,程序终止。这是我到目前为止所得到的
import io.*;
public class Marks {
public static void main(String[] args) {
double marks, inMarks;
int assess, inAssess, calcMark, ten;
boolean dna, dnc, fail, pass, calcDna, calcDnc, calcF, calcP;
marks = inMarks();
assess = inAssess();
dna = calcDna(assess);
dnc = calcDnc(assess);
fail = calcF(assess, marks);
pass = calcP(assess, marks);
ten = calcMark(marks);
if (dna == false) {
if (dnc == true) {
System.out.println("DNC-" + (int) marks);
} else if (fail == true) {
System.out.println("F-" + (int) marks);
} else if (pass == true) {
System.out.println(ten + "-" + (int) marks);
}
} else {
System.out.println("DNA");
}
}
private static double inMarks() {
double marks;
boolean validMark;
marks = ConsoleInput.readDouble("Input student's mark");
while ((marks < 0) && (marks > 100)) {
System.out.println("invalid mark, please enter again");
marks = ConsoleInput.readDouble("Input student's mark");
}
// Assertion 0 > marks > 100
return marks;
}
private static int inAssess() {
int assess;
boolean validAssess;
do {
assess = ConsoleInput
.readInt("Input number of assessments student completed");
if ((0 > assess) && (assess > 5)) {
System.out
.println("invalid assessment attendence. please enter again");
}
} while ((0 >= assess) && (assess >= 5));
// Assertion 0 < assess < 5
return assess;
}
private static boolean calcDna(int assess) {
boolean calcDna;
calcDna = false;
if ((assess == 0)) {
calcDna = true;
}
return calcDna;
}
private static boolean calcDnc(int assess) {
boolean calcDnc;
calcDnc = false;
if ((assess > 0) && (assess <= 4)) {
calcDnc = true;
}
return calcDnc;
}
private static boolean calcF(int assess, double marks) {
boolean calcF;
calcF = false;
if ((assess == 5) && (marks < 50.0)) {
calcF = true;
}
return calcF;
}
private static boolean calcP(int assess, double marks) {
boolean calcP;
calcP = false;
if ((assess == 5) && (marks > 50.0)) {
calcP = true;
}
return calcP;
}
private static int calcMark(double marks) {
int ten;
ten = (int) marks / 10;
return ten;
}
}
答案 0 :(得分:2)
第一个问题:
while( (marks < 0) && (marks > 100))
必须是或:
while( (marks < 0) || (marks > 100))
没有数字小于0且大于100:)
do {
assess = ConsoleInput.readInt ("Input number of assessments student completed");
if((0 <= assess ) || (assess >= 5))
{
System.out.println("invalid assessment attendence. please enter again");
}
} while ((0 <= assess) || ( assess >= 5));
答案 1 :(得分:2)
如果“循环不循环”,你指的是:
while( (marks < 0) && (marks > 100))
那么你的问题是marks
不可能都小于零且大于100。
这里有类似的问题:
} while ((0 >= assess) && ( assess >= 5));
assess
不能小于零且大于/等于5.
答案 2 :(得分:1)
已更改
while( (marks < 0) && (marks > 100))
;
while ((0 >= assess) && ( assess >= 5));
到
while( (marks < 0) || (marks > 100))
while ((0 > assess) && ( assess > 5));