简体   繁体   English

如何使用扫描仪在用户输入中使用书写器

[英]How to use writer with user input using scanner

I'm trying to create a file by getting the user input. 我正在尝试通过获取用户输入来创建文件。 For example, it the user enter number of sets and write that to a file and writing two more user input. 例如,用户输入套数并将其写入文件,然后再写入两个用户输入。 But it doesn't show any files. 但是它不显示任何文件。 Also for additional questions how do I record a random numbers in the file? 另外还有其他疑问,如何在文件中记录随机数?

My purpose of the code is that when the user choose 2 sets and the size of sets that user wants. 我编写代码的目的是,当用户选择2套和用户想要的套数时。 For example, if the user pick 4 and 6, it randomly generate 4 numbers and 6 numbers seperately. 例如,如果用户选择4和6,它将随机分别生成4个数字和6个数字。 I know how to use random but just don't know how to implement this to writing a file. 我知道如何使用随机,但只是不知道如何实现此目的以编写文件。

These are the sample output that I should get 这些是我应该得到的样本输出

2

5

23 789 900 4000 4

10

10002 50930 2345 30 20 1 0 45 5 8000

From the code: 从代码:

public static void main(String[] args) throws IOException {
Random rnd = new Random();
File file = new File("C://Users//Hyeon Jin Ryu//nums.txt");
FileWriter w = new FileWriter(file);
Scanner in = new Scanner(System.in);



System.out.println("Please enter numbers of sets: ");
int a = in.nextInt();


System.out.println("Please enter the size of two each sets");
int firstSet = in.nextInt();
int secondSet = in.nextInt();
int t = rnd.nextInt(firstSet);
int k = rnd.nextInt(secondSet);


w.write(a);
w.write(t);
w.write(k);
in.close();
w.flush();
w.close();

} }

The code you posted does not output anything close to the sample output you provided. 您发布的代码不会输出与您提供的示例输出相近的任何内容。 You should really pay some attention to what the methods you're invoking actually do. 您应该真正注意所调用的方法的实际作用。

  1. You are asking the user for the amount of sets but only scanning for 2 inputs, which are hardcoded. 您要向用户询问集合的数量,但只扫描2个输入(已硬编码)。 So the first Scanner input is completely redundant. 因此,第一个扫描器输入是完全冗余的。
  2. The reason you're getting weird characters is because FileWriter.write(int) writes one single character as stated in the documentation . 之所以会出现奇怪的字符,是因为FileWriter.write(int) 按照文档中的说明写入一个字符 Otherwise you should be using strings. 否则,您应该使用字符串。

Writes a single character. 写一个字符。 The character to be written is contained in the 16 low-order bits of the given integer value; 给定整数值的16个低位包含在要写入的字符中。 the 16 high-order bits are ignored. 16个高位被忽略。

You need to parse the integer to a string with w.write(String.valueOf(t)) but it still wouldn't output more than one number because you need to loop your code first. 您需要使用w.write(String.valueOf(t))将整数解析为一个字符串,但是它仍然不会输出多个数字,因为您需要先循环代码。

  1. The following code does not generate X amount of numbers but just gets a number firstSet from the user and sets it as the highest random number it will generate, which is also shown in the docs or any IDE for that matter: 以下代码不会生成X数量的数字,而只是从用户那里获取一个数字firstSet并将其设置为它将生成的最高随机数,这也将在文档或任何IDE中显示:

     int firstSet = in.nextInt(); // if user enters 6 int t = rnd.nextInt(firstSet); // generate random number from 0-6 

So even if you did parse the a , t and k values then with the same inputs you provided (2, 5, 10) it would: 因此,即使您确实解析了atk值,然后使用您提供的相同输入(2、5、10),它也会:

  • Ask for 2 sets regardless of the user input 要求2套,无论用户输入什么
  • Generate a random number between 0-5 产生0-5之间的随机数
  • Generate a random number between 0-10 产生介于0到10之间的随机数
  • Write them into a file with no spacing or line breaks, so you would end up with something like 245 . 将它们写入没有空格或换行符的文件中,因此最终会得到类似245

You want to ask the user for the amount of sets, store it as a variable and create a loop which runs as many times as the value in the variable and generates X amount of sets with Y amount of random numbers. 您想向用户询问集合的数量,将其存储为变量,并创建一个循环,循环的次数与变量中的值一样多,并生成X数量的集合和Y数量的随机数。 In case of 2 sets with sizes 5 and 10 your code would need to loop 5 and 10 times respectively to generate 5 and 10 random numbers, and repeat the entire process 2 times. 如果有2套大小分别为5和10的代码,则您的代码将需要分别循环5和10次以生成5和10随机数,并重复整个过程2次。

The following code produces the result you're describing. 以下代码产生您要描述的结果。 Note how rnd.nextInt() is bound by 2500, how the 2nd for loop runs as many times as your user-provided set size and then runs everything again in a nested for loop for the provided amount of sets. 请注意rnd.nextInt()如何被2500绑定,第二个for循环如何运行与用户提供的集合大小一样多的次数,然后在嵌套的for循环中再次为提供的集合数量运行所有内容。

It also uses try-with-resources for FileWriter for handle the exception in the code rather than passing it back to JVM to just crash, and secondly for autoclosing the FileWriter stream. 它还将FileWriter的try-with-resources用于处理代码中的异常,而不是将其传递回JVM以使其崩溃,其次用于自动关闭FileWriter流。

public static void main(String[] args) {
    Random rnd = new Random();
    File file = new File("nums.txt");

    try (FileWriter w = new FileWriter(file)) {
        Scanner in = new Scanner(System.in);
        StringBuilder setResult = new StringBuilder();

        System.out.println("Please enter the number of sets: ");
        int n = in.nextInt();

        System.out.println("Please enter the size of each set: ");
        for (int j = 0; j < n; j++) {
            int setSize = in.nextInt();

            for (int k = 0; k < setSize; k++) {
                int randomNum = rnd.nextInt(2500);
                setResult.append(randomNum).append(" ");
            }
            setResult.append("\n");
        }
        w.write(setResult.toString());
        in.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

The generated file writes just fine and contains: 生成的文件写得很好,并包含:

699 494 1611 1521 2042 
2478 500 177 1602 348 231 1191 
842 421 93 1229 1804 802 1845 2245 836 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM