从用户那里获取五个输入的最大值和最小值
getting max and min of five input from user
import java.util.Scanner;
class Compare
{
int max = 0;
int min = 0;
public void max(int num)
{
if(num > max)
{
max = num;
}
}
public void min(int num)
{
if(num < min)
{
min = num;
}
}
}
class Main
{
public static void main(String args[])
{
Compare compare = new Compare();
Scanner input = new Scanner(System.in);
int sets = input.nextInt();
for (int i = 0; i < sets ; i++)
{
for (int j = 0; j < 5; j++)
{
int num_main = input.nextInt();
compare.max(num_main);
compare.min(num_main);
}
System.out.println(compare.max);
System.out.println(compare.min);
}
}
}
I want to calculate max and min of five inputs from the user, i managed to get max value but failed to get the min value instead i am getting 0 as my min value which was initialized from before in class Compare. How can i get the min value in the simplest possible way
您将 0 作为最小值的原因是因为当您调用 compare.min(num);
时,您的值正在与等于 0 的预定义本地值 min
进行比较。
当您将任何正值与 0 进行比较时,您将得到 0 作为最小值。
一个可行的解决方案是
class Compare
{
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
public void max(int num)
{
if(num > max)
{
max = num;
}
}
public void min(int num)
{
if(num < min)
{
min = num;
}
}
}
这将保证您首先比较的内容将保存为 min 值,并因此用作新值整个程序执行的最小值。
这会给你一个想法
I modified your Compare class. I used wrapper classes as instance variable data types.
class Compare
{
Integer max;
Integer min;
public void max(int num)
{
if (max == null) max = num;
else if(num > max) max = num;
}
public void min(int num)
{
if (min == null) min = num;
else if(num < min) min = num;
}
}
Now you will get what output you want.
您可以使用标记来简化此操作以跟踪第一个输入,然后将其余输入与相同的进行比较。
class Demo{
public static void main(String[]args){
Compare c = new Compare();
try(java.util.Scanner sc = new java.util.Scanner(System.in)){
int i = 1;
final int m = 5;
while(m>=i){
System.out.printf("Numer %d of %d: ", i, m);
if(sc.hasNextInt()){
c.process(sc.nextInt());
i ++;
continue;
}
System.out.println("Kindly input numbers only!");
sc.nextLine();/* clear input */
}
}
System.out.printf("Max: %d, Min: %d%n", c.getMax(), c.getMin());
}
}
class Compare{
private int min;
private int max;
private boolean first = true;
public int getMin(){
return min;
}
public int getMax(){
return max;
}
public void process(int val){
if(first){
this.min = this.max = val;
first = false;
return;
}
if(this.min > val){
this.min = val;
}
if(this.max < val){
this.max = val;
}
}
}
import java.util.Scanner;
class Compare
{
int max = 0;
int min = 0;
public void max(int num)
{
if(num > max)
{
max = num;
}
}
public void min(int num)
{
if(num < min)
{
min = num;
}
}
}
class Main
{
public static void main(String args[])
{
Compare compare = new Compare();
Scanner input = new Scanner(System.in);
int sets = input.nextInt();
for (int i = 0; i < sets ; i++)
{
for (int j = 0; j < 5; j++)
{
int num_main = input.nextInt();
compare.max(num_main);
compare.min(num_main);
}
System.out.println(compare.max);
System.out.println(compare.min);
}
}
}
I want to calculate max and min of five inputs from the user, i managed to get max value but failed to get the min value instead i am getting 0 as my min value which was initialized from before in class Compare. How can i get the min value in the simplest possible way
您将 0 作为最小值的原因是因为当您调用 compare.min(num);
时,您的值正在与等于 0 的预定义本地值 min
进行比较。
当您将任何正值与 0 进行比较时,您将得到 0 作为最小值。
一个可行的解决方案是
class Compare
{
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
public void max(int num)
{
if(num > max)
{
max = num;
}
}
public void min(int num)
{
if(num < min)
{
min = num;
}
}
}
这将保证您首先比较的内容将保存为 min 值,并因此用作新值整个程序执行的最小值。
这会给你一个想法
I modified your Compare class. I used wrapper classes as instance variable data types.
class Compare
{
Integer max;
Integer min;
public void max(int num)
{
if (max == null) max = num;
else if(num > max) max = num;
}
public void min(int num)
{
if (min == null) min = num;
else if(num < min) min = num;
}
}
Now you will get what output you want.
您可以使用标记来简化此操作以跟踪第一个输入,然后将其余输入与相同的进行比较。
class Demo{
public static void main(String[]args){
Compare c = new Compare();
try(java.util.Scanner sc = new java.util.Scanner(System.in)){
int i = 1;
final int m = 5;
while(m>=i){
System.out.printf("Numer %d of %d: ", i, m);
if(sc.hasNextInt()){
c.process(sc.nextInt());
i ++;
continue;
}
System.out.println("Kindly input numbers only!");
sc.nextLine();/* clear input */
}
}
System.out.printf("Max: %d, Min: %d%n", c.getMax(), c.getMin());
}
}
class Compare{
private int min;
private int max;
private boolean first = true;
public int getMin(){
return min;
}
public int getMax(){
return max;
}
public void process(int val){
if(first){
this.min = this.max = val;
first = false;
return;
}
if(this.min > val){
this.min = val;
}
if(this.max < val){
this.max = val;
}
}
}