首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >问答程序问题

问答程序问题
EN

Stack Overflow用户
提问于 2014-10-10 02:55:02
回答 2查看 153关注 0票数 0

我对Java相当陌生,我有一项任务,我必须创建5个问题和答案,并让用户回答它们。然后,程序应该输出五个中的正确结果,以及百分比和反馈信息。我的代码如下。

代码语言:javascript
复制
package quiz;
import javax.swing.*;

public class Quiz {

    public static void main(String[] args) {

        int count = 0;
        final int totalQuestions = 5;

        JOptionPane.showMessageDialog(null, "Welcome to the Trivia Program!");
        JOptionPane.showMessageDialog(null, "A series of questions will be asked."
                + "\nPlease enter the letter of the answer"
                + "\nyou think is the correct choice.");

        String[][] question = {
            {"What letter is used to represent the number 100 in Roman Numerals?"
                + "\nA. M"
                + "\nB. D"
                + "\nC. C"
                + "\nD. X", "c"
            },
            {"Triton is the moon to which planet?"
                + "\nA. Jupiter"
                + "\nB. Neptune"
                + "\nC. Saturn"
                + "\nD. Uranus", "b"
            },
            {"What are Lanthanides?"
                + "\nA. Elements of the periodic table"
                + "\nB. Mountains on Earth"
                + "\nC. Mountains on the moon"
                + "\nD. Bacterium", "a"
            },
            {"What does a nidoligist study?"
                + "\nA. waves"
                + "\nB. clouds"
                + "\nC. bird nests"
                + "\nD. caves", "c"
            },
            {"Hypermetropic people are what?"
                + "\nA. obese"
                + "\nB. underfed"
                + "\nC. moody"
                + "\nD. far sighted", "d"
            }
        };

        String[] answers = new String[totalQuestions];

        for (int x = 0; x < totalQuestions; x++) {

            answers[x] = JOptionPane.showInputDialog("\t\t" + (x + 1) + ". " + question[x][0] + "   ");
            answers[x] = answers[x].toLowerCase();

            if (question[x][1].equals(answers[x])) {
                JOptionPane.showMessageDialog(null, "Correct!");
                count++;
            } else {
                JOptionPane.showMessageDialog(null, "Incorrect");
            }
        }

        int avg = (count / totalQuestions) * 100;

        switch (count) {
            case 3:
                JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
                        + "(" + avg + "%)"
                        + "\nAverage");
                break;
            case 4:
                JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
                        + "(" + avg + "%)"
                        + "\nVery Good");
                break;

            case 5:
                JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
                        + "(" + avg + "%)"
                        + "\nExcellent!");
                break;

            default:
                JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct "
                        + "(" + avg + "%)"
                        + "\nPoor");
                break;
        }
    }
}

我的问题是,只有当所有答案都正确时(5/5),它才能输出正确的平均%。任何其他的,百分比是0。有人能帮我解释一下吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-10 03:13:03

您需要从以下代码中更改这一行代码:

代码语言:javascript
复制
int avg = (count / totalQuestions) * 100;

代码语言:javascript
复制
float avg= ((float)count/(float)totalQuestions) * 100;

编辑:

代码语言:javascript
复制
To get the format like XX.XX%, you need to use string formatter:
代码语言:javascript
复制
String.format("%,.2f", avg);
票数 0
EN

Stack Overflow用户

发布于 2014-10-10 03:13:04

在Java中除以两个整数时,结果是一个整数。为了得到一个百分比,你可以像这样把整数转换成浮点数/双数。

代码语言:javascript
复制
int avg = (int)((((double)count) / ((double)totalQuestions)) * 100.0)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26291231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档