Используя Google Guava-библиотеку, мы можем легко создать и записать файл.
package com.zetcode.writetofileex;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
public class WriteToFileEx {
public static void main(String[] args) throws IOException {
String fileName = "fruits.txt";
File file = new File(fileName);
String content = "banana, orange, lemon, apple, plum";
Files.write(content.getBytes(), file);
}
}
В примере создается новый файл fruits.txt
в корневом каталоге проекта.
При просмотре вашего кода egghead правильно заявляет, что вы не инициализировали свой массив. Но после этого мне пришлось изменить несколько других вещей в вашем коде, чтобы заставить его работать.
Module Module1
Sub Main()
Dim Counter As Integer = 1
Dim Numbers(1000) As Integer 'Initialized the Array so it will be usable.
Dim NumbersCounter As Integer = 0
Dim Total As Integer = 0
While (Counter <= 1000)
If (Counter Mod 3 = 0) Then
Numbers(NumbersCounter) = Counter
NumbersCounter = NumbersCounter + 1
Counter = Counter + 1
ElseIf (Counter Mod 5 = 0) Then
Numbers(NumbersCounter) = Counter
NumbersCounter = NumbersCounter + 1
Counter = Counter + 1
Else
Counter = Counter + 1
End If
End While
Counter = 0
While (Counter <= Numbers.Length - 1) ' Arrays are zero based so you need to subtract 1 from the length or else you will overflow the bounds
If (Counter = 0) Then
Total = Numbers(Counter)
Counter = Counter + 1
Else
Total = Total + Numbers(Counter) 'You were multiplying here not adding creating a HUGE number
Counter = Counter + 1
End If
End While
Console.WriteLine(Total) 'Changed PrintLine which prints to a file to Console.WriteLine which writes to the screen
Console.ReadLine 'Added a Console.ReadLine so the Window doesn't close until you hit a key so you can see your answer
End Sub
End Module
Вам нужно выделить память массиву Numbers, и поскольку размер известен изначально, вы можете выделить при объявлении:
Dim Numbers (1000) As Integer