簡體   English   中英

為什么即使txt文件存在,代碼也不會寫入txt文件

[英]Why won't the code write to the txt file, even though the txt file exists

我編寫了一個程序來隨機吐出字符,直到找到正確的單詞。 例如,如果我輸入“hello”這個詞,它會隨機吐出五個字母的單詞,直到碰到“hello”。 我在控制台中打印了所有猜測,目前正在運行; 但是,我也希望它寫在一個 txt 文件中,但這是行不通的。 它不會打印 txt 文件中的猜測。 我什至檢查以確保我有正確的文件。 這是我的代碼:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class RandomeWords {

    public static void main(String[] args) throws IOException {
        File file = new File("hey.txt");
        if(file.exists())
            System.out.println("The File Exists");
        FileWriter writer = new FileWriter(file);
        writer.write("go");
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("Enter Words");
        String words = scanner1.nextLine();
        int letters = words.length();
        
        for(int i1 = 1; i1 > 0; i1++) {
            

            if(words.contains(" "))
            {
                
                String[] alpha = " abcdefghijklmnopqrstuvwxyz".split("");
            
                StringBuilder sb = new StringBuilder("");
                for( int i3 = 0; i3 < letters; i3++) {
                sb.append(alpha[(int)(Math.random()*72)]);
                }
                
                String finall = sb.toString();
                
                
                
                if(!finall.equals(words)) {
                    System.out.println(finall);
                    
                     writer.write("\n" +finall);
                }
                
                if(finall.equals(words)) {
                    System.out.println(finall);
                    System.out.println(i1);
                    System.out.println("it took " +i1 + " tries to randomly find the word.");
                    System.out.println("It should have taken " +Math.pow((int) 27, letters) +" times, statistacally");
                    
                    System.out.println("The difference of statistically, and with the simulation is " +(int)Math.abs((int)i1 - (int)Math.pow(27, letters)));
                    System.exit(0);
                }
                }
            
             
                if(!words.contains(" ")){
                    String[] alpha1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*?".split("");
                    StringBuilder sb1 = new StringBuilder("");
                    for(int i2 = 0; i2 < letters; i2++) {
                        sb1.append(alpha1[(int)(Math.random()*71)]);
                    }
                    String finall1 = sb1.toString();
                    
                    if(!finall1.equals(words))
                        System.out.println(finall1);
                    
                    if(finall1.equals(words)) {
                        System.out.println(finall1);
                        System.out.println(i1);
                        System.out.println("it took " +i1 + " tries to randomly find the word.");
                        System.out.println("It should have taken " +Math.pow((int) 26, letters) +" times, statistacally");
                        
                        System.out.println("The difference of statistically, and with the simulation is " +Math.abs((int)i1 - (int)Math.pow(26, letters)));
                        System.exit(0);
                        
                    }
                    
                    }
                
            
            

                    
            
                
        
            
        }
//      writer.close();
        
    }}
        

有人可以幫忙嗎? 謝謝。

看起來writer.write("\n" +finall)僅在 if 塊內部調用if(words.contains(" ")) 僅當輸入包含空格時才能到達此代碼。

如果輸入不包含空格,也應該在另一個 if 塊if(.words.contains(" "))中調用writer.write

換句話說,替換

if(!finall1.equals(words))
    System.out.println(finall1);

if (!finall1.equals(words)) {
    System.out.println(finall1);
    writer.write("\n" + finall1);
}

暫無
暫無

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

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