编写一个用户界面的程序,接受输入的10个整数,并输出这是个整数的最大值盒最小值

任付江 2010-08-14 03:47:44
代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Max_Min {
public static void main(String[] args) {
int i,n = 10,max = 0,min = 0,temp = 0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

max=min=Integer.parseInt(br.readLine());

} catch (IOException e) { }
for(i =2;i<=n;i++) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
temp = Integer.parseInt(br.readLine());
if(temp>max)max=temp;
if(temp<min)min=temp;
} catch (IOException e) {}
}
System.out.println("max=" +max+"\\nmin="+min);
}

}

怎么就数不结果了,请高手讲解!
...全文
1689 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
goldyellow34 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 closewbq 的回复:]

Java code

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Max_Min {
public static void main(String[] args) {
Scan……
[/Quote]
简单粗暴
yidinghe2 2010-08-16
  • 打赏
  • 举报
回复
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
*
* @author HeYiding
*/
public class ScannerDemo {

public static void main(String[] args) {
// 获取用户输入
String prompt = "请输入任意多个数字,用空格隔开:";
List<Integer> ints = readNumbers(prompt);

// 查找最大最小值
Integer max = null, min = null;
for (Integer i : ints) {
if (max == null || i > max) {
max = i;
} else if (min == null || i < min) {
min = i;
}
}

System.out.println("最大值: " + max + ", 最小值: " + min);
}

/**
* 读取用户输入的数字,数字以空格隔开
*
* @param prompt 提示信息
*
* @return 用户输入的数字
*/
private static List<Integer> readNumbers(String prompt) {
List<Integer> result = new ArrayList<Integer>();

System.out.println(prompt);
Scanner scanner = new Scanner(readLine());
while (scanner.hasNextInt()) {
result.add(scanner.nextInt());
}

return result;
}

private static String readLine() {
Scanner lineReader = new Scanner(System.in);
String line = lineReader.nextLine();
return line;
}
}
closewbq 2010-08-16
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Max_Min {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int count=1;
List<Integer> list=new ArrayList<Integer>();
while(count<11){
System.out.println("请输入第"+count+"个数字");
list.add(scan.nextInt());
count++;
}
Collections.sort(list);
System.out.println("Min:"+list.get(0));
System.out.println("Max:"+list.get(list.size()-1));
}
}
wh070502219 2010-08-16
  • 打赏
  • 举报
回复
lz 可以结贴啦
wh070502219 2010-08-14
  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Max_Min {
public static void main(String[] args){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int max=0;
int min=10000000;

for(int i=0;i<10;i++){
try {
int c=Integer.parseInt(bf.readLine());
if(c>max){
max=c;
}
if(c<min){
min=c;
}
//System.out.println();
}
catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Max number:"+max);
System.out.println("Min number:"+min);
}
}

那个最小的写的不怎么好,局限于开始设置的初始值,没时间了,你自己看下吧,反正流程是这样!
在控制台没输入一个数字 回车
直到输出10数字后显示最大值和最小值
茫茫大海 2010-08-14
  • 打赏
  • 举报
回复
楼主不是说图形用户界面吗?怎么这就是嘛?感觉那程序写的很烂的!
wh070502219 2010-08-14
  • 打赏
  • 举报
回复
你的写法只能从控制台读入一次数据
你在控制台输入10行数据 最终读入的是最后第10行的数据
你自己可以去试试
wh070502219 2010-08-14
  • 打赏
  • 举报
回复
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

max=min=Integer.parseInt(br.readLine());

} catch (IOException e) { }


改为
for(int i=0;i<10;i++){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

max=min=Integer.parseInt(br.readLine());

} catch (IOException e) { }
}

62,614

社区成员

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

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