D
D
Denis Akhunov2021-03-04 22:02:29
Kotlin
Denis Akhunov, 2021-03-04 22:02:29

Why is only the answer displayed in the file?

Condition:
Create a file f whose components are integers.
Find: what numbers does it contain more: positive or negative?
The code:

import java.io.File
fun main() {
    val fileName = "f.txt"
    val file = File(fileName)
    println("Введите диапазон A: ")
    val a = readLine()!!.toInt()
    println("Введите диапазон B: ")
    val b = readLine()!!.toInt()
    if ( a < b )
    {
        var positive = 0
        var negative = 0
        println("Введите количество чисел: ")
        val count = readLine()!!.toInt()
        val massive = IntArray(count)
        file.writeText("Исходный массив:")
        for ( i in 0 until count )
        {
            massive[i] = (a..b).random()
            file.writeText("${massive[i]} ")
            when
            {
                massive[i] > 0 -> positive++
                massive[i] < 0 -> negative++
            }
        }
        file.writeText("Количество положительных: $positive")
        file.writeText("Количество отрицательных: $negative")
        when
        {
            positive > negative -> file.writeText("В массиве положительных чисел больше отрицательных")
            positive < negative -> file.writeText("В массиве отрицательных чисел больше положительных")
            positive == negative -> file.writeText("В массиве одиннаковое количество положительных и отрицательных чисел")
        }
    }
        else println("Введен не правильный диапазон.")
}

60412ec1f41ae902233978.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
foonfyrick, 2021-03-05
@DeNySzZz

Almost did the right thing, you just need to replace file with FileWriter and set append=true.
It also incorrectly reads values ​​from the file, sometimes there are 1 less of them.

fun main() {
    val fileName = "f18.txt"
    val file = File(fileName)
    val fw =FileWriter(file,true)
    println("Введите диапазон A: ")
    val a = readLine()!!.toInt()
    println("Введите диапазон B: ")
    val b = readLine()!!.toInt()
    if ( a < b )
    {
        var positive = 0
        var negative = 0
        println("Введите количество чисел: ")
        val count = readLine()!!.toInt()
        val massive = IntArray(count)
        fw.appendln("Исходный массив:")
        fw.flush()
        for ( i in 0 until count )
        {
            massive[i] = (a..b).random()
            fw.appendln("${massive[i]} ")
            when
            {
                massive[i] > 0 -> positive++
                massive[i] < 0 -> negative++
            }
        }
        fw.appendln("Количество положительных: $positive")
        fw.flush()
        fw.appendln("Количество отрицательных: $negative")
        fw.flush()
        when
        {
            positive > negative -> fw.appendln("В массиве положительных чисел больше отрицательных")
            positive < negative -> fw.appendln("В массиве отрицательных чисел больше положительных")
            positive == negative -> fw.appendln("В массиве одинаковое количество положительных и отрицательных чисел")
        }
        fw.flush()

    }
    else println("Введен не правильный диапазон.")
    fw.close()
}

K
Kirill Romanov, 2021-03-04
@Djaler

Because the File.writeText method overwrites the contents of the file, not appends ( https://kotlinlang.org/api/latest/jvm/stdlib/kotli... )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question