有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

cmd Java错误:线程“main”Java中出现异常。lang.RuntimeException

我正在CMD中运行一个程序

我使用两种方法将其打印出来,如下所示:

打印a和w之间的所有字符:
a b c d e f g h i j
k l m n o p q s t
u v w

下面是我到目前为止所拥有的。。。但我在这里过不去。 我总是犯各种各样的错误

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){


}
}

共 (3) 个答案

  1. # 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();
    }
    
  2. # 2 楼答案

    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));
    
    }
    
  3. # 3 楼答案

    E:\Java>;jdk1。8.0_111\bin>;java-jarselenium-server-standalone-3.0.0。jar-htmlsuite“*firefox”C:\ProgramFiles(x86)\Mozilla firefox\firefox。exe“https://www.google.com/”E:\Selenium\newsuite。html“E:\Selenium\result。html“

    我得到了上面提到的错误。请帮忙