我试图通过一个数组来查找某些字符串

时间:2016-05-25 01:54:22

标签: java arrays string

我们必须为我的AP计算机科学课完成一项任务,并且我想出了代码的基础,但我被困在了一部分。如果您想尝试一下,这是完成作业的链接。

LINK:http://codingbat.com/prob/p254067

到目前为止,这是我的代码:

public int numVworthy(String wordsList[]) {
  int count = 0;
  String w = "w";
  String v = "v";

  for(int i = 0; i < wordsList.length; i++) {
    if(wordsList[i].contains(v) && wordsList.length - 1 == 0) {
      count++;
    }

    if(wordsList[i].contains(v) && i == 0) {
      if(!wordsList[i + 1].contains(w)) {
        count++;
      }
    }
    if(wordsList[i].contains(v) && i == wordsList.length - 1) {
      if(!wordsList[i - 1].contains(w)) {
        count++;
      }
    }

  }
  return count;
}

4 个答案:

答案 0 :(得分:0)

您正在使用的语法是incorect

应该是

if(wordsList[i].contains(v)) {

答案 1 :(得分:0)

此代码应该可以使用

public int numVworthy(String wordsList[]) { 
  int count = 0;
  String w = "w";
  String v = "v";

  for(int i = 0; i < wordsList.length; i++) { 
    // first we check the final decision: does the word contain v or not
    if(wordsList[i].contains(v)){ 
      // then we check if there's only one word, since this is a special case, where it'll always count
      if(wordsList.length == 1){
        count++;
      }
      // otherwise we'll see if the previous word contains a w
      // if there is a previous word
      else if(i>0 && !wordsList[i-1].contains(w)){
          // if the previous word doesn't contain a w we'll see if the next
          // word contains a w, if there is a next word
          if((i+1)<wordsList.length && !wordsList[i+1].contains(w)){
            // all conditions are satisfied
            count++;
          // if there isn't a next word all conditions have been satisfied
          } else if(i==(wordsList.length - 1)){
            count++;
          }
       // if there isn't a previous word, the only condition is that the next
       // word doesn't contain a w
        } else if(i==0 && (i+1)<wordsList.length && !wordsList[i+1].contains(w)){
            count++;
      }
    }
  }
  return count;
}

我希望你觉得这很有帮助这个练习是一个贯穿所有场景的问题,在这种情况下:

  1. 只有一个元素,它包含一个v
  2. 有两个元素,第一个包含v
  3. 有两个元素,第二个元素包含v
  4. 有三个或更多元素,第一个元素包含v,第二个元素不包含
  5. 有三个或更多元素,最后一个元素包含一个v而前一个元素不包含
  6. 有三个或更多元素,一个中间元素包含一个v和一个前后的元素

答案 2 :(得分:-1)

试试这个

enter code here

  public int numVworthy(String wordsList[]) {
  int count = 0;
  String w = "w";
  String v = "v";
  bool prevw = false;
  bool flagv = false;
  for(int i = 0; i < wordsList.length; i++) {
     if(!wordsList[i].contains(w) && !prevw) {
       prevw = true;

        }else if(wordsList[i].contains(v) && !flagv && prevw) {
           flagv= true;
       }

        else if(!wordsList[i].contains(w) && flagv)
        {
           count++;
        }
        else
         {
            prevw=nextw=flagv= false;
            i=-2;  // used to recheck this line for prev two that are excaped      beacuse of the prevw flag is true
         }




          }
          return count;
           }

答案 3 :(得分:-1)

此代码工作正常!只使用一个if。

"""
This script will submit current file to deadline for render
"""
import os
import sys
import subprocess
import maya.cmds as cmds


def maya_deadline_job():
    """
    this function will collect scene file information and write a job file
    :return:
    """
    renderer_name = 'File'
    version = cmds.about(version=True)
    project_path = cmds.workspace(q=True, directory=True)
    width = cmds.getAttr("defaultResolution.width")
    height = cmds.getAttr("defaultResolution.height")
    output_file_path = cmds.workspace(expandName="images")
    output_file_prefix = cmds.getAttr("defaultRenderGlobals.imageFilePrefix")
    scene_file = cmds.file(q=True, location=True)
    info_txt = 'Animation=1\n' \
               'Renderer={}\n' \
               'UsingRenderLayers=0\n' \
               'RenderLayer=\n' \
               'RenderHalfFrames=0\n' \
               'LocalRendering=0\n' \
               'StrictErrorChecking=1\n' \
               'MaxProcessors=0\n' \
               'AntiAliasing=low\n' \
               'Version={}\n' \
               'Build=64bit\n' \
               'ProjectPath={}\n' \
               'ImageWidth={}\n' \
               'ImageHeight={}\n' \
               'OutputFilePath={}\n' \
               'OutputFilePrefix={}\n' \
               'Camera=\n' \
               'Camera0=\n' \
               'Camera1=RENDERShape\n' \
               'Camera2=frontShape\n' \
               'Camera3=perspShape\n' \
               'Camera4=sideShape\n' \
               'Camera5=topShape\n' \
               'SceneFile={}\n' \
               'IgnoreError211=0'.format(renderer_name
                                         version,
                                         project_path,
                                         width,
                                         height,
                                         output_file_path,
                                         output_file_prefix,
                                         scene_file)

    maya_deadline_job_file = r'{}\maya_deadline_job.job'.format(os.getenv('TEMP'))
    with open(maya_deadline_job_file, 'w') as job_file:
        job_file.write(info_txt)
    return maya_deadline_job_file


def maya_deadline_info():
    """
    this function will collect maya deadline information and write a job file
    :return:
    """
    info_txt = 'Plugin=MayaBatch\n' \
               'Name=MY_FILE_NAME\n' \
               'Comment=Render Launch by Python\n' \
               'Pool=none\n' \
               'MachineLimit=0\n' \
               'Priority=50\n' \
               'OnJobComplete=Nothing\n' \
               'TaskTimeoutMinutes=0\n' \
               'MinRenderTimeMinutes=0\n' \
               'ConcurrentTasks=1\n' \
               'Department=\n' \
               'Group=none\n' \
               'LimitGroups=\n' \
               'JobDependencies=\n' \
               'InitialStatus=Suspended\n' \
               'OutputFilename0=C:/Users/raijv/Documents/maya/projects/default/images/masterLayer_2.iff.????\n' \
               'Frames=1-10\n' \
               'ChunkSize=1'

    maya_deadline_info_file = r'{}\maya_deadline_info.job'.format(os.getenv('TEMP'))
    with open(maya_deadline_info_file, 'w') as job_file:
        job_file.write(info_txt)
    return maya_deadline_info_file


def submit_to_deadline():
    """
    this function will send current scene to deadline for rendering
    :return:
    """
    deadline_cmd = r"C:\Program Files\Thinkbox\Deadline\bin\deadlinecommand.exe"
    job_file = maya_deadline_job()
    info_file = maya_deadline_info()
    command = '{deadline_cmd} "{job_file}" "{info_file}"'.format(**vars())
    process = subprocess.Popen(command, stdout=subprocess.PIPE)
    lines_iterator = iter(process.stdout.readline, b"")
    #  Lets print the output log to see the Error / Success 
    for line in lines_iterator:
        print(line)
        sys.stdout.flush()

submit_to_deadline()