检查字符串

时间:2017-12-24 16:33:36

标签: java loops

注意:我知道有很多方法可以使这更简单,但是不允许这样做。我受限于简单,基本的java,循环和手写方法。 甚至数组都不允许。还有.gegex。

任务是检查句子每个单词中的数字,找到同一时间POWER 3的最大数字。 我在这里做了所有事情并且工作正常,直到我输入这样的东西。

  

asdas8 dasjkj27 asdjkj64 asdjk333 asdjkj125

我收到输出64而不是125,因为当它到达第一个数字时它会停止检查,而不是3的幂。

如何继续迭代直到句子结束并避免在达到3号码的非幂时停止,如何修改此代码以达到此目的?

编辑:但如果我在失败状态之后输入多个单词,它就会正常工作。

例如:

  

asdas8 dasjkj27 asdjkj64 asdjk333 asdjkj125 asdash216

这是我的代码:

public class Nine {

static int num(String s) { // method to change string to int
    int b = 0;
    int o = 0;
    for (int i = s.length() - 1; i >= 0; i--) {
        char bi = s.charAt(i);
        b += (bi - '0') * (int) Math.pow(10, o);
        o++;

    }

    return b;
}

static boolean thirdPow(int a) {
    boolean ntrec = false;
    if (Math.cbrt(a) % 1 == 0)
        ntrec = true;
    return ntrec;

}



static int max(int a, int b) {
    int max= 0;
    if (a > b)
        max= a;
    else
        max= b;
    System.out.print(max);
    return max;
}

static String search(String r) {
    String current= ""; // 23aa64
    String currentA= "";
    String br = ""; // smjestamo nas broj iz rijeci 23
    int bb = 0; // nas pretvoreni string u broj
    int p = 0;

    for (int i = 0; i < r.length(); i++) {

        current+= r.charAt(i);
        if (r.charAt(i) == ' ') { 
            for (int j = 0; j < current.length(); j++) {
                while ((int) current.charAt(j) > 47 && (int) current.charAt(j) < 58) {
                    br += current.charAt(j); 
                    j++;
                }
                bb = num(br);
                System.out.println("Third pow" + thirdPow(bb));

                if (thirdPow(bb)) {
                    p = max(p, bb);

                }

                br = "";

            }

            current= "";

        }

    }

    String pp = "" + p;
    String finalRes= "";
    for (int u = 0; u < r.length(); u++) {
        currentA+= r.charAt(u);
        if (r.charAt(u) == ' ') {
            if (currentA.contains(pp))
                finalRes+= currentA;
            currentA= "";
        }
    }
    System.out.println(p);
    return finalRes;

}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter sentence: ");
    String r = scan.nextLine();

    System.out.println("Our string is : " + search(r));

}
  }

4 个答案:

答案 0 :(得分:2)

我假设每个单词都由空格分隔并包含非整数。

使用正则表达式肯定会降低代码复杂性,让我们试试这段代码: -

String input = "asdas8 dasjkj27 asdjkj64 asdjk333 asdjkj125";

String[] extractWords = r.split(" ");        //extracting each words

int[] numbers = new int[extractWords.length]; // creating an Integer array to store numbers from each word

int i=0;

for(String s : extractWords) {
        numbers[i++] = Integer.parseInt(s.replaceAll("\\D+", "")); // extracting numbers
}

现在,&#34;数字&#34;数组将包含[8,27,64,333,125]

您可以使用逻辑在其中找到最大值。希望这会有所帮助。

答案 1 :(得分:0)

兄弟使用

*string.split(" ");*

形成一个字符串数组,然后遍历数组并使用正则表达式解析数字

^[0-9]

\d+

然后从数组中找到最大的数字就这么简单。兄弟一步一步地进行,然后你的代码运行得更快。

答案 2 :(得分:0)

你可以做我正在做的事情。首先将句子分成大块的单词。我是根据空格来做的,因此是in.split("\\s+")。然后从这些单词中找到数字。在这些数字上,只有当它是3的幂时才检查最高数字。

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static boolean isPowOfThree(int num)
{
    int temp = (int)Math.pow(num, 1f/3);
    return (Math.pow(temp, 3) == num);
}

public static void main (String[] args) throws java.lang.Exception
{
    Scanner sc = new Scanner(System.in);
    String in = sc.nextLine();
    String[] words = in.split("\\s+");
    String maxWord = ""; //init default word
    int maxNum = -1; //init default num

    for(String word : words)
    {
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher(word);
        while (m.find()) 
        {
          String num = m.group();
          if(isPowOfThree(Integer.parseInt(num)))
          {
            if(Integer.parseInt(num) > maxNum)
            {
                maxNum = Integer.parseInt(num);
                maxWord = word;
            }
          }
        }
    }

    if(maxNum > -1)
    {
        System.out.println("Word is : " + maxWord);
    }
    else
    {
        System.out.println("No word of power 3");
    }

}
}

答案 3 :(得分:0)

使用Java中的Matcher和Pattern API使用\\d+正则表达式可以解决问题。

package com.company;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        String i = "asdas8 dasjkj278 asdjkj64 asdjk333 asdjkj125";
        Matcher matcher = Pattern.compile("\\d+").matcher(i);

        List<Integer> numbers = new ArrayList<>();
        while (matcher.find()){
            numbers.add(Integer.parseInt(matcher.group()));
        }

        Collections.sort(numbers);
        Collections.reverse(numbers);

        Integer power3 = 0;

        for (Integer n : numbers) {
            if (isPowOfThree(n)) {
                power3 = n;
                break;
            }
        }
        System.out.println(power3);
    }

    static boolean isPowOfThree(int num) {
        int temp = (int)Math.pow(num, 1f/3);
        return (Math.pow(temp, 3) == num);
    }
}

使用\\d+正则表达式时,我们会在while(matcher.find())的每次迭代中获取给定字符串中的所有数字。收集给定字符串中的所有数字后,我们需要对集合进行反向排序。如果我们迭代这个集合,我们找到的第一个数字是3的幂的最大数字,因为集合已经按降序排序。