2017-02-08 24 views
0

これは学校向けのプロジェクトです。目的は、ユーザーの入力を読み取ってから、140文字に達するまで文字をランダムに削除して入力を短くするプログラムを作成することです。これまで私が行ってきたことは次のとおりです。現在、1つの文字だけを削除してから実行を停止します。アドバイスありがとうございます文字列からランダムな文字を削除する

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(); 

     } 
+1

をうわー、それは印象的なループだ... – shmosel

答えて

1

ループのフォーマットが間違っています。あなたは使用する必要があります:あなたが前に持っていた何

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(); 

    while (tweetLength > 140) { 
     int characterposition = rand.nextInt(tweetLength); 
     String shorttweet = tweet.substring(0, characterposition); 
     String shorttweet2 = tweet.substring(characterposition + 1); 

     tweet = shorttweet + shorttweet2; 
     System.out.println("Shortented Tweet: " + tweet); 
     tweetLength = tweet.length(); 
    } 

は、それが一度だけ起こった理由だったコードの1つのブロック、続く空do-whileループでした。ループの状態を変更したことにも注意してください。ループの長さが140よりも大きい間はループする必要があります。以下の目的を学ぶための

は、あなたの元のループです:

do { 
    //you didn't do anything inside the loop! 
} while (tweetLength <= 140); 

//all of your code was after the loop 

編集:

ことがint 0間の(包括的)を返しますので、我々はまた、このラインrand.nextInt(tweetLength)を修正するために必要とtweetLength(排他)。これが0を返したら、substring(0, -1)を呼び出しているので次の行が壊れます。この点をPatrickParkerのおかげ

+0

あなたの位置は1でオフになっています。 nextIntが0を返すとjava.lang.StringIndexOutOfBoundsExceptionが発生する可能性があります。 –

+0

@PatrickParker良い点、固定。 – nhouser9

+0

いいえ、最初の文字を削除できなくなりました。 –

1

あなたがより良い操作のこの種のためにはるかに高速である、StringBuilderによってStringを交換したい:

private static String shorten(String str, int length) { 
    StringBuilder sb = new StringBuilder(str); 
    Random rand = new Random(); 
    while (sb.length() > length) { 
     int pos = rand.nextInt(sb.length()); 
     // The deleteCharAt() method deletes the char at the given 
     // position, so we can directly use the retrieved value 
     // from nextInt() as the argument to deleteCharAt(). 
     sb.deleteCharAt(pos); 
    } 
    return sb.toString(); 
} 

をあなたの質問に答えるために:

do-whileループを使用しています。これは次の形式です。

do { 
    // Things to do 
} while (condition); 

このコードの後ろのブロックは、このループとは関係ありません。それはちょうど匿名コードブロックです:

{ 
    // Statements 
} 

だから、最初のあなたの空do-whileループを実行し、それ以下のコード - 一度、当然のは。あなたが代わりにwhileループを使用する必要があります

while (lengthOfString > 140) { 
    // remove a character 
} 
関連する問題