在 java 的输出中添加连字符

Adding Hyphen in the output of java

假设我想做一个字符到数字的转换器,并想在输出中添加连字符。在前 3 位数字中有连字符,随后每 4 位数字后有连字符

例如预期输出

    comecomecomecomecome

    266-3266-3266-3266-3266-3

我只添加了第一个连字符,但如何在 4 位数字后添加子序列连字符?

这是我如何处理第一个连字符;

if(  count==3) {
       Number += "-";
   }
   i++;

我现在得到的输出

 callhome
 225-54663

这里是主要代码

  import java.util.Scanner;

 {
  public static void main (String[] args)

 {
   Scanner  console = new Scanner(System.in);


   {
    System.out.println("Enter the  word to be converted : ");
    String Letter = console.nextLine();
    Letter = Letter.replaceAll("\s", ""); 
    Letter = Letter.toLowerCase();
    String  Number="";


    int count=0;
     int  i=0;

    while( count < Letter.length()) 
      {switch(Letter.charAt(i))
      {case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
              Number += "2";
              count++;
      break;
  case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
               Number += "3";
              count++;
      break;
   case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
              Number += "4";
              count++;
      break;
    case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':

              Number += "5";
             count++;
      break;
    case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
          Number += "6";
              count++;
      break;
    case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
              Number += "7";
              count++;
      break;
    case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
            Number += "8";
            count++;
      break;
    case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
         Number += "9";
         count++;
      break;

      }

    if(  count==3) {
       Number += "-";
   }
   i++;

           }
     System.out.println( Number );

    }


    }}

你应该使用 modulo operator:

if((count % 4) == 3) {
    Number += "-";
}

这将在位置之后放置连字符:3、7、11...等等。

试试下面... 我已经修改了程序,它正在运行

package com.test;

import java.util.Scanner;
public class TestPrint 
{
public static void main (String[] args)

{
Scanner  console = new Scanner(System.in);


{
  System.out.println("Enter the  word to be converted : ");
  String Letter = console.nextLine();
  Letter = Letter.replaceAll("\s", ""); 
 Letter = Letter.toLowerCase();
 String  Number="";


 int count=0,prevCount=0;
 int  i=0;

 while( count < Letter.length()) 
 {switch(Letter.charAt(i))
 {case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
         Number += "2";
         count++;
 break;
case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
          Number += "3";
         count++;
 break;
 case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
         Number += "4";
         count++;
 break;
  case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':

         Number += "5";
        count++;
 break;
  case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
     Number += "6";
         count++;
 break;
 case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
         Number += "7";
         count++;
 break;
 case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
       Number += "8";
       count++;
 break;
 case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
    Number += "9";
    count++;
 break;

 }

 if(count==3 || (count-prevCount == 4)) {
  Number += "-";
  prevCount = count;
 }
 i++;

      }
    System.out.println( Number );

  }


 }}