【问题标题】:What's the best way to separate numbers manually for learning algorithm为学习算法手动分离数字的最佳方法是什么
【发布时间】:2021-10-19 10:22:06
【问题描述】:

我正在学习 Java,想知道如何在同一行中获得两个数字。

这个算法可以吗,我可以做些什么改进?你有什么建议?

import java.util.Scanner;

public class Main{
    public static int Separate(String Values, int Order){
        String toReturn = "";
        int Counter = 0;

        for(int Iterator = 0; Iterator < Values.length(); Iterator = Iterator + 1){
            if(Values.charAt(Iterator) == ' ') {
                if(Order == Counter) break;
                else{
                    toReturn = "";
                    Counter = Counter + 1;
                }
            }
            else toReturn += Values.charAt(Iterator);
            
        }

        return Integer.parseInt(toReturn);
    }
    public static void main(String[] args){
        Scanner Entry = new Scanner(System.in);
        System.out.print("Enter two numbers separated by space: ");
        String Number = Entry.nextLine();

        int Frst = Separate(Number, 0);
        int Scnd = Separate(Number, 1);

        
    }
}

【问题讨论】:

  • 所以代码已经工作了?我认为Code Review 更好(询问之前阅读他们的帮助中心)
  • 是的,它正在工作。
  • 如果数字之间有多个空格,您的代码可能不会像预期的那样运行。我建议使用Values.split("\\s+") 来分割至少有一个空格的字符串。
  • 备注:在java中,它是一种广泛使用的约定,即变量名以小写字符开头。
  • 你是对的@MrSmith42。不再使用一个空间。我会尝试修复它。谢谢。

标签: java algorithm function class oop


【解决方案1】:

我可以做些什么改进?你有什么建议?

采用Java Naming Conventions

  1. 方法名称为camelCase,以小写字母开头

  2. 字段和属性名称以及方法参数名称也是camelCase

在 Java 中基本上只有类和接口名称以大写字母开头。

public static int separate(String values, int order){
  String toReturn = "";
  int counter = 0;

  for(int iterator = 0; ...) { ...

否则我会说:这个算法对于初学者来说非常可靠。很容易理解发生了什么。

当然,Java 提供了更复杂的工具来解决这个问题,例如使用 myString.split(...) 的正则表达式或 IntStream intStream = myString.chars() 的 Streams。

最后但并非最不重要的一点是,您可以添加异常处理:如果Integer.parseInt 被赋予一些非数字会发生什么?它会崩溃。

try {
  return Integer.parseInt(toReturn);
} catch (NumberFormatException e) {
  // when "toReturn" cannot be parsed to an int, return a
  // default value instead of crashing your application
  return 0;
}

或者如果崩溃是期望的行为,或者您可以确保永远不会使用非法字符串调用此方法,请保持原样(= 不要添加 try catch

【讨论】:

    【解决方案2】:

    我认为您所做的对于格式良好的输入非常有用,在数字之间有一个空格字符。正如其他人指出的那样,遵循 Java 命名约定将大大提高代码的可读性。

    处理可能在数字之前、之间和之后的空格字符序列有点棘手。一般的模式是使用任何空格序列,记住当前位置,使用数字序列,然后如果我们在正确的位置返回解析的数字。

    public static int separate(String str, int order) 
    {
        for(int i = 0, pos = 0; ; pos++)
        {
            while(i < str.length() && str.charAt(i) == ' ') i += 1;
      
            int j = i;
            while(i < str.length() && str.charAt(i) != ' ') i += 1;
           
            if(i == j) throw new IllegalStateException("Missing number!");
            
            if(order == pos) 
            {
                // handle NumberFormatException
                return Integer.parseInt(str.substring(j, i));       
            }
        }
    }
    

    测试:

    String s = "  23432   798  44";
    for(int i=0; i<3; i++)
        System.out.print(separate(s, i) + " ");
    

    输出:

    23432 798 44 
    

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2013-06-25
      • 2015-01-13
      • 2018-09-29
      • 1970-01-01
      相关资源
      最近更新 更多