如何使变量最大和最小连续识别不同的数字输入。而不是只识别最后的输入。 [英] How Do I Make The Variables Max And Min Continuously Recognize The Different Inputs Of Num. Instead Of Only Recognizing The Last Input.

查看:76
本文介绍了如何使变量最大和最小连续识别不同的数字输入。而不是只识别最后的输入。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/**
 * Write a description of class MinimumMaximum here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
import java.util.Scanner;
public class MinimumMaximum
{

    public MinimumMaximum()
    {
        Scanner number = new Scanner(System.in);
        int min=0, max=0, lcv = 0, num, num1;
        String maxmin;

          while ((lcv < 10) && (lcv >= 0))
        {
            lcv++;
            System.out.print("Please enter a number: ");
            num = number.nextInt();
            max = max(max,num);
        min = min(min,num);
        }










        maxmin= maxmin(max,min);
        System.out.println(maxmin);


    }


    public int max(int max, int num)
    {


        max = 1;




            if (num > max)

            max = num;

           else

           max = max;




    return max;
}

 public int min(int min,int num)
    {



        min = 0;



            if (num < min)
            min = num;

           else if (min > num)
           min = min;

        return min;
    }










    public String maxmin(int max, int min) {
        String maxmin = "The greatest value is " + max + " and the lowest value is "  + min + ".";
        return maxmin;
}

}

推荐答案

检查变量命名。你搞砸了很多最大和分钟。



你可以用this来引用类变量。



你也搞砸了返回值和引用:你按值传递max然后你返回那个值。

那太多了。传回或分配值:



Check your variable naming. You messed up there, to much "max" and "min".

You can reference the class variable with "this.".

also you messed up with return values and references: You are passing the "max" by value and then you return that value.
That is too much. Pass back OR assign values:

private void isMax(int num) // no return value
{
  if (num > this.max){
     this.max = num;
  }
}
 
public int min(int min,int num) // with return value
{
  if (num < min){
    return num;
  }
  return min; // default return
}





请选择一个版本并重写另一个版本。



编辑:

向方法或构造函数发送信息 [ ^ ]


这篇关于如何使变量最大和最小连续识别不同的数字输入。而不是只识别最后的输入。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆