【问题标题】:How to get rid of that pesky leading plus sign?如何摆脱那个讨厌的前导加号?
【发布时间】:2015-12-02 19:54:32
【问题描述】:

我有一个基本上从文本文件中读取的代码。输出时有一个前导加号,因为答案是循环打印的。我如何摆脱那个单数的前导加号?我能想到的就是将 ENTIRE THING 转换为字符串,然后取出前三个索引,但这太复杂了。有没有一种简单的方法来重新排列逻辑并做到这一点?

我的代码:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package importtextfiles;

/**
 *
 * @author Hana
 */
import java.io.*;
import java.util.*;
public class InputNumData {
    public static void main(String args[]) throws IOException
    {
        Scanner sf = new Scanner(new File("C:\\Users\\Hana\\SkyDrive\\CompSci\\Programming\\importTextFiles\\meow.txt"));
        int maxIndx = -1; //so the first index is 0

        String text[] = new String[1000];

        while(sf.hasNext()){
            maxIndx++;
            text[maxIndx] = sf.nextLine();
            System.out.println(text[maxIndx]);
        }

        sf.close();

        String answer = "";
        int sum;

        for(int j=0; j<=maxIndx; j++){
            Scanner sc = new Scanner(text[j]);
            sum = 0;
            answer = "";

            while(sc.hasNext()){
                int i = sc.nextInt();
                answer = answer + " + " + i;
                sum = sum + i;
            }
            answer = answer + " = " + sum;
            System.out.println(answer);
        }
    }
}

我的输出:

run:
12 10 3 5
18 1 5 92 6 8
2 9 3 22 4 11 7
 + 12 + 10 + 3 + 5 = 30
 + 18 + 1 + 5 + 92 + 6 + 8 = 130
 + 2 + 9 + 3 + 22 + 4 + 11 + 7 = 58
BUILD SUCCESSFUL (total time: 0 seconds)

喵喵.txt:

12 10 3 5
18 1 5 92 6 8
2 9 3 22 4 11 7

【问题讨论】:

    标签: java arrays java.util.scanner


    【解决方案1】:

    只需将此行更改为

    answer = answer.isBlank() ? i : answer + " + " + i;
    

    有关其工作原理的更多详细信息,请参阅this

    【讨论】:

      【解决方案2】:

      使用您的 while 循环,并修复第一个值:

      //Set up first value
      int i = sc.nextInt();  //might want to check for hasNext() here 
      answer = i;
      sum = sum + i;
      
      while(sc.hasNext())
      {
         i = sc.nextInt();
         answer = answer + " + " + i;
         sum = sum + i;
      }
      

      【讨论】:

        【解决方案3】:

        首先不要在循环中使用连接。比如:

        String result = ""
        for (...) {
            result = result + "some additonal data";
        }
        

        创建几个中间字符串对象,这是不好的做法。它应该替换为:

        StringBuilder sb = new StringBuilder();
        for (...) {
            sb.append( "some additional data" );
        }
        result = sb.toString();
        

        这允许您在完成追加之前添加字符串而不创建新的字符串对象。

        现在我们使用的是StringBuilder,对于初始加号问题,您可以有多种解决方案。第一个也适用于非推荐的字符串连接,它是保留一个标志,告诉您这是否是“第一个操作数”。将您的 while 循环更改为:

        StringBuilder sb = new StringBuilder();
        boolean firstOperand = true;
        
        while(sc.hasNext()){
            int i = sc.nextInt();
            if ( firstOperand ) {
                firstOperand = false;
            } else {
                sb.append( " + " );
            }
            sb.append( i );
            sum = sum + i;
        }
        answer = sb.toString();
        

        使用StringBuilder 可能的另一种方法是在完成循环后删除额外的" + "。在这种情况下,最好在每个操作数之后添加" + ",这样额外的就会在末尾。从StringBuilder 的末尾删除比从开头删除更有效:

        StringBuilder sb = new StringBuilder();
        
        while(sc.hasNext()){
            int i = sc.nextInt();
            sb.append( i ).append( " + " );
            sum = sum + i;
        }
        if ( sb.length() > 0 ) {
            sb.setLength( sb.length() - 3 );
        }
        answer = sb.toString();
        

        【讨论】:

          猜你喜欢
          • 2021-05-18
          • 2018-12-14
          • 2011-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多