我的任务是争夺一些人 [英] my Assignment about Scramble some leter

查看:53
本文介绍了我的任务是争夺一些人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此作业涉及对由大写字母组成的字符串的推理。您将实现出现在同一个类中的几个静态方法(未显示)。以下是详细信息。

1.第一种方法采用单个字符串参数并返回该字符串的加扰版本。加扰过程从单词的第一个字母开始,从左到右继续。如果两个连续的字母由A后跟一个不是A的字母组成,那么这两个字母将在结果字符串中交换。一旦交换了两个相邻位置的字母,这两个位置都不会涉及未来的交换。

public static String scrambleWord(String word)

该方法接受一个给定的单词(一个空字符串或一个只包含大写字母的字符串)并返回一个包含一个字母的字符串根据上面给出的规则对该单词进行加扰。下表显示了几个单词及其加扰版本的例子。

原字加扰后

TANTNA

ABRACADABRA BARCADABARA

WHOAWHOA

AARDVARKARADVRAK

鸡蛋蛋

AA





我的代码是>>我知道这是错的,但请求帮助我>>

This assignment involves reasoning about strings made up of uppercase letters. You will implement several static methods that appear in the same class (not shown). Here are the details.
1. The first method takes a single string parameter and returns a scrambled version of that string. The scrambling process begins at the first letter of the word and continues from left to right. If two consecutive letters consist of an "A" followed by a letter that is not an "A", then the two letters are swapped in the resulting string. Once the letters in two adjacent positions have been swapped, neither of those two positions can be involved in a future swap.
public static String scrambleWord(String word)
The method takes a given word (an empty string or a string containing only upper case letters) and returns a string that contains a scrambled version of the word according to the rules given above. The following table shows several examples of words and their scrambled versions.
Original word After scrambling
"TAN" "TNA"
"ABRACADABRA" "BARCADABARA"
"WHOA" "WHOA"
"AARDVARK" "ARADVRAK"
"EGGS" "EGGS"
"A" "A"
"" ""

my code is >> i know it is wrong but pleas help me>>

package assignments;
import java.util.*;
import java.*;

public class ScrambleWord
{
   public static void main(String[] args)
   {
      List<String> strList=new ArrayList<String>();
      strList.add("TAN");
      strList.add("ABRACADABRA");
      strList.add("WHOA");
      strList.add("EGGS");
      strList.add("A");
      strList.add("");
		
      System.out.println(MainMethod.scrambleWordMeth(strList));
   }
}
	
class MainMethod
{
   public static String scrambleWordMeth(List<String> strList)
   {
      int curr=0;
      String res="";
      String currentString = strList.get(curr);
      while (curr<strList.size())
      {
         for(int i = 0; i < currentString.length(); i++)
         {
            if (currentString.charAt(i) == 'A' && !(currentString.charAt(i+1)=='A'))
            {
               res=res+currentString.substring(curr+1,curr+2);
               res=res+'A';
               curr=curr+2;
            }
            else
            {
               res= res+currentString.substring(curr,curr+1);
               curr++;
            }
         }

         if (curr<strList.size())
         {
            res=res+currentString.charAt(curr);
            //res=res+strList.substring(curr);
         }
      }

      return res;      
   }
}





从非解决方案2复制的其他信息

问题是..在数组中将是多个(Char),我想要的是Scramble里面的字母A>>



从非解决方案5中复制的其他信息

i做了但是我不知道为什么它仍然不起作用





additional information copied from non-solution 2
the question is .. in the array will be more than one (Char) and what i want is Scramble just letter A inside it >>

additional information copied from non-solution 5
i did it but i do not why it is still does not work

public static void main(String[] args)
{
    List<string> strList=new ArrayList<string>();
    strList.add("TAN");
    strList.add("ABRACADABRA");
    strList.add("WHOA");
    strList.add("EGGS");
    strList.add("A");
    strList.add("");
    System.out.print(MainMethod.scrambleAllwords(strList));
    }
}

class MainMethod
{
   public static void scrambleAllwords(List<string>strList)
   {
      for(int i=0;i<strlist.size();i++)
      {
         String scrabled=scrambleWordMeth(strList.get(i));
         strList.set(i,scrabled);
      }
   }

   public static String scrambleWordMeth(String strList)
   {
      char[] letters=strList.toCharArray();
      int n=letters.length-1;

      for(int i=0;i<n;i++)
      {
         char tmp= letters[i+1];
         if (letters[i]=='A'&& !(letters[i+1]=='A'))
         {
            letters[i+1]=letters[i];
            letters[i]=tmp;
         }
            return new String(letters);
      }
      return new String(letters);
   }
}





从非解决方案6中复制的其他信息

i尝试过但不起作用任何人都可以帮我解决这个请求



additional information copied from non-solution 6
i tried but it dose not work can anyone help me with that pleas

public static String scrambleWordMeth(String strList)
{
   char[] letters=strList.toCharArray();
   int n=letters.length-1;
   int i=0;
   while(i<n)
   {
      char tmp= letters[i+1];
      if (letters[i]=='A'&& !(letters[i+1]=='A'))
      {
         letters[i+1]=letters[i];
         letters[i]=tmp;
      }
      else
      {
         letters[i]+=letters[i+1];
      }
      return new String(letters);	
   }
}

推荐答案

好的 - 首先要注意的是你会跑掉结束字符串:

OK - the first thing to note is that you will run off the end of the string:
for(int i = 0; i < currentString.length(); i++){

    if (currentString.charAt(i) == 'A' && !(currentString.charAt(i+1)=='A')){



如果字符串中有1个字符,则输入循环, i 将为零。哪个没问题,如果你没有尝试使用 i + 1 ...

所以你需要改变终止条件你的循环。



除此之外 - 你注意到它有什么问题?


If you have 1 character in the string, then the loop will be entered, and i will be zero. Which is fine, if you didn't try to use i + 1 as well...
So you need to change the termination condition of your loop.

Other than that - what have you noticed that is wrong with it?


我很感激你在使用中的机智所有可用的资源来解决你的问题。

然而,要求别人为你做任务不会让你成为更好的程序员。



我建议花一些时间在调试器中分析现有代码的问题并相应地修改它。它可能需要更长时间,但它是值得的...
I appreciate your resourcefulness in using all available resources to solve your problem.
However, asking others to do your assignments for you isn't going to make you a better programmer.

I recommend spending some time in the debugger analysing the problems with your existing code and modifying it accordingly. It may take longer, but it'll be worth it...


问题是..在数组中将不止一个(Char),我想要的是Scramble just letter在里面>>
the question is .. in the array will be more than one (Char) and what i want is Scramble just letter A inside it >>


这篇关于我的任务是争夺一些人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆