用枚举法编程

来源:百度知道 编辑:UC知道 时间:2024/05/23 15:00:38
四名专家对四款车进行评论。
A说:2号赛车是最好的。
B说:4号赛车是最好的。
C说:3号不是最佳赛车。
D说:B说错了。
事实上只有一款赛车最佳,且只有一名专家说对了,其他三人都说错了。请编程输出最佳车的车号,以及哪位专家说对了。
用C语言编程

3号最佳,D说对了,java控制台程序如下:
public static void main(String[] args) {
Comment commentA = new Comment(2, Comment.State.BEST);
Comment commentB = new Comment(4, Comment.State.BEST);
Comment commentC = new Comment(3, Comment.State.NOT_BEST);
Comment commentD = commentB.negate();

Comment[] comments = {commentA, commentB, commentC, commentD};

int correctCommentNumber = 0, correctCommentCount;
for (int i = 1; i <= 4; ++i) {
correctCommentCount = 0;
Comment theFact = new Comment(i, Comment.State.BEST);
for (Comment comment : comments) {
if (theFact.isCorrect(comment)) {
correctCommentCount++;
correctCommentNumber = i;
}
}
if (correctCommentCount == 1) {
System.out.println("Car number " + i + " is the best.");
System.out.println("Expert number " + correctCommentNumber
+ " is correct.&qu