这是一个针对学校的项目。其目标是创建一个读取用户输入的程序,然后通过随机删除字符来缩短输入,直到达到140个字符。这是我迄今为止所做的。目前,它只删除一个字符,然后停止运行。感谢您的任何建议从字符串中删除随机字符
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the tweet you want to shorten:");
String tweet = null;
tweet = keyboard.nextLine();
int tweetLength = tweet.length();
Random rand = new Random();
do {
} while (tweetLength <= 140); {
int characterposition = rand.nextInt(tweetLength);
String shorttweet = tweet.substring(0, characterposition-1);
String shorttweet2 = tweet.substring(characterposition);
tweet = shorttweet + shorttweet2;
System.out.println("Shortented Tweet: " + tweet);
tweetLength = tweet.length();
}
哇,这是一个令人印象深刻环... – shmosel