Java 查找数组中的最小数时出错
我试图在1000个可能的插槽数组中找到最小的数字,但我的代码始终返回0,即使0不是我的输入之一。我的问题是在最后一个for循环中,其余的代码可以工作。这是我的密码:Java 查找数组中的最小数时出错,java,arrays,for-loop,Java,Arrays,For Loop,我试图在1000个可能的插槽数组中找到最小的数字,但我的代码始终返回0,即使0不是我的输入之一。我的问题是在最后一个for循环中,其余的代码可以工作。这是我的密码: import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; public class SmallestNumber { public static boolean isInteger(Strin
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);
}
}
import java.util.ArrayList;
导入java.util.InputMismatchException;
导入java.util.Scanner;
公共类最小数
{
公共静态布尔isInteger(字符串数)
{
布尔值=假;
尝试
{
intd=Integer.parseInt(num);
再次=正确;
}
捕获(数字格式)
{
再次=假;
}
再次返回;
}
公共静态void main(字符串[]args)
{
int[]intNum=新的int[1000];
int i=0;
字符串数;
布尔重复=假;
String done=“done”;
扫描仪inData=新扫描仪(System.in);
System.out.println(“最多可以输入1000个整数。”+“\n”+“输入‘完成’以完成”);
而(!重复)
{
系统输出打印(“Int:”);
num=inData.next();
重复=isInteger(num);
如果(重复==错误)
{
String entry=num.toUpperCase();
布尔等于=输入。等于(“完成”);
如果(等于真)
{
重复=正确;
}
其他的
{
System.out.println(“错误:您没有输入有效的字符。请输入整数或状态‘完成’”);
重复=错误;
}
}
其他的
{
int number=Integer.parseInt(num);
intNum[i]=数字;
i=i+1;
如果(i您没有说明实际输入的整数数量,但问题是您正在迭代intnum.length
次。您将输入字段声明为1000个元素的数组,length
将始终为1000,即使用户输入的整数少于1000个。一旦您的代码经过整数,您就可以实际输入时,它将到达数组的初始化0。过程:
提示用户
验证输入:如果输入未跳过,则添加到整数的ArrayList
按升序对数组列表排序
索引0处的值是最小的
您的系统中不适合此过程的所有其他内容要么不合适,要么不需要
更新:
public static void main(String[] args)
{
List<Integer> numbers = new ArrayList<Integer>(1000);
Scanner inData = new Scanner(System.in);
System.out.println("You can enter up to 1000 integers." + "\n"
+ "Enter 'done' to finish");
String input = "";
do
{
input = inData.next();
try
{
numbers.add(Integer.parseInt(input));
}
catch(NumberFormatException e)
{
System.out.println(input + " is not a number. Skipping...");
}
} while (!input.equalsIgnoreCase("done"));
Collections.sort(numbers);
inData.close();
System.out.println("Smallest number: " + numbers.get(0));
}
publicstaticvoidmain(字符串[]args)
{
列表编号=新阵列列表(1000);
扫描仪inData=新扫描仪(System.in);
System.out.println(“最多可以输入1000个整数。”+“\n”
+“输入‘完成’以完成”);
字符串输入=”;
做
{
input=inData.next();
尝试
{
add(Integer.parseInt(输入));
}
捕获(数字格式)
{
System.out.println(输入+“不是数字,正在跳过…”);
}
}而(!input.equalsIgnoreCase(“done”);
集合。排序(编号);
inData.close();
System.out.println(“最小数:+numbers.get(0));
}
这正是标题所建议的,没有所有额外的内容。这是大卫·华莱士建议的,可能有点过火。我不确定。没有时间进行基准测试。我明天必须打包旅行。也许有人可以对此发表评论。嗨,我通过将循环结束的长度更改为I并切换,解决了这个问题
intNum[j]=temp;
到
再次感谢您花时间学习如何使用IDE的调试器。真的。请看这一行-intNum[j]=temp;
并向我解释它在做什么。然后将其更改为它应该是什么。此外,您还有名为i
、num
、number
、temp
和intNum
的变量。您如何跟踪每个变量的用途?请为所有变量使用更多信息性的名称。它设置了temp的e值到j处的数组值?(如果这看起来很简单,很抱歉,我是初学者……)您确定它将temp
的值设置为某个值吗?如果您编写类似a=5;
的内容,通常会发生什么情况?这很有意义,有没有办法将for循环的长度设置为仅数组的初始化元素?没有。数组对Java只有1000个整数,因此初始零对i同样有效t作为您的数据项。您必须跟踪您记录的有效项数,这看起来您已经在使用i变量。对数组进行排序对我来说似乎有些过分。您只需通过一次就可以找到最低的数字。OP的原始算法比这更有效-他/她只是有两个小错误它的实施过程中会遇到困难。
temp=intNum[j];