簡體   English   中英

反轉句子中的單詞

[英]Reverse the words in a sentence

我在文本文件中有以下數據

5
this is a test
foobar
all your base
class
pony along

這里的第一個數字表示我輸入的行數。 遵循數據線。 在這里,我想顛倒句子的話。

Eg:
this is a test

被顛倒過來

test a is this

當我運行以下代碼

String input = "This is interview question.";
String output = "";
String[] array = input.split(" ");
for(int i = array.length-1; i >= 0; i--)
{
    output =output+ array[i];
    if (i != 0) 
    { 
        output =output+ " ";
    }
}
System.out.println(output);

我得到正確的輸出,但是從文件讀取時卻是另外一回事。

我正在使用以下代碼查看文本文件中的數據。

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class Dummy {
    public static void main(String args[]) throws Exception {
//        Scanner s = new Scanner(new File("D:/GC/Store Credit/A-small.in"));
        Scanner s = new Scanner(new File("D:/GC/Reverse/B-small.in"));

        while (s.hasNextLine()){
            System.out.println(s.nextLine());
        }

        s.close();
    }
}

它根據我的文本文件給出輸出。

類似地,當我嘗試運行上面的代碼以條件來反轉字符串時,它給了我一個錯誤的輸出

我使用的代碼是

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;


public class RevSent {
    public static void main(String[] args) throws Exception {
        File file=new File("D:/GC/Reverse/B-Small.txt");
        FileWriter fw=new FileWriter(file);
        BufferedWriter bw=new BufferedWriter(fw);
        Scanner sc=new Scanner(new File("D:/GC/Reverse/B-small.in"));
        int testCaseCount = Integer.parseInt(sc.next());
        System.out.println("Test cases are:"+testCaseCount);

        for(int i=0;i<testCaseCount;i++)
        {
             while (sc.hasNextLine()){
                    String input=sc.nextLine();
                    System.out.println(input);
                    String output="";
                    String[] array=input.split(" ");
                    for(int j=array.length-1;j>0;j--){
                        output+=array[j];
                    }
                    System.out.println(output);
                }
            }
        sc.close();
    }
}

我得到的輸出是

Test cases are:5


this is a test
testais
foobar

all your base
baseyour
class

pony along
along

請讓我知道我哪里錯了。 謝謝你我所做的改變和代碼工作正常,但我遇到了另一種情況,我要在結果之前添加案例編號(測試案例編號),但在我的代碼中,案例編號仍然是只有1.代碼如下。

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;


public class RevSent {

    public static void main(String[] args) throws Exception {
        File file=new File("D:/GC/Reverse/B-Small.txt");
        FileWriter fw=new FileWriter(file);
        BufferedWriter bw=new BufferedWriter(fw);
        Scanner sc=new Scanner(new File("D:/GC/Reverse/B-small.in"));
        int testCaseCount = Integer.parseInt(sc.next());
//      System.out.println("Test cases are:"+testCaseCount);

        for(int i=0;i<testCaseCount;i++)
        {
            String output="";
                    String input=sc.nextLine();
                    String[] array=input.split(" ");
                    for(int j=array.length-1;j>=0;j--){
                        output+=array[j]+" ";

                    }

                    bw.write("Case #" + (i+1) + ": " + output + "\r\n");
             System.out.println("Case #" + (i+1) + output+"\r\n");
            }
        bw.close();
        sc.close();
    }
}

輸出如下,還有一個多余的情況,沒有結果。

Case #1 

Case #2test a is this 

Case #3foobar 

Case #4base your all 

Case #5class 

謝謝

編輯更新了最新代碼。 請嘗試以下方法:

public static void main(String[] args) throws Exception {
    File file = new File("D:/GC/Reverse/B-Small.txt");
    FileWriter fw = new FileWriter(file);
    BufferedWriter bw = new BufferedWriter(fw);
    Scanner sc = new Scanner(new File("D:/GC/Reverse/B-small.in"));
    int testCaseCount = Integer.parseInt(sc.next());
    //System.out.println("Test cases are:"+testCaseCount);
    int i = 0;

    while (sc.hasNextLine()) {
        String input = sc.nextLine();
        //System.out.println(input);
        String output = "";
        String[] array = input.split(" ");
        for (int j = array.length - 1; j >= 0; j--) {
            output += array[j] + " ";
        }
        if (!"".equals(output)){
            bw.write("Case #" + (i + 1) + ": " + output.trim() + "\r\n");
        }

    }

    bw.close();
    sc.close();
}

您在第二個代碼段中的for循環條件不正確。 它應該是:

for(int j=array.length-1; j >= 0; j--) {

您還必須在輸出中附加一個空白:

output += array[j] + " ";

我還建議使用StringBuilder來組合輸出字符串。

這是你的固定程序。

您的主要問題是:
1)for循環內的嵌套while循環
2)您沒有在單詞之間添加空格的事實

附帶說明一下,有一個不錯的算法
將O(N)中句子的單詞反轉。
反轉字符串中單詞的順序

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class RevSent {
    public static void main(String[] args) throws Exception {
        File file = new File("D:/GC/Reverse/B-Small.txt");
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);
        Scanner sc = new Scanner(new File("D:/GC/Reverse/B-small.in"));
        int testCaseCount = Integer.parseInt(sc.nextLine());
        System.out.println("Test cases are:" + testCaseCount);

        for (int i = 0; i < testCaseCount; i++) {
            String input = sc.nextLine();
            System.out.println(input);
            String output = "";
            String[] array = input.split(" ");
            for (int j = array.length - 1; j >= 1; j--) {
                output += array[j] + " ";
            }
            output += array[0];
            System.out.println(output);
            bw.write(output);
        }
        sc.close();
        bw.close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM