【问题标题】:Subtracting strings减去字符串
【发布时间】:2013-12-02 15:37:49
【问题描述】:

我被要求创建一个JOptionPane 程序,该程序需要用户想要的任意数量的数字(作为字符串)并将它们加在一起。 我想到了一个这样的伪:

  1. 制作int Total = 0
  2. 以字符串 X 形式接收输入
  3. 循环:for (int i = 0; i<=X.length();i++)
    1. 创建另一个字符串S1,它将数字从开始到第一个空格
    2. S1 转换为数字并将其添加到总计中
    3. X 中减去S1,然后重新开始循环
  4. 显示总数

所以,我的问题是从X 中减去S1

到目前为止我的代码:

public static void main(String[] args) {
int total = 0;
    String x = JOptionPane.showInputDialog("enter nums please");
    for (int i = 0; i<=x.length();i++){
        String s1 = x.substring (0, x.indexOf(' '));
        total += Integer.parseInt(s1);  
        x = x - s1;
    }
    JOptionPane.showMessageDialog(null, "the sum is" + total); }

【问题讨论】:

  • 为什么不用空格分割字符串x?然后解析结果数组中的字符串并求和。
  • 没有数组,没学过。也许还有另一种方法可以做到这一点?
  • 您可以更改子字符串的索引起始索引,将起始索引增加前一个数字的长度等。

标签: java string joptionpane subtraction


【解决方案1】:

如果你还没有学过数组,你可以这样实现:

public static void main(String[] args){
        int total = 0;
        String x = "12 7";
        String s1 = x.trim(); //trim the string
        while(!s1.isEmpty()){ //loop until s1 is not empty
            int index = x.indexOf(' ');//search the index for a whitespace
            if(index != -1){ //we found a whitespace in the String !
                s1 = s1.substring(0, index); //substract the right number
                total += Integer.parseInt(s1); 
                x = x.substring(index+1).trim(); //update the String x by erasing the number we just added to total
                s1 = x; //update s1
            } else {
                total += Integer.parseInt(s1); //when there is only one integer left in the String
                break; //break the loop this is over
            }
        }
        System.out.println(total);
    }

【讨论】:

    【解决方案2】:

    这是@ZouZou 使用的方法的另一种解释,但实际上并没有分解你的字符串,它会记住它已经看过的位置,并沿着字符串工作

    int total = 0;
    String inputString = "12 7 8 9 52";
    int prevIndex = 0;
    int index = 0;
    while (index > -1) {
      index = inputString.indexOf(' ', prevIndex);
      if (index > -1) {
        total += Integer.parseInt(inputString.substring(prevIndex, index));
        prevIndex = index + 1;
      } else {
        total += Integer.parseInt(inputString.substring(inputString.lastIndexOf(' ')+1));
    break;
      }
    }
    System.out.println(total);
    

    【讨论】:

      【解决方案3】:

      简单的解决方案

      int total = 0;
      String x = JOptionPane.showInputDialog("enter nums please");
      
      for (String s : x.split("\\s+")){
          total += Integer.parseInt(s);
      }
      
      System.out.println(total);
      

      编辑:“不能使用数组”- 然后使用Scanner 扫描String 中的nextInt()

      int total = 0;
      String x = JOptionPane.showInputDialog("enter nums please");
      Scanner scanner = new Scanner(x);  // use scanner to scan the line for nextInt()
      
      while (scanner.hasNext()){
          total += scanner.nextInt();
      }
      System.out.println(total);
      

      【讨论】:

      • 我知道,这就是我寻求帮助的原因
      • 查看我的编辑。我原来的答案有点使用了一个数组,但我编辑的解决方案没有。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2018-10-05
      • 2014-01-16
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      相关资源
      最近更新 更多