- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我正在为类制作的一个程序。在编写了大部分程序后,我现在尝试运行它,似乎有一个逻辑错误。由于某种原因,计算机只会执行到第一个 for 循环,然后忽略 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);
}
最佳答案
您正在使用 0 大小初始化数组(请注意,您正在使用 0 初始化 numQuestions
)。只需在从用户输入中获取 numQuestions
值后移动数组初始化即可:
numQuestions = scan.nextInt();
int[] key = new int[numQuestions];
<小时/>
如果您想使用 int
/long
将 double
值分配给 double
变量,则应该进行强制转换将 int
/long
转换为 double
或乘以 1.0
(文字),否则浮点结果将为int
/long
。修改这部分代码:
double percentCorrect = ((numCorrect * 1.0) / ((key.length + 1) * 1.0) );
如果你想打印格式化文本来输出,你应该使用System.out.printf
。我将编写最终代码的示例(关于语言语法):
//System.out.println("The percent correct: %" + percentCorrect);
System.out.printf("The percent correct: %% %.2f\n", percentCorrect);
来自上面的链接:
%%
将打印 %
符号%.2f
将使用 2 位固定小数打印 float \n
将打印换行符关于java - For 循环未正确执行 scan.nextInt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15238084/
我正在编写一个 Bukkit 插件,它将雪球视为彩弹。当我不小心使用时: (byte) (random.nextInt() % 16) 而不是 (byte) (random.nextInt(16))
我是编程初学者(学习Java)。我正在尝试编写一个程序,其中列出了四个不同的选项供用户选择。 这是其中的一部分: import java.util.*; public class fight
我正在使用以下代码来学习java套接字编程。它的作用是,client.java 程序从用户那里获取一个号码并将其发送到 sever.java。然后服务器将其乘以2并发回给客户端。在我的客户端程序中,它
这个问题已经有答案了: How to use java.util.Scanner to correctly read user input from System.in and act on it?
我正在使用 random 类生成 1 到 5 之间的随机数,如 myrandom.nextInt(6),它工作正常,但我想知道是否有一种方法可以给特定数字赋予权重以增加它出现的概率,假设我希望数字“4
我正在编写一个程序,该程序使用 nextInt(); 在程序中接受两个整数,并将其包装在 try catch block 中以阻止错误输入,例如 double 或字符。 当输入多个错误输入时,循环会重
我不是 Java 专家,但我有一个问题。我使用 Scanner 从键盘获取数据,但在将数据注册到数组之前我必须进行 2 次验证。一是数据不必是字符串,我使用 !in.hastNextInt() 来验证
每当我尝试执行它时,它都会在我的应用程序上给我 FC int a, b; Random s = new Random(); if (diffi.contains("easy") && name
我正在尝试从以下格式的文本文件中解析信息: WarningGeotask: 0, 1 第一个词是某个对象的关键字,要在其后的数字中给出的坐标位置创建。这是我的循环目前的样子: // Open file
我通过输入 Scanner class 请求电话号码,我只强制输入号码,但我可能会出错。我需要的是一串整数,而不是一个整数并且比一个整数的大小容量更大的东西。最终,它的长度需要是 7 个整数或 10
这个问题在这里已经有了答案: How to use java.util.Scanner to correctly read user input from System.in and act on
我需要读入一个整数,如果它不是整数则显示一条消息,我需要循环直到只输入一个 int。我在下面的代码中遇到的问题是当它循环时它不会等待读取 nextInt,它只会继续循环 - 打印出重试消息。
我有一个使用 SecureRandom 实例并获取下一个随机数的类。 让我们说这个例子是: public class ExampleClass() { public void method()
有谁知道Java的Random.nextInt()一段时间后会重演吗? 具体有没有这样的号码n使得以下两个列表相等? List a = new LinkedList<>(); List b = new
在下面的 Java 代码中 Scanner input = new Scanner(System.in); //Cmd1 int i1,i2;
是ThreadLocalRandom's nextInt() method (即没有任何参数的 nextInt() 方法)实际上生成一个没有范围的伪随机整数(即在 Integer.MIN_VALUE
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 7年前关闭。 Imp
这是我正在为类制作的一个程序。在编写了大部分程序后,我现在尝试运行它,似乎有一个逻辑错误。由于某种原因,计算机只会执行到第一个 for 循环,然后忽略 scan.nextInt 方法,在这些方法中我尝
每次我尝试执行包含 .nextInt() 的代码行时,都会收到此错误:“无法对非静态字段进行静态引用” . 以下是可能影响的代码行(我能想到的): private Scanner input = ne
我问的问题不太清楚,但基本上我的任务是: Write a program to read in 5 integer numbers from the user. Youshould store the
我是一名优秀的程序员,十分优秀!