第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

這段代碼應(yīng)該從用戶那里獲取 N 個(gè)值。然后輸入到.txt文件中

這段代碼應(yīng)該從用戶那里獲取 N 個(gè)值。然后輸入到.txt文件中

陪伴而非守候 2023-07-28 10:07:00
這段代碼應(yīng)該從用戶那里獲取 N 個(gè)值。然后將值輸入到 .txt 文件中。我無法在 .txt 文件中顯示值。不知道為什么。// This program writes data into a file.import java.io.*; // Needed for File I/O class.import java.util.Scanner; // Needed for Scanner class.public class program01{    public static void main (String [] args) throws IOException    {        int fileName;        int num;        // Create a Scanner object for keyboard input.        Scanner input = new Scanner(System.in);        File fname = new File ("namef.txt");        Scanner inputFile = new Scanner(fname);  // Create a Scanner object for keyboard input.        FileWriter outFile = new FileWriter ("namef.txt", true);        PrintWriter outputfile = new PrintWriter("/Users/******/Desktop/namef.txt");        System.out.println("Enter the number of data (N) you want to store in the file: ");        int N = input.nextInt(); // numbers from the user through keyboard.        System.out.println("Enter " + N + " numbers below: ");        for ( int i = 1; i <= N; i++)        {            // Enter the numbers into the file.            input.nextInt();            outputfile.print(N);        }        System.out.println("Data entered into the file.");        inputFile.close(); // Close the file.    }} // End of class
查看完整描述

2 回答

?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊

在你的程序中,你似乎已經(jīng)扔掉了所有東西并希望它能起作用。要找出應(yīng)該使用哪個(gè)類,您應(yīng)該在 Java 版本的 Javadoc 中搜索它。?

打印作家:

將對(duì)象的格式化表示打印到文本輸出流。此類實(shí)現(xiàn)了 PrintStream 中的所有打印方法。它不包含寫入原始字節(jié)的方法,程序應(yīng)使用未編碼的字節(jié)流。

文件編寫器:

使用默認(rèn)緩沖區(qū)大小將文本寫入字符文件。從字符到字節(jié)的編碼使用指定的字符集或平臺(tái)的默認(rèn)字符集。

掃描儀(文件來源):

構(gòu)造一個(gè)新的 Scanner,它生成從指定文件掃描的值。使用底層平臺(tái)的默認(rèn)字符集將文件中的字節(jié)轉(zhuǎn)換為字符。

現(xiàn)在您可以看到每個(gè)課程的用途。PrintWriter 和 FileWriter 都用于寫入文件,但 PrintWriter 提供更多格式選項(xiàng),而 Scanner(文件源)用于讀取文件而不是寫入文件。


因?yàn)?PrintWriter 已經(jīng)有了答案。我正在使用 FileWriter 編寫此內(nèi)容。

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

? ? // Create a Scanner object for keyboard input.

? ? Scanner input = new Scanner(System.in);


? ? // You can provide file object or just file name either would work.

? ? // If you are going for file name there is no need to create file object

? ? FileWriter outputfile = new FileWriter("namef.txt");

? ? System.out.print("Enter the number of data (N) you want to store in the file: ");


? ? int N = input.nextInt(); // numbers from the user through keyboard.

? ? System.out.println("Enter " + N + " numbers below: ");

? ? for (int i = 1; i <= N; i++) {

? ? ? ? System.out.print("Enter the number into the file: ");

? ? ? ? // Writing the value that nextInt() returned.

? ? ? ? // Doc: Scans the next token of the input as an int.

? ? ? ? outputfile.write(Integer.toString(input.nextInt()) + "\n");

? ? }

? ? System.out.println("Data entered into the file.");

? ? input.close();

? ? outputfile.close(); // Close the file.

}

輸出:


Enter the number of data (N) you want to store in the file: 2

Enter 2 numbers below:?

Enter the number into the file: 2

Enter the number into the file: 1

Data entered into the file.

文件:


2

1


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
達(dá)令說

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是您想要實(shí)現(xiàn)的目標(biāo)的一個(gè)工作變體:


import java.io.*; // Needed for File I/O class.

import java.util.Scanner; // Needed for Scanner class.


public class program01

{

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

    {

        int fileName;

        int num;


        // Create a Scanner object for keyboard input.

        Scanner input = new Scanner(System.in);


        File fname = new File ("path/to/your/file/namef.txt");

        PrintWriter outputfile = new PrintWriter(fname);


        System.out.println("Enter the number of data (N) you want to store in the file: ");


        int N = input.nextInt(); // numbers from the user through keyboard.

        System.out.println("Enter " + N + " numbers below: ");

        for (int i = 1; i <= N; i++)

        {

            // Enter the numbers into the file.

            int tmp = input.nextInt();


            outputfile.print(tmp);

        }

        System.out.println("Data entered into the file.");

        outputfile.close(); // Close the file.

    }

}

對(duì)上面的幾點(diǎn)評(píng)論:


1)刪除多余的行:


 Scanner inputFile = new Scanner(fname);  // Create a Scanner object for keyboard input.

 FileWriter outFile = new FileWriter ("namef.txt", true);

其實(shí)你根本就沒有使用過它們。


2)在PrintWriter我們傳遞File對(duì)象,而不是String。


3)在for循環(huán)中存在邏輯錯(cuò)誤 - 在每次迭代中,您應(yīng)該編寫N而不是用戶在控制臺(tái)上輸入的實(shí)際數(shù)字。


4)另一個(gè)錯(cuò)誤是在最后一行關(guān)閉了錯(cuò)誤的文件。


編輯:根據(jù)評(píng)論添加。在第 2 點(diǎn))中,還有一種替代方法 - 您可以跳過創(chuàng)建File對(duì)象并String直接將其作為路徑傳遞給甚至不存在的文件PrintWriter,如下所示:


PrintWriter outputfile = new PrintWriter("path/to/your/file/namef.txt");


查看完整回答
反對(duì) 回復(fù) 2023-07-28
  • 2 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)