簡體   English   中英

逐個字符地讀取文件,然后將它們放入java中的for循環模式

[英]Reading a File character by character then putting them into a for loop pattern in java

我目前正在研究一個小型的循環模式,我偶然發現了我的項目中的路障。 基本上,我想要的是從我的.java文件中創建一個for循環模式,該文件通過char讀取源char並替換當前for循環模式中的星號:

變成這樣的事情

在此輸入圖像描述

這是我所擁有的for循環模式的當前代碼

for( i = 1; i <= 5; ++i, z = 0) { // first line
    for(int space = 1; space <= segments - i; ++space) {
        System.out.print("  ");
    }

    while(z != 2 * i - 1) {
        System.out.print("* ");
       z++;
    }

    System.out.println();
}   
for( i = 1; i <= 10; ++i, z = 0) { // second line
    for(int space = 1; space <= segments - i; ++space) {
        System.out.print("  ");
    }

    while(z != 2 * i - 1) {
        System.out.print("* ");
       z++;
    }

    System.out.println();
} 

提前致謝!

這是關於如何通過char讀取文件char的示例,

try {
    BufferedReader r=new BufferedReader(new FileReader("file.txt"));
    int ch;
    while((ch=r.read())!=-1){
        System.out.print((char)ch);
    }
} catch(Exception e) {
    System.out.print((char)ch);
}

要在代碼中實現它,只需將此while循環放在asterisk的while循環中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM