gpt4 book ai didi

Java Scanner 未接受有效输入或与整数解析相关的其他问题

转载 作者:行者123 更新时间:2023-11-30 03:06:38 26 4
gpt4 key购买 nike

请引用以下与数学表格练习相关的代码。对于除 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

虽然提供了 135 正确的输入,但上面的程序显示了错误。不知道 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) {

关于Java Scanner 未接受有效输入或与整数解析相关的其他问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34608782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com