输入N个数,找最大的一个数输出

yanxuehe 2006-10-17 04:20:00
输入N个数,找最大的一个数输出
请各位高手帮忙
小弟感激不尽
...全文
442 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mfkvfn 2006-10-19
  • 打赏
  • 举报
回复
读的时候读字符串,比较的时候再判断是否合法,就是能否转换成字符串。
比较的时候还是按照字符串比较,返回结果时候再转换成数字,不是更简单吗?
  • 打赏
  • 举报
回复
人家又没说 效率问题,给出1个 完了
Bird_fro 2006-10-19
  • 打赏
  • 举报
回复
简单的题目,方法应该很多,只看效率怎么样

guzuoshantou 2006-10-18
  • 打赏
  • 举报
回复
楼上这个就完全忘了效率问题了,你想确定让别人输多少个,声明个变量也就行了,不用声明个数组那么浪费
lgh2008 2006-10-18
  • 打赏
  • 举报
回复

import java.io.*;

public class SelectMax{

public static void main( String [] args ){

int max = 0 ;

while( true ){
System.out.print( "Please input a number , zero for exit : " );
int temp = Read.readInt( );
if( temp == 0 ) {
break ;
}
if ( temp == -1 ){
System.out.println( "bad number , try agin " );
continue ;
}

if( temp > max ){
max = temp ;
}
}

if( max == 0 ){
System.out.println( "You didn't input any nuber" );
}else {
System.out.println( "The Max number you had input is " + max );
}

}


}

/*
* Read 类用于读入数据。
*
*/

class Read{

private static BufferedReader br = null ;

/*
* 静态代码块初始化 br .
*/
static{
try{
br = new BufferedReader(
new InputStreamReader(
System.in ) );
}catch( Exception e ){
}
}

/*
* 读入一个整数,
*/
public static int readInt( ){
try{
String str = br.readLine( );
return Integer.parseInt( str.trim() );
}catch( Exception e ){
return -1 ;
}
}

}
Gallen1983 2006-10-17
  • 打赏
  • 举报
回复
import java.io.*;
//import java.io.IOException;
//import java.io.InputStreamReader;

public class Getmax
{

/**
* @param args
*/
public static void main(String[] args)
{
int i,n=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入比较数字的个数:");
try
{
n = Integer.parseInt(br.readLine());
}
catch (IOException e) {}
int[] a;
a = new int[n];
//int max=0;
for(i=0;i<a.length;i++)
{
System.out.println("请输入第"+(i+1)+"个数字:");
//String num="";
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
try
{
a[i] = Integer.parseInt(br1.readLine());
if(a[0]<a[i])
{
a[0] = a[i];
}
}
catch (IOException e) {}

}
System.out.println("Max is :"+a[0]);
}

}

这个比较好用!!
Gallen1983 2006-10-17
  • 打赏
  • 举报
回复
楼上说的那个程序 要是输入负数的话不就不好用了吗??
zml449542017 2006-10-17
  • 打赏
  • 举报
回复
你学没学过程序设计哟!这么简单的东东都有要问别人啊,算了我看你可以休息了.
liuguangliang 2006-10-17
  • 打赏
  • 举报
回复
顺序查找,找出最大的输出。时间复杂度为O(N)
haisenmai 2006-10-17
  • 打赏
  • 举报
回复
错了,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Getmax {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int max = 0;
while(true){
System.out.println("请输入数字,-1结束");
String num="";

try {
num = br.readLine();
if(max<Integer.parseInt(num)){
max = Integer.parseInt(num);
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}

if(num.equalsIgnoreCase("-1"))
break;
}
System.out.println("Max is :"+max);
}

}
haisenmai 2006-10-17
  • 打赏
  • 举报
回复
package csdn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Getmax {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int max = 0;
while(true){
System.out.println("请输入数字,-1结束");
String num="";

try {
num = br.readLine();
if(max<Integer.parseInt(num)){
max = Integer.parseInt(num);
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}

if(num.equalsIgnoreCase("quit"))
break;
}
System.out.println("Max is :"+max);
}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧