如何检查数组中的最大数字 [英] How to check biggest number in an array

查看:94
本文介绍了如何检查数组中的最大数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在java中找到数组中最大的数字?

i尝试过这段代码,显示异常可以帮助我调试代码。



我尝试过:



/ * package codechef; //不要放包名! * /



import java.util。*;

import java.lang。*;

import java .io。*;



/ *只有当班级公开时,班级名称必须为主。 * /

class Codechef

{

public static void main(String [] args)抛出java.lang.Exception

{

// System.out.println(输入要输入的数字的编号);

扫描仪s =新扫描仪(System.in );

int a = s.nextInt();

int digits [] = new int [a];

for(int i = 0; i< a; i ++)

{

扫描仪编号=新扫描仪(System.in);

int inputnumber = number。 nextInt();

digits [i] = inputnumber;

}

int max;

max = digits [0];

for(int j = 0; j< a; j ++)

{

if(digits [j + 1] >数字[j])

{

max = digits [j + 1];

}

}

System.out.println(最大);





}

}

How to find the biggest number in an array in java?
i have tried this code and its showing exception can anyone help me out to debug the code.

What I have tried:

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// System.out.println("enter the no of number you want to enter");
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int digits[]=new int[a];
for(int i=0;i<a;i++)
{
Scanner number=new Scanner(System.in);
int inputnumber=number.nextInt();
digits[i]=inputnumber;
}
int max;
max=digits[0];
for(int j=0;j<a;j++)
{
if(digits[j+1]>digits[j])
{
max=digits[j+1];
}
}
System.out.println(max);


}
}

推荐答案

你可以用一个basi c循环语句找到最大值



you could use a basic looping statement to find the maximum value

<pre>class Biggest
{
	public static void main(String args[])
	{
		int[] arr = {1,2,3,4,5,6,7,8,9};
		int currentBiggestNo= Integer.MIN_VALUE; //do not assign 0
                //if every integers present in the array are negative then you cannot find the biggest one since 0 is bigger than all but not present in the array..
		for(int number:arr)
		{
			if(number>currentBiggestNo)currentBiggestNo=number;
		}
		System.out.println(currentBiggestNo);
	}
}


Quote:

我已经尝试过这个代码,它的显示异常可以帮助我调试代码。

i have tried this code and its showing exception can anyone help me out to debug the code.



使用调试器查看代码正在做什么以及为什么会出现异常。 />
Nota:异常和位置的文本可以帮助我们看到原因。


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

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第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 [ ^ ]

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

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


Use the debugger to see what the code is doing and why you get an exception.
Nota: The text of exception and position may help us to see the reason.

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, it is an incredible learning tool.

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.


这篇关于如何检查数组中的最大数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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