gpt4 book ai didi

java - 使用 System.out 中的方法不起作用?

转载 作者:行者123 更新时间:2023-12-01 18:19:05 26 4
gpt4 key购买 nike

我的任务是完成几个目标,我要读取一个文件,我相信我做得正确,然后从该整数文件中将其放入一个数组中。它不会让我编译代码,它至少会出现错误。那么,如何打印出 min 的方法呢?

public class jlrogers2 {

public static void reader(int[] arr) throws FileNotFoundException {

Scanner scanner = new Scanner(new FileReader("numbers.txt"));
int i = 0;

while(scanner.hasNextInt())
{
arr[i++] = scanner.nextInt();
}


}

public static int minnimum(int[] arr){

int smallest =arr[0];

for (int i = 1; i>arr.length; i++){
if (arr[i] > smallest)
{
smallest= arr[i];
System.out.println(smallest);
}
}
return smallest;
}
public static void main(String [] args) throws FileNotFoundException
{


Scanner in = new Scanner (System.in);
System.out.println("Enter 1 for max index value.\nEnter 2 for min index value.\nEnter 3 to search for an index value.\n"
+ "Enter 4 for display all index's\nEnter 5 for numbers in a range.\nEnter 6 to exit menu. ");

int number = in.nextInt();

if(number==6){
System.out.println("Thank you for being awesome");
}

if (number==5){
System.out.println(minnimum(smallest)) // here is my issue }


}

}

最佳答案

更改for (int i = 1; i>arr.length; i++)for (int i = 0; i<arr.length; i++)而且你找到最小的逻辑也是错误的。实际上你正在寻找最大的。

要找到最小的,请将其写为

if(arr[i] < smallest )里面for循环

将主要更改为

 public static void main(String [] args) throws FileNotFoundException   
{

Scanner in = new Scanner (System.in);
System.out.println("Enter 1 for max index value.\nEnter 2 for min index value.\nEnter 3 to search for an index value.\n"
+ "Enter 4 for display all index's\nEnter 5 for numbers in a range.\nEnter 6 to exit menu. ");

int number = in.nextInt();
int arr[] = new int [200];//change this according to the requirement
if(number==6){
System.out.println("Thank you for being awesome");
}

if (number==5){
reader(arr);
System.out.println(minnimum(arr));
}
}

关于java - 使用 System.out 中的方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28082685/

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