简体   繁体   中英

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

You're doing a replaceAll call which will just set them all to the same value. If you want each character to be considered separately, you need to evaluate and select at random for each character then rebuild a new String with it.

import java.util.List;
import java.util.Random;

public class DyslexiaExample
{
  public static void main(String[] args) {
    System.out.println(simulateDyslexia("Some possible bountiful quotes."));
  }
  
  public static String simulateDyslexia(String sentence) {
    List<Character> vals = List.of('d', 'b', 'q', 'p');
    StringBuilder builder = new StringBuilder();
    
    Random random = new Random();
    
    for(char c : sentence.toCharArray()) {
      if(vals.contains(c)) {
        builder.append(vals.get(random.nextInt(vals.size())));
      }
      else {
        builder.append(c);
      }
    }
    
    return builder.toString();
  }
}

Sample output: Some bossible dountiful buotes.

If you want some consistency between which letter gets swapped with another letter, then consider generating a Map .

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DyslexiaExample
{
  public static void main(String[] args) {
    System.out.println(simulateDyslexia("And quite possibly even more boisterous bountiful quotes."));
  }
  
  public static String simulateDyslexia(String sentence) {
    List<Character> vals = List.of('d', 'b', 'q', 'p');
    StringBuilder builder = new StringBuilder();
    
    List<Character> shuffleVals = new ArrayList<>(vals);
    Collections.shuffle(shuffleVals);
    
    Map<Character, Character> swapMap = new HashMap<>();
    for(int i = 0; i < vals.size(); i++)
      swapMap.put(vals.get(i), shuffleVals.get(i));
    
    for(char c : sentence.toCharArray()) {
      if(swapMap.keySet().contains(c)) {
        builder.append(swapMap.get(c));
      }
      else {
        builder.append(c);
      }
    }
    
    return builder.toString();
  }
}

Sample output: Anb puite dossiqly even more qoisterous qountiful puotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM