java输出最大数

来源:百度知道 编辑:UC知道 时间:2024/06/20 04:03:05
int c=0;
int i=0;
String a,b;

System.out.print("shu ru yi ge shu: ");
a=z.readLine();
int a1=Integer.parseInt(a);

do
{
System.out.print("shu ru yi ge shu: ");
b=z.readLine();
int b1=Integer.parseInt(b);
i++;

c=b1;
if(c<a1)
c=b1;
}
while(i<4);
System.out.println(c);

为什么这个不能输出最大的数?应该怎样改?
int c=0;
int i=0;
String a,b;

System.out.print("shu ru yi ge shu: ");
a=z.readLine();
int a1=Integer.parseInt(a);
c=a1;

do
{
System.out.print("shu ru yi ge shu: ");
b=z.readLine();
int b1=Integer.parseInt(b);
i++;

c=b1;
if(c<a1)
c=b1;
}
改成
c=b1;
if(c<a1)
c=a1;
}

//给你个现成的:
import java.io.*;
import java.util.Scanner;
//数字游戏
public class Test2 {
public static void main(String[] args) {
int len = 10;//数字个数
int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
int[] tmp = new int[len];//临时数组
String 使用方法="请输入 "+len+" 组数字,每组输入以回车结束\n本程式将给你算出最大值和最小值:";
System.out.println(使用方法);
//为了兼容JDK1.5以前的版本,这里采用BufferedReader对象来读取数据,而不用SCANNER对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i=0;
while(i<len){
System.out.print(i+1+"->");
try{
tmp[i] = Integer.parseInt(br.readLine());
++i;
}catch(Exception e){
System.out.println("您刚才输入有误,请输入整形的纯数字:");
}
}

for(i=0;i<len; i++){
max=max>tmp[i]?max:tmp[i