如何将单词中的第一个字母向前移动一定量,Java

问题描述 投票:0回答:2

我是编码新手,需要一些帮助才能完成作业。它指定我们需要从文件中获取字符串,并将字母向前移动一定量。这是我正在努力的部分:

如果一个单词有n个字母,其中n是奇数,将前(n+1)/2个字母移到单词的末尾。例如:“kitchen”变成“henkitc”。

如果一个单词有n个字母,其中n是偶数,将前n/2个字母移到单词的末尾。例如,“之前”变成“orebef”。

输入文件将如下所示:

之前

厨房

这样的输出:

OREBEF之前

厨房 HENKITC

如果这是一个愚蠢的问题,我们深表歉意。

这是我目前所拥有的:

public static void main(String[] args) throws IOException {
        
        Scanner input;
        FileInputStream inputFile = null;
        FileOutputStream outputFile = null;
        PrintWriter outPrint = null;
        
        inputFile = new FileInputStream("input2.in");
        outputFile = new FileOutputStream("results.out.txt");
        outPrint = new PrintWriter(outputFile);
        input = new Scanner(inputFile);
        
        while (input.hasNext()) {
            String lineReader = null;
            lineReader = input.nextLine();
            if (lineReader.length() % 2 == 0) {
                String evenWord = lineReader;
                String flippedEvenWord = null;
                System.out.print(evenWord);
                System.out.print("  ");
                System.out.println(evenWord.toUpperCase());
                outPrint.print(evenWord);
                outPrint.print("    ");
                outPrint.println(evenWord.toUpperCase());
            }
            else if (lineReader.length() % 2 != 0){
                String oddWord = lineReader;
                System.out.print(oddWord);
                outPrint.print(oddWord);
                outPrint.print("    ");
                System.out.print("  ");
                String flippedOddWord = oddWord;
                System.out.println(flippedOddWord.toUpperCase());
                outPrint.println(oddWord.toUpperCase());
            }
            else {
                break;
            }
        }
        inputFile.close();
        outPrint.close();
        input.close();
    }

    }
java string char
2个回答
0
投票

你在正确的轨道上!以下是如何修改代码以按照作业中指定的方式向前移动字母:

public static void main(String[] args) throws IOException {
        
        Scanner input;
        FileInputStream inputFile = null;
        FileOutputStream outputFile = null;
        PrintWriter outPrint = null;
        
        inputFile = new FileInputStream("input2.in");
        outputFile = new FileOutputStream("results.out.txt");
        outPrint = new PrintWriter(outputFile);
        input = new Scanner(inputFile);
        
        while (input.hasNext()) {
            String lineReader = null;
            lineReader = input.nextLine();
            if (lineReader.length() % 2 == 0) {
                // even length word
                String evenWord = lineReader;
                String shiftedWord = evenWord.substring(evenWord.length()/2) + evenWord.substring(0, evenWord.length()/2);
                System.out.print(evenWord + " ");
                System.out.print(shiftedWord.toUpperCase());
                System.out.println();
                outPrint.print(evenWord + "    ");
                outPrint.println(shiftedWord.toUpperCase());
            }
            else if (lineReader.length() % 2 != 0){
                // odd length word
                String oddWord = lineReader;
                String shiftedWord = oddWord.substring((oddWord.length()+1)/2) + oddWord.substring(0, (oddWord.length()+1)/2);
                System.out.print(oddWord + "  ");
                System.out.println(shiftedWord.toUpperCase());
                outPrint.print(oddWord + "    ");
                outPrint.println(shiftedWord.toUpperCase());
            }
        }
        inputFile.close();
        outPrint.close();
        input.close();
    }

在上面的代码中,对于偶数长度的单词,我们在中间拆分单词,交换两半并将它们连接在一起。对于奇数长度的单词,我们将单词的长度加 1,在中间拆分它,交换两半并将它们连接在一起。

请注意,我还修改了打印语句以匹配作业中指定的输出格式。


0
投票

计算这个的另一种方法(没有额外的代码)是使用

int
总是整数的事实。
所以,加一除以二就可以得到要移动的字符串的长度。
(7+1)/2 = 4
在存储为
4
.
 时保持 
int
(6+1)/2 = 3.5
存储为
3
时变为
int

        while (input.hasNext()) {
            //Remove the "= null" part. It's redundant
            String line = input.nextLine();
            int charactersToMove = (line.length() + 1) / 2;
            String shiftedWord = line.substring(charactersToMove)
                               + line.substring(0, charactersToMove);
            System.out.println(line + " " + shiftedWord.toUpperCase());
            outPrint.println(evenWord + "    " + shiftedWord.toUpperCase());
        }

您也可以使用 % 来计算单行上的偏移量:

int charactersToMove = (line.length()/2) + (line.length() % 2);
or
int charactersToMove = ((line.length() + (line.length() % 2)) / 2);

在这种情况下,

(line.length() % 2)
对于奇数总是
1
,对于偶数总是
0

© www.soinside.com 2019 - 2024. All rights reserved.