为什么我的min总是打印0? [英] Why does my min always print out 0?

查看:67
本文介绍了为什么我的min总是打印0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我要求用户输入10个数字,然后我必须找到这些数字的最大值和最小值。我可以打印出我输入的最大数量,但因为我无法弄清楚如何打印我的最小数字。



我尝试了什么:



  import  java.util.Scanner; 
public class 应用{

public static void main(字符串 [] args){
int [] minAndMax = new < span class =code-keyword> int [ 10 ];
扫描仪扫描= 扫描仪(System.in);


int smallest = minAndMax [ 0 ];
int maximum = minAndMax [ 0 ];


for int i = 0 ; i< minAndMax.length; i ++)
{
System.out.print( 输入数字:);
minAndMax [i] = scan.nextInt();
}

for int i = 0 ; i< minAndMax.length; i ++)
{
if (minAndMax [i] >最大)
最大= minAndMax [i];

/ * 否则if(minAndMax [i]< smallest)
{
最小= minAndMax [i];
* /


}
for int i = 0 ; i> minAndMax.length; i ++)
{
if (minAndMax [i]< smallest)
smallest = minAndMax [i];
}

System.out.println( 最大数量: +最大);
System.out.println( 最小数量: +最小值);
}

}

解决方案

 int smallest = minAndMax [0 ]; 



什么是minAndMax [0]初始化为?



将最小值设置为最大值并且最小到可能的最小值。

 int smallest = Integer.MAX_VALUE; 
int largest = Integer.MIN_VALUE;





通常情况下,如果你将初始化为Integer的默认值为0不要指定一个值。

除非你输入一个负数,否则你已经有0个最小值。

取消注释第一个循环中的代码但是没有使用别的。摆脱for语句中有错误的第二个循环 - 它不是必需的。



 for(int i = 0; i< ; minAndMax.length; i ++)
{
if(minAndMax [i]>最大)
最大= minAndMax [i];
if(minAndMax [i]< smallest)
smallest = minAndMax [i];由$ int $ = 0; i> minAndMax.length; i ++)



更改>对于< - 它应该可以工作。


你应该尝试设置最小最大 用户输入值后的初始值



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


First I ask the user to input 10 numbers, then I have to find the max and min of those numbers. I can print out the maximum number of my input, but for I can't figure out how to print my minimum number.

What I have tried:

import java.util.Scanner;
public class App {

    public static void main(String[] args) {
        int[] minAndMax = new int[10];
        Scanner scan = new Scanner(System.in);
        
  
        int smallest = minAndMax[0];
        int largest = minAndMax[0];
       
        
        for(int i = 0; i < minAndMax.length; i++ )
        {
           System.out.print("Enter in a number: ");
           minAndMax[i] = scan.nextInt();
        }
      
        for (int i = 0; i < minAndMax.length; i++)
        {
            if(minAndMax[i] > largest)
                largest = minAndMax[i];
            
            /*else if (minAndMax[i] < smallest)
            {
                smallest = minAndMax[i];
            */
      
        }   
        for(int i = 0; i > minAndMax.length; i++ ) 
        {
            if (minAndMax[i] < smallest)
                smallest = minAndMax[i];
        }
       
        System.out.println("Maximum Number: " + largest);
        System.out.println("Minimum Number: " + smallest);
    }
    
}

解决方案

int smallest = minAndMax[0];


What is minAndMax[0] initialized to?

Set smallest to the largest possible value and largest to the smallest possible value.

int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;



Normally, the default value an Integer will be initialized to will be 0 if you don't specify a value.
Unless you type in a negative number, you already have 0 in the smallest.
Uncomment the code in your first loop but don't use an else. Get rid of the second loop which has an error in the for statement - it's not needed.

for (int i = 0; i < minAndMax.length; i++)
{
    if(minAndMax[i] > largest)
        largest = minAndMax[i];        
    if (minAndMax[i] < smallest)
        smallest = minAndMax[i];
}


Because your loop never enters the body:

for(int i = 0; i > minAndMax.length; i++ ) 


Change the > for a < - it should work.


You should try to set smallest and largest initial values after the user have entered the values.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于为什么我的min总是打印0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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