Java扫描程序未获取有效输入或与整数解析相关的其他问题

Java扫描程序未获取有效输入或与整数解析相关的其他问题,java,Java,请参考以下与数学表格练习相关的代码。对于除15*9以外的所有数字乘法,它工作正常,但15*9有问题 包com.test import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayLis

请参考以下与数学表格练习相关的代码。对于除15*9以外的所有数字乘法,它工作正常,但15*9有问题

包com.test

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Entry {

    static Scanner scanIn;
    static Random random;
    static List<String> list;
    static List<String> mistakeList;
    static File mistakeFile;

    static {
        scanIn = new Scanner(System.in);
        random = new Random();
        list = new ArrayList<String>();

    }

    /**
     * Main function to call the private methods.
     */
    public static void main(String[] args) throws IOException {
        mistakeFile = new File(args[0]);
        if(!mistakeFile.exists())
        {
            try {
                mistakeFile.createNewFile() ;
            } catch (IOException e) {           
                e.printStackTrace();
            }
        }
        mistakeList = getFileContent(mistakeFile);
        System.out.println("Please enter max limit .....");
        Integer maxLimit = Integer.parseInt(scanIn.nextLine());

        System.out.println("Please enter min limit .....");
        Integer minLimit = Integer.parseInt(scanIn.nextLine());

        System.out.println("Enter the number if iterations ..... ");
        Integer iterations = Integer.parseInt(scanIn.nextLine());

        executeTable(maxLimit, minLimit, iterations);
        scanIn.close();
        writeIntoFile();
    }
    /**
     * 
     * Multiplication logic which outputs two numbers, take input from user and 
     * matches it with result of multiplicaion.
     */
    private static void executeTable(Integer maxLimit, Integer minLimit,
            Integer iterations) {
        int skipCount = 0;
        for (int iTemp = 0; iTemp < iterations; iTemp++) {
            try
            {
            Integer num1 = getNextRandomInt(minLimit, maxLimit);
            Integer num2 = getNextRandomInt(2, 9);
            if (list.contains(num1 + "_X_" + num2) && skipCount < 10) {
                iTemp = iTemp - 1;
                skipCount++;
                continue;
            }
            skipCount = 0;
            System.out.println(num1 + " X " + num2 + " = ");
            Integer usrAnswer = Integer.parseInt(scanIn.nextLine());
            Integer answer = num1 * num2;
            if (usrAnswer != answer) {
                System.out.println("WRONG!! The correct answer is  " + num1
                        + " X " + num2 + " = " + answer);
                if(!mistakeList.contains(num1 + "_X_" + num2))
                {
                    mistakeList.add(num1 + "_X_" + num2);
                }
            } else {
                if(!list.contains(num1 + "_X_" + num2))
                {
                    list.add(num1 + "_X_" + num2);
                }
            }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

        }

    }

    /**
     * Gets next random number
     * 
     */
    private static Integer getNextRandomInt(Integer minLimit, Integer maxLimit) {
        Integer number = random.nextInt();
        if(maxLimit < minLimit)
        {
            maxLimit = maxLimit + minLimit ;
            minLimit = maxLimit - minLimit ;
            maxLimit = maxLimit - minLimit ;
        }

        number = (number % (maxLimit - minLimit + 1));
        if (number < 0) {
            number = -number;
        }
        number += minLimit;
        return number;
    }

    /**
     * Records commom mistake in file
     */
    private static void writeIntoFile() throws IOException 
    {
        String fileContent = "" ;
        for(String data : mistakeList)
        {
            fileContent = fileContent +data+ "\r\n" ;
        }
        try (FileWriter fw = new FileWriter(mistakeFile.getAbsoluteFile(), false);
                BufferedWriter bw = new BufferedWriter(fw)) {
            bw.write(fileContent);
            bw.close();
        }

    }

    /**
     *Returns file content.
     *
     */
    private static List<String> getFileContent(File file) {
        List<String> lst = new ArrayList<String>();
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                lst.add(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return lst;
    }
}

The console output is as follows .

Please enter max limit .....
15

Please enter min limit .....
15

Enter the number if iterations ..... 
100

15 X 2 = 
30

15 X 8 = 
120

15 X 9 = 
135

WRONG!! The correct answer is  15 X 9 = 135
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入java.util.Scanner;
公开课入学{
静态扫描;
静态随机;
静态列表;
静态列表错误列表;
静态文件出错;
静止的{
scanIn=新扫描仪(System.in);
随机=新随机();
列表=新的ArrayList();
}
/**
*调用私有方法的Main函数。
*/
公共静态void main(字符串[]args)引发IOException{
mistakeFile=新文件(args[0]);
如果(!mistakeFile.exists())
{
试一试{
mistakeFile.createNewFile();
}捕获(IOE){
e、 printStackTrace();
}
}
mistakeList=getFileContent(mistakeFile);
System.out.println(“请输入最大限制…”);
整数maxLimit=Integer.parseInt(scanIn.nextLine());
System.out.println(“请输入最小限制…”);
整数minLimit=Integer.parseInt(scanIn.nextLine());
System.out.println(“如果迭代次数……,请输入数字”);
整数迭代次数=Integer.parseInt(scanIn.nextLine());
可执行(最大限制、最小限制、迭代);
scanIn.close();
writeIntoFile();
}
/**
* 
*乘法逻辑,输出两个数字,从用户和用户处获取输入
*将其与乘法的结果匹配。
*/
私有静态void可执行(整数maxLimit、整数minLimit、,
整数迭代){
int skipCount=0;
对于(int iTemp=0;iTemp
尽管提供了正确的输入,但上述程序显示了错误。 不知道15*9有什么问题

添加更多描述


上述程序在一个限制内生成两个随机数,用户必须输入正确答案。例如,上述程序显示两个数字,如“15 X 8=”,用户必须输入120。上述程序适用于除“15 X 9=”以外的所有其他组合。用户输入135,这是正确答案,但程序显示它的答案不正确。

问题在这里:

if (usrAnswer != answer) {
您正在比较
Integer
对象,必须这样做:

if (usrAnswer.equals(answer)) {
比较=检查对象实例是否相同。它适用于较小数量的实例的原因是实例的缓存达到了一定的限制

在这种情况下,更好的解决方案是使用原语
int
类型:

int usrAnswer = Integer.parseInt(scanIn.nextLine());
int answer = num1 * num2;
if (usrAnswer != answer) {

这是因为将对象与
==
进行比较。问题
Integer usrAnswer = Integer.parseInt(scanIn.nextLine());
Integer answer = num1 * num2;
if (usrAnswer != answer)
int usrAnswer = Integer.parseInt(scanIn.nextLine());
int answer = num1 * num2;
if(usrAnswer != answer)
if(!usrAnswer.equals(answer))