查找最小的数字,我试图寻找1000个可用插槽一个数组中最小的数字,但我的代码返回不停0,即使0不是我的一个输入。我的问题是在最后的for循环,其余的代码工作。这里是我的代码:阵列中的错误
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SmallestNumber
{
public static boolean isInteger(String num)
{
boolean again=false;
try
{
int d= Integer.parseInt(num);
again=true;
}
catch(NumberFormatException e)
{
again=false;
}
return again;
}
public static void main(String[] args)
{
int [] intNum = new int[1000];
int i=0;
String num;
boolean repeat = false;
String done="done";
Scanner inData = new Scanner(System.in);
System.out.println("You can enter up to 1000 integers." + "\n" + "Enter 'done' to finish");
while (!repeat)
{
System.out.print("Int: ");
num=inData.next();
repeat=isInteger(num);
if (repeat==false)
{
String entry=num.toUpperCase();
boolean equals=entry.equals("DONE");
if (equals==true)
{
repeat=true;
}
else
{
System.out.println("Error: you did not enter a valid chracter. Please enter a interger or state 'done'");
repeat=false;
}
}
else
{
int number=Integer.parseInt(num);
intNum[i]=number;
i=i+1;
if(i<1000)
{
repeat=false;
}
else
{
repeat=true;
}
}
}
int temp=intNum[0];
for(int j=1;j<intNum.length;j++)
{
if (intNum[j]<temp)
{
intNum[j]=temp;
}
else
{
}
}
System.out.print(temp);
}
}
时间的长度,解决了这个问题精益如何使用IDE的调试器。真。 – OldProgrammer
看看这行 - “intNum [j] = temp;'并向我解释它在做什么。然后改变它应该是什么。 –
此外,你有变量称为'我','数字','数字','温度'和'intNum'。你怎么可能跟踪每个人的目标?请为所有变量使用更多信息名称。 –