初学者问题!如何判断用户输入的一串数字里的最大值和最小值?

qq_40710007 2017-12-03 01:51:45
题目如下:
Write a program with a loop that lets the user enter a series of integers. The user should
enter -99 to signal the end of the series. After atl the numbers have been entered, the program
should display the largest and smallest numbers entered.

我的思路:
用户每输入一个数字,就和前一个比大小,保留大的,然后再和下一个数字对比,以此类推,最后剩下的肯定的最大的。
同理获得最小的。
但是如果用loop的话,新输入的数字就会立刻把之前的顶掉,无法进行两者对比了。
请教如何解决,或者换一种思路?

注:

因是初学者问题,只能使用以下知识点:

2.1 The Parts of a Java Program 27
2.2 The print and println Methods, and the java API 33
2.3 Variables and Literals 39
2.4 Primitive Data Types 44
2.5 Arithmetic Operators 54
2.6 Combined Assignment Operators 63
2.7 Conversion between Primitive Data Types 65
2.8 Creating Named Constants with final 69
2.9 The string Class 70
2.10 Scope 75
2.11 Comments 77
2.12 Programming Style 82
2.13 Reading Keyboard Input 84
2.14 Dialog Boxes 92

3.1 The if Statement 109
3.2 The if-else Statement 119
3.3 Nested if Statements 122
3.4 The if-else-if Statement 129
3.5 Logical Operators 1 35
3.6 Comparing string Objects 143
3.7 More about Variable Declaration and Scope 149
3.8 The Conditional Operator (Optional) 150
3.9 The switch Statement 152
3.10 The system.out.printf Method 162
3.11 Creating Objects with the DecimalFormat Class 174

4.1 The Increment and Decrement Operators 193
4.2 The while Loop 197
4.3 Using the while Loop for Input Validation 204
4.4 The do-while Loop 208
4.5 The for Loop 211
4.6 Running Totals and Sentinel Values 220
4.7 Nested Loops 225
4.8 The break and continue Statements (Optional) 233
4.9 Deciding Which Loop to Use 233
4.10 Introduction to File Input and Output 234
4.11 Generating Random Numbers with the Random Class 253
...全文
632 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_40710007 2017-12-03
  • 打赏
  • 举报
回复
引用 5 楼 u014038116 的回复:


import java.util.Scanner;

public class FindNumber {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int currentNum=0,maxNum=0,minNum=0;
		currentNum=sc.nextInt();
		minNum=currentNum;
                maxNum=currentNum;
		while(currentNum!=-99){
			if(currentNum>maxNum){
				maxNum=currentNum;
			}
			if(currentNum<minNum){
				minNum=currentNum;
			}
			currentNum=sc.nextInt();
		}
		sc.close();
		System.out.println("max:"+maxNum+"-------min:"+minNum);
	}
}

试试看
完美解决!我怎么没想到用2个新变量把数字挪出去呢。。。 感谢感谢! 已给分!
xiaovhao 2017-12-03
  • 打赏
  • 举报
回复


import java.util.Scanner;

public class FindNumber {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int currentNum=0,maxNum=0,minNum=0;
		currentNum=sc.nextInt();
		minNum=currentNum;
                maxNum=currentNum;
		while(currentNum!=-99){
			if(currentNum>maxNum){
				maxNum=currentNum;
			}
			if(currentNum<minNum){
				minNum=currentNum;
			}
			currentNum=sc.nextInt();
		}
		sc.close();
		System.out.println("max:"+maxNum+"-------min:"+minNum);
	}
}

试试看
qq_40710007 2017-12-03
  • 打赏
  • 举报
回复
引用 3 楼 Inhibitory 的回复:
找到 -99 前面的部分,分割成数字,这些数字中找最大最小值
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        String input = "23 33 45 adfb 123 132 -99 23 abc";
        input = input.substring(0, input.indexOf("-99"));

        Integer min = null;
        Integer max = null;

        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            Integer value = Integer.valueOf(matcher.group(0));

            if (min == null) {
                min = value;
                max = value;
            }

            min = value < min ? value : min;
            max = value > max ? value : max;
        }

        System.out.println("Min: " + min + ", Max: " + max);
    }
}
您的代码使用了我没有学过的知识点,应该不是出题者所期望的答案。 能否只使用我帖子所列出的知识点来解决这个问题? 拜托拜托!
Inhibitory 2017-12-03
  • 打赏
  • 举报
回复
找到 -99 前面的部分,分割成数字,这些数字中找最大最小值
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        String input = "23 33 45 adfb 123 132 -99 23 abc";
        input = input.substring(0, input.indexOf("-99"));

        Integer min = null;
        Integer max = null;

        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            Integer value = Integer.valueOf(matcher.group(0));

            if (min == null) {
                min = value;
                max = value;
            }

            min = value < min ? value : min;
            max = value > max ? value : max;
        }

        System.out.println("Min: " + min + ", Max: " + max);
    }
}
理工小哥 2017-12-03
  • 打赏
  • 举报
回复
同为初学者路过
李德胜1995 2017-12-03
  • 打赏
  • 举报
回复

62,614

社区成员

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

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