c语言中void scan,对于没有正确执行scan.nextInt的循环

这是我正在为类进行的一个程序。编写大部分程序后,我现在试图运行它,似乎我有一个逻辑错误。出于某种原因,计算机只会执行直到第一个for循环,然后忽略scan.nextInt方法,我尝试提示用户将值输入到数组中。对于没有正确执行scan.nextInt的循环

编辑:新问题:现在,当我运行程序时,循环没有正确执行是第二个for循环。无论您输入什么内容,它都会返回总数正确的总数。也返回0%正确:(

编辑2:我固定的百分比正确的部分问题,但我仍然是在正确的方式去为输入了正确的答案比较输入值亏损分级测验的时候......它总是返回正确的是问题的总数,无论他们是否正确与否的数量。

import java.util.Scanner;

public class gradingQuizzes {

public static void main(String[] args)

{

int numQuestions; // number of questions on quiz

int numCorrect = 0; // number correct of entered values

int correctAnswer;

Scanner scan = new Scanner(System.in);

System.out.println("Please enter the amount of questions in the Quiz: ");

numQuestions = scan.nextInt();

int[] key = new int[numQuestions]; // creates an array for the key to the quiz

// Prompts the user to enter values into the key array

for (int i = 0; i < key.length; i++)

{

System.out.println("Please enter the correct answer to question number " + (i + 1) + " : ");

key[i] = scan.nextInt();

}

System.out.println("Please enter the answers for the quiz to be graded: ");

System.out.println("---------------------------------------------------");

// 'for' loop asking for the correct answer for each question on the quiz

for (int i = 0; i < key.length; i++)

{

System.out.println("Question " + (i + 1) + " answer: ");

correctAnswer = scan.nextInt();

if (correctAnswer == key [i]);

{

numCorrect++; // increments the number correct for each match to the key

}

}

// Creates a variable to compute the percent correct on the quiz

double percentCorrect = (numCorrect/(key.length));

System.out.println("The number correct is: " + numCorrect);

System.out.println("The percent correct: %" + percentCorrect);

}

2013-03-06

TommyD

+0

你确定你需要使用'new int [0]'(注意'numQuestions'是用0初始化的)吗? –

2013-03-06 02:52:14

+0

不客气。另外,下一次不要在标题中添加该语言,因为它已经在标签中。 –

2013-03-06 02:53:53

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值