Classic small instances in Java development-(compare the maximum and minimum values of input values)

Source: Internet
Author: User

Enter numbers to generate numbers and compare sizes

Import Java.util.Scanner;

public class Test1 {

public static void Main (string[] args) {

Scanner sc = new Scanner (system.in);

System.out.println ("Please enter the number of numbers you want to enter:");

int num = Sc.nextint ();

int[] array = new Int[num];

for (int i = 0; i < Array.Length; i++) {

System.out.println ("Please enter the number of" + (i + 1) + ". ");
Array[i] = Sc.nextint ();
}

SYSTEM.OUT.PRINTLN ("Maximum value:" + Getarraymax (array));
SYSTEM.OUT.PRINTLN ("Minimum value:" + getarraymin (array));

}

public static int Getarraymax (int[] array) {

int max = array[0];

for (int i = 1; i < Array.Length; i++) {

max = (Max < array[i])? Array[i]: max;
}

return Max;

}

public static int getarraymin (int[] array) {

int min = 0;

for (int i = 1; i < Array.Length; i++) {

min = (Array[min] > Array[i])? I:min;
}

return array[min];
}



}

Import Java.util.Scanner;

public class Test {

public static void Main (string[] args) {
TODO auto-generated Method Stub

/*
* int num=123; int sum;
*
* int a=num/100%10; int b=num/10%10; int c=num%10;
*
* SUM=A+B+C;
*
* SYSTEM.OUT.PRINTLN (SUM);
*/

Scanner input = new Scanner (system.in);
int max = 0;
int min = 0;

for (int i = 0; i < 5; i++) {
System.out.println ("Please enter" + (i + 1) + "number");

int number = Input.nextint ();

if (i = = 0) {

max = number;
min = number;
} else {

if (max < number) {

max = number;
}
if (min > number) {

min = number;
}
}
}

System.out.println ("Max:" + max);
SYSTEM.OUT.PRINTLN ("Minimum value is:" + min);
}

}

Classic small instances in Java development-(compare the maximum and minimum values of input values)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.