编写一个程序,使用循环显示用户输入的整数从最小到大

时间:2016-07-22 01:38:03

标签: java file loops if-statement

用户在循环中输入一系列整数。用户输入-99表示系列的结束。所有整数都将保存在文件中。输入所有数字后,程序应读取保存在文件中的所有整数,并显示从最小到最大的所有数字。

我一直在尝试,但我只是想弄清楚,如何按顺序显示整数。现在我卡住了。问你能看到,我没有使用任何奇特的编程,如数组或列表,因为我我还在学习课程,还没有学到任何课程。请帮忙!谢谢你,我还是个初学者。

package chapter4;

import java.util.Scanner;

import java.io.*;

import java.util.Random;

public class ProChallenge10 {

    public static void main(String[]args) throws IOException{

        int integer =0; //Integer enter by the user.

        boolean signal = false; // To end the loop.

        Scanner keyboard = new Scanner(System.in);

        //Created the file for the integers entered. 
        PrintWriter outputFile = new PrintWriter("ProChallenge10.txt"); 

        //Let the user enter a series of numbers.
        while(signal == false){

            System.out.println("Enter an integer");
            integer = keyboard.nextInt();

            outputFile.println(integer);

        //To end the program.
        if(integer == -99){
            signal = true;
            //Close the outputFile.
            outputFile.close();
            } 
        }

         //Open the file and read input from the file.
         File file = new File("ProChallenge10.txt");
         Scanner inputFile = new Scanner(file);

         //Read all of the values from the file and display their numbers. 
         while(inputFile.hasNext()){

            int number = inputFile.nextInt();

           }
         //Close the InputFile.
         inputFile.close();
    }
}

2 个答案:

答案 0 :(得分:0)

将它们添加到List然后sort(),然后再将其打印到文本文件中。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.*;
import java.util.Random;

public class ProChallenge10 {

public static void main(String[]args) throws IOException{

    int integer =0; //Integer enter by the user.

    boolean signal = false; // To end the loop.

    Scanner keyboard = new Scanner(System.in);

    List<Integer> listofInt = new ArrayList<Integer>();// Array List for your integers

    //Created the file for the integers entered. 
    PrintWriter outputFile = new PrintWriter("ProChallenge10.txt"); 

    //Let the user enter a series of numbers.
    while(signal == false){

        System.out.println("Enter an integer");
        integer = keyboard.nextInt();

        listofInt.add(integer);// adding of int to list


    //To end the program.
    if(integer == -99){
        listofInt.sort(null);// sort the list
        for(int x=0;x<listofInt.size();x++){
            outputFile.println(listofInt.get(x));//Printing of list to text file
        }
        signal = true;
        //Close the outputFile.
        outputFile.close();
        } 
    }

     //Open the file and read input from the file.
     File file = new File("ProChallenge10.txt");
     Scanner inputFile = new Scanner(file);

     //Read all of the values from the file and display their numbers. 
     while(inputFile.hasNext()){

        int number = inputFile.nextInt();

       }
     //Close the InputFile.
     inputFile.close();
}

}

答案 1 :(得分:0)

有可能为您做好功课,从//Open the file and read input from the file.注释开始,如果您更换它或只是添加这些更改,这将从最小到最大排序并输出到控制台窗口:

     //Open the file and read input from the file.
     File file = new File("ProChallenge10.txt");
     Scanner inputFile = new Scanner(file);
     ArrayList<Integer> arrList = new ArrayList<Integer>();
     //Read all of the values from the file and display their numbers. 
     while(inputFile.hasNext()){

        Integer number = inputFile.nextInt();
        arrList.add(number);

       }
    Collections.sort(arrList, new Comparator<Integer>() {
    @Override
    public int compare(Integer integer1, Integer integer2)
    {
        return  integer1.compareTo(integer2);
    }
});
     //Close the InputFile.
     inputFile.close();

     //Display the sorted numbers
     for(Integer number : arrList){
         System.out.println(number);
     }

如果您需要将已排序的数字放回文件中,我会留给您做的,因为工具已经可以使用了。如果你想知道这是如何工作的,我很乐意在有机会时回复。

作为F.Y.I.你因为没有付出努力而被遗忘。我赞成,因为我知道它是什么样的开始,每个人都应该有些松懈。但是要为下一篇文章付出更多努力。