mercredi 9 décembre 2020

How can I get my outputs to print on the same line without repeats?

Here is my code. It's for class and supposed to simulate "dyslexia" by replacing "dbqp" with eachother randomly.

import java.util.Scanner;

public class Dyslexia
{
  public static void main(String[] args)
  {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a String");
    String str = scanner.nextLine();
    String output = "";
    
    String e = null;
    
    for (int i = 0; i < str.length(); i++)
    {
      int randomInt = (int)(Math.random() * 4) + 1; 
      System.out.println(randomInt);
      
      if (randomInt == 1)
      {
        e = "d";
      }
      if (randomInt == 2)
      {
        e = "b";
      }
      if (randomInt == 3)
      {
        e = "q";
      }
      if (randomInt == 4)
      {
        e = "p";
      }
      output = str.replaceAll("[dbqp]", e);
      System.out.println(output); 
    }
  }
}   

The output I am currently getting (lets say I type in qpdb) is:

1
dddd
3
qqqq
3
qqqq
3
qqqq

Ignore the numbers for debugging but given those random numbers the output I would be aiming for is:

1
d
3
q
3
q
3
q



Aucun commentaire:

Enregistrer un commentaire