要求用户输入第二大数字

时间:2015-05-08 17:49:17

标签: java

//java program that asks user to input a number and print the 1st and 2nd largest numbers

import java.util.Scanner;

public class twolargestnum
{
    public static void main(String args[])
    {
        Scanner input=new Scanner(System.in);

        int largest_2=0; //initializes second number
        int largest=0; //initializes first number
        int number;
        int counter=0;//initializes counter



        while(counter<10)
        {
            System.out.println("enter the number:");
            number=input.nextInt();//prompts user

            if(largest<number){
            largest=number;//stores number to largest
            largest_2=largest;}//stores largest to second largest or         largest_2
            else{

            if(largest_2<number &&largest>largest_2)
            largest_2=number; //stores second largest to number


            }
            counter=counter+1;//counts number input 10 times
    }

        System.out.println("The first largest number is "         +largest);//displays largest number inputed
    System.out.println("The second largest number is " +largest_2);//displays second largest number inputed

     }

}

输出

enter the number:
99
enter the number:
88
enter the number:
77
enter the number:
66
enter the number:
55
enter the number:
44
enter the number:
33
enter the number:
22
enter the number:
11
enter the number:
10
The first largest number is 99
The second largest number is 99 

正如您所看到的,该程序显示第二大数字为99而不是88.我做错了什么?

3 个答案:

答案 0 :(得分:1)

至少交换这两行:

        largest=number;//stores number to largest
        largest_2=largest;//stores largest to second largest or

答案 1 :(得分:1)

在更新largest_2之前,您需要将largest更改为largest

答案 2 :(得分:0)

这是一个经典的错误。

element.find('input[style="display:inline"]').length => 0
element.find('input[style="display:block"]').length => 0
element.find('input[style="display:flex"]').length => 0
element.find('input[style="display:inline-block"]').length => 0
element.find('input[style="display:inline-flex"]').length => 0
element.find('input[style="display:inline-table"]').length => 0
element.find('input[style="display:list-item"]').length => 0
element.find('input[style="display:run-in"]').length => 0
element.find('input[style="display:table"]').length => 0
element.find('input[style="display:table-caption"]').length => 0
element.find('input[style="display:table-column-group"]').length => 0
element.find('input[style="display:table-header-group"]').length => 0
element.find('input[style="display:table-footer-group"]').length => 0
element.find('input[style="display:table-row-group"]').length => 0
element.find('input[style="display:table-cell"]').length => 0
element.find('input[style="display:table-column"]').length => 0
element.find('input[style="display:table-row"]').length => 0
element.find('input[style="display:none"]').length => 2
element.find('input[style="display:initial"]').length => 0
element.find('input[style="display:inherit"]').length => 0

执行第一个操作时,实质上是在更改(删除)最大的存储值。 @Ishamael说你需要切换这两个语句是正确的。逻辑是您正在将最大更改为数字(删除旧的最大值),然后尝试将新变量( largest_2 )分配给删除的值。您需要使用 largest_2 来保存最大 的值更改它的值。