2015-09-26 39 views
0

我正在CMD中运行一个程序。Java错误:线程“main”中的异常java.lang.RuntimeException

我使用2种方法把它打印出来像下面的:

打印和W之间的所有字符:
ABCDEFGHIJ
klmnopqst
UVW

下面是我迄今为止...但我无法在这里过去。 我不断收到各种错误。

public class Letters{ 
public static void main (String[] args) { 
System.out.println("Print all characters between a and w"); 

for (char i = 0; i <= 10; i++) { 
String myrow = printChars('a','w'); 
System.out.println(myrow); 
} 
} 
public static void printChars (char c1, char c2){ 


} 
} 
+2

你检查有关文件for循环在Java中https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html?提示在for语句的右括号之后删除';'并且大括号不匹配 –

+0

如果你需要帮助,那不是e没有必要说“各种各样的错误”。您需要添加确切的错误消息,并显示错误出现在代码中的哪个位置。 – RealSkeptic

+0

线程“main”中的异常java.lang.RuntimeException:不可编译的源代码 - 错误的sym类型:Unit03Lab.printRow \t at Unit03Lab.main(Unit03Lab.java:6) –

回答

0
public static void main(String[] args) 
{ 
    char c1='a'; 
    char c2='w'; 

    String str= "a b c d e f g h i j k l m n o p q s t u v w"; 
    Pattern compile = Pattern.compile(".*"+c1+"(.*)"+c2+".*"); 
    Matcher matcher = compile.matcher(str); 
    boolean found = matcher.find(); 
    System.out.println("found:"+ found); 
    if (found) System.out.println(matcher.group(1)); 

} 
+2

(a)只有代码答案是低质量的。请添加解释。 (b)这是硬编码 - OP似乎想要灵活地做到这一点。 – RealSkeptic

1
首先

,我建议适当缩进代码,使其更容易阅读。其次,当你使用char时,你可以检查字符的值。 char c = 'a'就是这样。如果你递增c,那么它的值为b

public class Letters 
{ 
    public static void main (String[] args) { 
     System.out.println("Print all characters between a and w"); 

     for (char c = 'a'; c <= 'w'; c++) { 
      System.out.print(c); 
      if ((c+1 - 'a') % 10 == 0) { 
       System.out.println(); 
      } 
     } 

     System.out.println("\n"); 

     printChars ('d','z'); 
    } 

    public static void printChars (char c1, char c2) { 
     // no valid range, can also add checks to see if they are actual letters 
     if (c1 > c2) { 
      return; 
     } 

     System.out.println("Print all characters between " + c1 + " and " + c2); 
     for (char c = c1; c <= c2; c++) { 
      System.out.print(c); 
      if ((c+1 - c1) % 10 == 0) { 
       System.out.println(); 
      } 
     } 
    } 
} 

这部分的检查,目前正在做什么迭代,它然后检查是否该迭代是由10整除,添加一个新的生产线,如果不是,沿着。

if ((c+1 - 'a') % 10 == 0) { 
    System.out.println(); 
} 
+0

谢谢!我看到我有一些事情正在进行。但是该程序不会输出行中的字母.. @emz –

+0

谨慎澄清?它通过'System.out.println();'打印出新行中的每一个字母,并且每次迭代只需递增一次。 – Emz

+0

每行10个字母@emz –

0

E:\的Java> jdk1.8.0_111 \ BIN> Java的罐子硒的服务器独立-3.0.0.jar -htmlsuite “* Firefox的” C:\ Program Files文件(x86)的\ Mozilla Firefox浏览器\ firefox.exe” “https://www.google.com/” “E:\硒\ newsuite.html” “E:\硒\ result.html”

我得到了上述error.please帮助

相关问题