A program that randomly displays lines from a text file and displays phrases that are not in the text file with a 5% chance.

Asked 2 years ago, Updated 2 years ago, 30 views

I don't know how to combine scanner and random class.

Doraemon program with 5% chance of serving kettle
A list of secret tools is created by a text file and read at the time of starting. In the text, one secret tool name is written in each line.
Every time Enter is entered, a 5% chance of talking "kettle" and a 95% chance of talking the name of one of the honey tools in the list.
Enter "end" to exit the program.
Example) "Small light~" (Tool name +~)
   """Donbura powder~""

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class Question {

    public static void main(String[]args) {
        
        try{
            BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream(filePath), "txt"));         

            String line;
            while((line=br.readLine())!=null){
                // process the line.
            }
            br.close();

        } catch(IOExceptione){
            e.printStackTrace();
        }

java

2022-09-30 15:50

1 Answers

Once you have read from the file, store the string in a list, array, etc.
After that, please use random numbers for each input.
An example implementation based on the code provided is as follows:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Question {


    public static void main(String[]args) {
        String filePath = "items.txt";
        try{
            BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream(filePath)));         

            String line;
            
            // List to store input
            List<String>list=new ArrayList<String>();;
            
            while((line=br.readLine())!=null){
                // list the data in a file line by line
                list.add(line);             
            }
            // Generating Random Objects
            Random rnd = new Random();
            // Generating Scanner Objects
            try(Scanner sc=new Scanner(System.in)){
                while(true){
                    // Line feed (enter a string containing Enter)
                    line=sc.nextLine();
                    if("end".equals(line)){
                        // complete the program by typing end
                        break;
                    } else {
                        if(rnd.nextInt(20)==0){
                            // have a 5% chance of marking the kettle
                            System.out.println("Kettle");
                        } else {
                            // 95% chance of displaying one of the names in the list
                            System.out.println(list.get(rnd.nextInt(list.size()));
                        }
                    }
                }
            }
            
            
            
            br.close();

        } catch(IOExceptione){
            e.printStackTrace();
        }   
    }
}


2022-09-30 15:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.