package practiceit;
import java.util.Scanner;
public class numbers {
public static void main (String[]args) {
int max=0;
int min = 0;
}
public static int smallestLargest() {
Scanner console= new Scanner(System.in);
System.out.print("how many numbers do you want to enter?");
int num= console.nextInt();
for (int i=1; i<=num; i++) {
int nums =console.nextInt();
System.out.println("Number "+i+": "+nums);
}
return nums;
}
}
如何返回nums以便将其存储为max和min变量,这样我就可以打印用户输入的最高值和最低值?
答案 0 :(得分:0)
这样的事情?
import java.util.Scanner;
public class numbers
{
public static void main(String[] args)
{
int min = 0, max = 0, current, nums;
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers do you want to enter: ");
nums = scan.nextInt();
for(int i = 0; i < nums; i++)
{
current = scan.nextInt();
if(i == 0)
{
min = current;
max = current;
}
if(current < min)
min = current;
if(max < current)
max = current;
}
//after the loop you should have the max and min value the user entered.
System.out.println("The max number is: "+max);
System.out.println("The min number is: "+min);
scan.close();
}
}
答案 1 :(得分:0)
您将获得数组中的所有值
package practiceit;
import java.util.Scanner;
public class numbers {
int[] array = null;
public static void main (String[]args) {
int max=0;
int min = 0;
}
public static int smallestLargest() {
Scanner console= new Scanner(System.in);
System.out.print("how many numbers do you want to enter?");
int num= console.nextInt();
array[] = new int[num];
for (int i=1; i<=num; i++) {
int nums =console.nextInt();
System.out.println("Number "+i+": "+nums);
array [i]= nums;
}
return nums;
}
}
如果使用Array.sort(array);
,您将对数组进行排序,并且可以轻松读取最小值和最大值