2015-09-10 69 views
-4

ADDn - 前n(0≤n≤4)位複製並連接到前n位。最後的n位被刪除如何在java中通過runner類和方法類添加?

例如, ADD3 ABCDEFGH成爲ABCABCDE

方法 -

public class Methods 
{ 

public String ADD(String x, int y) 
    { 
     if(y > 0) 
     { 
     String output = x.substring(0,y); 
     String output2 = x.substring(y, x.length() - y); 
     return output + output + output2; 
     }else { 
      return x; 
     } 
} 

RUNNER -

 import java.io.*; 
     import java.util.*; 
     public class Runner 
{ 

     public static void main(String []args)throws FileNotFoundException 
     { 
      Scanner in = new Scanner(new File("data.txt")); 
      Methods md = new Methods(); 
      String cell = in.nextLine(); 
      String methods = in.nextLine(); 
      for (int x = 0; x < 5; x++) 
      { 
      if(methods.equals(("ADD2")) 
       { 
        int i = Integer.parseInt(methods); 
        System.out.println(); 
       } 

DATAFILE- ADD2 ABBCDFGG

我需要它來打印ABABBCDF

+0

ÿ ou可能想要了解模運算符是什麼以及它是如何工作的(以及爲什麼它不適合這個問題):https://en.wikipedia.org/wiki/Modulo_operation – deyur

+0

@deyur你可以用它來解決這個問題,但你需要除以10,而不是3. –

+1

模數在這裏也不會工作,至少不容易,@PaulBoddington,因爲300%10 == 0;但它肯定包含3,而3%10 == 3. –

回答

1
class Problem { 
    public static void main(String[] args) { 
     for (int num = 1000; num <= 2000; num++) { 
      if (String.valueof(num).contains("3")) { 
       System.out.println(num); 
      } 
     } 
    }  
}