简体   繁体   English

当代码运行时它不会 output 任何东西

[英]When the code is run it doesn't output anything

This was the part of a codevita-2020 problem called "constellations".这是名为“星座”的 codevita-2020 问题的一部分。

I tried solving the question using Java.我尝试使用 Java 解决问题。 I have built the logic but faced difficulty while trying to take input in a char array.我已经建立了逻辑,但在尝试输入 char 数组时遇到了困难。

(There is a space between each line) (每行之间有一个空格)

Problem statement:问题陈述:
Three characters { #, *, .三个字符 { #, *, . } represents a constellation of stars and galaxies in space. } 代表太空中的恒星和星系群。 Each galaxy is demarcated by # characters.每个星系都由 # 个字符划分。 There can be one or many stars in a given galaxy.在给定的星系中可以有一颗或多颗恒星。 Stars can only be in shape of vowels { A, E, I, O, U }.星形只能是元音 { A, E, I, O, U }。 A collection of * in the shape of the vowels is a star.元音形状的 * 集合是星号。 A star is contained in a 3x3 block.一颗星包含在一个 3x3 的块中。 Stars cannot be overlapping.星星不能重叠。 The dot(.) character denotes empty space.点 (.) 字符表示空白区域。

Given 3xN matrix comprising of { #, *, .给定由 { #, *, 组成的 3xN 矩阵。 } character, find the galaxy and stars within them. } 字符,找到其中的星系和星星。

Note: Please pay attention to how vowel A is denoted in a 3x3 block in the examples section below.注意:请注意下面示例部分中元音 A 在 3x3 块中的表示方式。

Example 1示例 1

   Input

    18

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

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

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

    Output
    U#O#I#EA

MY CODE:我的代码:

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.

It doesn't reliably work to read System.in both via BufferedReader reader and via Scanner sc .通过BufferedReader readerScanner sc读取System.in并不可靠。 You have to decide on only one way to read the input.您只需要决定一种读取输入的方式。 The probably simplest fix is to change可能最简单的解决方法是更改

        Scanner sc = new Scanner(System.in);

to

        Scanner sc = new Scanner(reader);

Then, you didn't take the space between the characters into account.然后,您没有考虑字符之间的空格。 You can fix this by removing the spaces at an early stage;您可以通过在早期删除空格来解决此问题; change改变

            line[j] = sc.nextLine();

to

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

Also, the logic to insert # in the output is wrong.此外,在 output 中插入#的逻辑是错误的。 To fix, just change要修复,只需更改

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

to

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

Note that this still won't print # at the beginning, because you start the loop only at int i = 2;请注意,这仍然不会在开头打印# ,因为您仅在int i = 2;开始循环. .

int n=18,x1,y1;诠释 n=18,x1,y1; Scanner sc=new Scanner(System.in);扫描仪 sc=新的扫描仪(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;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM