gpt4 book ai didi

java - 使用 while 循环验证输入

转载 作者:行者123 更新时间:2023-12-01 17:20:36 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的程序,根据使用 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;

}
}

最佳答案

第一个问题:

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));

关于java - 使用 while 循环验证输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com