2014-10-10 77 views
0

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

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.任何人都可以帮我解释一下吗?

+1

变化数和平均为浮动 – keety 2014-10-10 02:57:36

+0

当我改变计数浮动,switch语句说,我不能用价值浮动。当我将totalQuestions更改为float时,使用totalQuestions的新字符串对象表示不能从float转换为int。 – 2014-10-10 03:05:08

+0

好的,我明白了!我在avg方程中的count和totalQuestion之前加了(float)。 – 2014-10-10 03:13:28

回答

0

您需要这行代码更改:

int avg = (count/totalQuestions) * 100; 

float avg= ((float)count/(float)totalQuestions) * 100; 

编辑:
保证格式像XX.XX%,你需要使用字符串格式:

String.format("%,.2f", avg); 
+0

好吧,我这样做,它的工作,但是当我到3/5纠正它输出为60.000004%。我该如何解决这个问题?不知道如何在JOptionPane中做到这一点 – 2014-10-10 03:20:59

0

当你用Java划分两个整数时,结果是一个i nteger。为了得到一个百分比,可以投整数到花车/双打这样

int avg = (int)((((double)count)/((double)totalQuestions)) * 100.0)