字符串B的单词分配

时间:2019-04-26 03:02:02

标签: java arrays string

我必须编写一个程序,其中用户输入5个单词,这些单词必须存储到字符串数组中。用户完成操作后,必须显示输入以字母“ B”开头的单词的次数(小写或大写),并重新声明B个单词。我无法告诉我有多少个'B'单词以及它们是什么。我使用Java,因为我是学生就可以编程,我不使用扫描仪,而所有

    //Asking user to input 5 words
    System.out.println ("Please enter 5 words");
    System.out.println ("====================");

    //set up loop so that x is the index variable going from 0 to 4
    //fill words array with five words
    int fromIndex = 0;
    int counter = 0;

    for (int x = 0 ; x <= 4 ; x = x + 1)
    {
        System.out.print (" ");
        words [x] = keyboardInput.readLine ();

      //change word to lowercase
      String lower = words[x].toLowerCase();
      while(fromIndex !=-1)
        {
        fromIndex = lower.indexOf("b",fromIndex);
        if (fromIndex !=-1) 
          {
          //character was matched
          counter = counter + 1;
          fromIndex++;
          }
        }
    }
    System.out.print ("You entered " + counter + " 'B' words and they were: ");

这就是我得到的:

请输入5个字

食物

比利

窗台

Pop

您输入了0个“ B”字,它们是:

3 个答案:

答案 0 :(得分:1)

您永远不会重置fromIndex,也不需要像这样的fromIndexwhile循环。我会使用String.startsWith(String)。喜欢,

for (int x = 0; x < 5; x++) {
    System.out.print(" ");
    words[x] = keyboardInput.readLine();
    if (words[x].toLowerCase().startsWith("b")) {
        counter++;
    }
}

或者,

if (Character.toLowerCase(words[x].charAt(0)) == 'b') {
    counter++;
}

答案 1 :(得分:0)

您可以在using AventStack.ExtentReports; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; namespace EFComFormsAutomation.EFComTestCases { public class EFComBaseTest { public IWebDriver driver = null; public IWebElement element; public static ExtentTest test; public static ExtentReports extent; [OneTimeSetUp] public void CallExtentReport() { extent = EFCOMExtentReport.GetExtent(); } [SetUp] public void Intiallize() { ChromeOptions options = new ChromeOptions(); //options.AddArgument("--Headless"); // added this condition for running an scripts headless options.AddArguments("--incognito"); options.AddArguments("--start-maximized"); options.AddArgument("no-sandbox"); driver = new ChromeDriver(options); } [TearDown] public void CleanUp() { if (driver != null) { driver.Close(); driver.Quit(); } } [TearDown] protected void ExecutionResult() { var status = TestContext.CurrentContext.Result.Outcome.Status; var stacktrace = TestContext.CurrentContext.Result.StackTrace; var errormessage = TestContext.CurrentContext.Result.Message; if (status == NUnit.Framework.Interfaces.TestStatus.Failed) { string screenshotpath = GetScreenShot.CaptureScreenShot(driver, "FailedTest_Screenshot" + System.DateTime.Now.Ticks.ToString()); test.Log(Status.Fail, stacktrace + errormessage); test.Log(Status.Fail, "Snapshot Below:" + test.AddScreenCaptureFromPath(screenshotpath)); } } [OneTimeTearDown] public void GenerateReportAndMailSender() { extent.Flush(); driver.Dispose(); } } } 上使用indexOf功能。如果是String,则表示您的单词以0开头。

b

答案 2 :(得分:0)

这是完整版本:

public class Bwords {
public static void main(String [] args){
    java.util.Scanner input=new java.util.Scanner(System.in);

    System.out.println ("Please enter 5 words");
    System.out.println ("====================");
    String[] words = new String[5];
    int count=0;
    String  [] b_words=new String[5];


        for (int x = 0; x <= 4; x++) {
            System.out.print(x+1+" word: ");
            words[x] = input.next();
             if (words[x].toLowerCase().startsWith("b")) {
                 count++;
                 b_words[x]=words[x];
             }

    }
    System.out.print ("You entered " + count + " 'B' words and they were: ");

    for (String b_word : b_words) {
        if (b_word != null)
            System.out.print(b_word + " ");
    }
  }
}

输出

请输入5个字

1个字:食物

2个字:Billy

3个字:窗台

4个字:熊

5个字:流行

您输入了2个'B'单词,它们是:Billy Bear