java - 当达到最大尝试次数时如何停止以及如果答案错误则留在同一问题中

标签 java

我正在尝试做一个测验,如果答案正确,数字就会增加,如果答案不正确,则每个问题将有 4 次尝试 2 x 1 = ?

如果您回答正确,您必须转到2 x 2 = ? 以此类推,直到达到 10 分。如果您回答正确,每个问题将获得 5 分。

public static void main(String[] args) {

  Scanner input = new Scanner(System.in);

  int points = 0;
  int multiply;


  System.out.println("please enter a number");
   int yourNumber = input.nextInt();

   for (multiply = 0; multiply<= 10; multiply++){
       int yourAnswer = yourNumber * multiply;

      System.out.println(yourNumber + " x " + multiply + " = ? ");

   int theAnswer = input.nextInt();

   for (int tries = 0; tries >= 4; tries++){
    if (theAnswer == yourAnswer){
        System.out.println("nice");
        tries++;

最佳答案

你的代码似乎不完整,for循环部分应该是<=,因为你想在treis小于或等于4时保持循环 在您的情况下 >= 无法进入循环,因为 attempts = 0 并且 attempts 不大于 4 我还编辑为 attempts = 0, <=5,因为如果您想要 5 次尝试而不是数字,您可以直接更改

for (int tries = 1; tries <= 5; tries++){
if (theAnswer == yourAnswer){
    System.out.println("nice");
    points = points + 5;
    break;
    }
 else{
    System.out.println("Your answer : " + theAnswer  + " is wrong, please try again. Attempts : " + tries);
    theAnswer = input.nextInt();
    }
}

关于java - 当达到最大尝试次数时如何停止以及如果答案错误则留在同一问题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58618563/

相关文章:

java - 如何在java中将对象的ArrayList转换为整数列表

java - 找不到库 : libGLESv2. dll

java - 如何在没有 App 服务器和 Web 服务器的情况下从 UI 调用 shell 脚本?

java - 如何使用 wsdl2java 创建的 java 客户端获取 Soap 输出数据

java - 如何用JDBC执行一组相关的SQL指令?

Java 相当于 perl unpack 函数

Java缓存设计问题

java - Android - 如何获取文件扩展名?

java - 在java中更新具有键值形式的值的文件

java - 将变量从一个 jsp 发送到另一个 jsp