运行代码时,它不会输出任何内容

时间:2020-08-12 12:45:55

标签: java arrays java.util.scanner user-input bufferedreader

这是codevita-2020问题的一部分,称为“星座”。

我尝试使用Java解决问题。我已经建立了逻辑,但是在尝试在char数组中接受输入时遇到了困难。

(每行之间有一个空格)

问题陈述:
三个字符{#,*,。 }代表太空中的恒星和星系的星座。每个星系都用#个字符分隔。给定星系中可能有一个或多个恒星。星星只能是元音{A,E,I,O,U}的形状。元音形状的*集合是一个星形。 3x3块中包含一个星星。星星不能重叠。点号(。)表示空白。

给出由{#,*,...组成的3×N矩阵。 },找到其中的银河和恒星。

注意:请注意以下示例部分中3x3块中元音A的表示方式。

示例1

   Input

    18

    * . * # * * * # * * * # * * * . * .

    * . * # * . * # . * . # * * * * * *

    * * * # * * * # * * * # * * * * . *

    Output
    U#O#I#EA

我的代码:

package codevita;

//constellations - codevita

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.*;

public class constellations {
    static public BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static public PrintWriter out = new PrintWriter(System.out);
    static List<String> list = new ArrayList<>();
    static int main = 0;
    public static void main(String[] args) throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
        StringBuilder result = new StringBuilder();
        int n = Integer.parseInt(reader.readLine());
        System.out.println();
        String line[] = new String[3];
        int x = line.length;
        for(int j = 0 ; j < 3; j++) {
            line[j] = sc.nextLine();
            System.out.println();
        }
        char []line1 = line[0].toCharArray();
        char []line2 = line[1].toCharArray();
        char []line3 = line[2].toCharArray();
        int count = 0,main = 0;
        for(int k = 0; k < line1.length; k++){
            if(line1[k] =='#'){
                count++;
            }
        }
        int split[] = new int [n];
        for(int l = 0; l < line1.length; l++){
            if(line1[l] =='#'){
                split[l] = l;
            }
        }
        // for A
        for(int i = 2; i< n; i++){
            // for A
            if(line1[i] =='#') {
                i++;
            }
            if(line1[i]== '.' && line1[i-1]== '*' &&line1[i-2]== '.' &&line2[i]== '*' && line2[i-1]== '*' &&line2[i-2]== '*' && line3[i]== '*' && line3[i-1]== '.' &&line3[i-2]== '*'){
                result.append('A');
                if(split[i] != 0){
                    result.append('#');
                }
                System.out.print(result.toString());
                main++;
            }
            // for E
            else if(line1[i]== '*' && line1[i-1]== '*' &&line1[i-2]== '*' &&line2[i]== '*' && line2[i-1]== '*' &&line2[i-2]== '*' && line3[i]== '*' && line3[i-1]== '*' &&line3[i-2]== '*'){
                result.append('E');
                if(split[i] != 0){
                    result.append('#');
                }
                main++;
            }
            // for I
            else if(line1[i]== '*' && line1[i-1]== '*' &&line1[i-2]== '*' &&line2[i]== '.' && line2[i-1]== '*' &&line2[i-2]== '.' && line3[i]== '*' && line3[i-1]== '*' &&line3[i-2]== '*'){
                result.append('I');
                if(split[i] != 0){
                    result.append('#');
                }
                main++;
            }
            // for O
            else if(line1[i]== '*' && line1[i-1]== '*' &&line1[i-2]== '*' &&line2[i]== '*' && line2[i-1]== '.' &&line2[i-2]== '*' && line3[i]== '*' && line3[i-1]== '*' &&line3[i-2]== '*'){
                result.append('O');
                if(split[i] != 0){
                    result.append('#');
                }
                main++;
            }
            // for U
            else if(line1[i]== '*' && line1[i-1]== '.' &&line1[i-2]== '*' &&line2[i]== '*' && line2[i-1]== '.' &&line2[i-2]== '*' && line3[i]== '*' && line3[i-1]== '*' &&line3[i-2]== '*'){
                result.append('U');
                if(split[i] != 0){
                    result.append('#');
                }
                main++;
            }
            
        }
        if(main >= (n-count)/x) {
            out.println(result.toString());
        }
        sc.close();
        out.close();
    }

    static int[] readArray(int n) throws IOException {
        Scanner sc = new Scanner(System.in);
        int[] a = new int[n];
        String[] data = reader.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(data[i]);
        }
        sc.close();
        return a;

    }
}


> It shows no output when I run this.

2 个答案:

答案 0 :(得分:1)

通过System.inBufferedReader reader读取Scanner sc都不可靠。您只需决定一种读取输入的方法。可能最简单的解决方法是更改​​

        Scanner sc = new Scanner(System.in);

        Scanner sc = new Scanner(reader);

然后,您没有考虑字符之间的空格。您可以通过在早期删除空格来解决此问题。改变

            line[j] = sc.nextLine();

            line[j] = sc.nextLine().replace(" ", "");

此外,在输出中插入#的逻辑是错误的。要解决,只需更改

            if(line1[i] =='#') {
                i++;
            }

            if (line1[i] == '#') result.append('#');

请注意,这仍然不会在开始时显示#,因为您只能从int i = 2;开始循环。

答案 1 :(得分:0)

int n=18,x1,y1; Scanner sc=new Scanner(System.in);

    char x[][]=new char[3][n];
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<n;j++)
        {
            x[i][j]=sc.next().charAt(0);
        }
    }
    for(int i=0;i<n;i++)
    {
        if(x[0][i]=='#' && x[1][i]=='#' && x[2][i]=='#')
        {
            System.out.print("#");
        }
        else if(x[0][i]=='.' && x[1][i]=='.' && x[2][i]=='.')
        {}
        else
        {
            char a,b,c,a1,b1,c1,a2,b2,c2;
            x1 = i;
            a = x[0][x1];
            b = x[0][x1+1];
            c = x[0][x1+2];
            a1 = x[1][x1];
            b1 = x[1][x1+1];
            c1 = x[1][x1+2];
            a2 = x[2][x1];
            b2 = x[2][x1+1];
            c2 = x[2][x1+2];
            if(a=='.' && b=='*' && c=='.' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='.' && c2=='*')
            {       
                System.out.print("A");
                i = i + 2;
            }
            if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
            {       
                System.out.print("E");
                i = i + 2;
            }
            if(a=='*' && b=='*' && c=='*' && a1=='.' && b1=='*' && c1=='.' && a2=='*' && b2=='*' && c2=='*')
            {       
                System.out.print("I");
                i = i + 2;
            }
            if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
            {       
                System.out.print("O");
                i = i + 2;
            }
            if(a=='*' && b=='.' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
            {       
                System.out.print("U");
                i = i + 2;
            }
        }
    }