Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编写一个程序,使用循环显示用户输入的从小到大的整数_Java_File_Loops_If Statement - Fatal编程技术网

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

Java 编写一个程序,使用循环显示用户输入的从小到大的整数,java,file,loops,if-statement,Java,File,Loops,If Statement,用户在循环中输入一系列整数。用户输入-99表示序列结束。所有整数将保存在一个文件中。输入所有数字后,程序应读取保存在文件中的所有整数,并从最小到最大显示所有数字 我一直在尝试,但我就是想不出,如何按顺序显示整数。现在我被卡住了。你可以看到,我没有使用任何奇特的编程,如数组或列表,因为我仍然在学习类,还没有学习任何这些。请帮忙!谢谢你,我还是个初学者 package chapter4; import java.util.Scanner; import java.io.*; import ja

用户在循环中输入一系列整数。用户输入-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();
    }
}

将它们添加到
列表中
,然后将其打印到文本文件中

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();
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
导入java.io.*;
导入java.util.Random;
公共课前挑战10{
公共静态void main(字符串[]args)引发IOException{
int integer=0;//用户输入的整数。
boolean signal=false;//结束循环。
扫描仪键盘=新扫描仪(System.in);
List listofInt=new ArrayList();//整数的数组列表
//为输入的整数创建了文件。
PrintWriter outputFile=新的PrintWriter(“ProChallenge10.txt”);
//让用户输入一系列数字。
while(信号==假){
System.out.println(“输入整数”);
整数=键盘.nextInt();
添加(整数);//将整数添加到列表
//结束节目。
如果(整数==-99){
排序(null);//对列表进行排序

对于(int x=0;x,有可能为您做家庭作业,从
//开始打开文件并读取文件中的输入。
向下注释,如果您替换它或只是添加这些更改,这将从最小到最大排序并输出到控制台窗口:

     //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);
     }
//打开文件并从文件中读取输入。
File File=新文件(“ProChallenge10.txt”);
扫描仪输入文件=新扫描仪(文件);
ArrayList arrList=新的ArrayList();
//从文件中读取所有值并显示其编号。
while(inputFile.hasNext()){
整数=inputFile.nextInt();
添加(编号);
}
Collections.sort(arrList,newcomparator(){
@凌驾
公共整数比较(整数整数1、整数2)
{
返回整数1.compareTo(整数2);
}
});
//关闭输入文件。
inputFile.close();
//显示已排序的数字
for(整数:arrList){
系统输出打印项次(编号);
}
如果您需要将排序后的数字放回文件中,我会让您自己做,因为工具已经在您的手中。如果您想知道这是如何工作的,我很乐意在有机会时回复


作为一名F.Y.I.你因为没有表现出努力而被否决,被遗忘。我之所以被否决,是因为我知道一开始是什么样子,每个人都应该放松一下。但是为你的下一篇文章表现出更多的努力。

你将他们添加到
列表中,然后
排序()
列表。我甚至没有看到有人试图查看数字是否符合顺序或改变顺序……嗯。作为CS专业的学生,这看起来像是教授代码,你必须添加到……你尝试过什么?@dovahkiinvasknormandy你的教授给出了示例代码,例如
while(signal==false){
?噢,天哪。@ElliottFrisch哈哈哈,上帝不。我的意思是就格式而言。它非常整洁,所有部分都有注释。表示不是初学者/早期学生代码。加上血腥的包名是
第4章
哈,我实际上不知道list.sort(null)按这种方式排序。Neato。@DovahkiinvasNormandy我对java也相当陌生。但它对我来说很有效,从最小到最大。@DovahkiinvasNormandy如果你喜欢,我可以给你看ProChallenge10.txt,或者你可以试试代码。如果有什么错误,请告诉我。出于学习目的。