如何修复代码(JAVA)? [英] How do I fix the the code (JAVA)?

查看:100
本文介绍了如何修复代码(JAVA)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,当满足条件时,代码不会增加2个数组,即xa []和yarray []。这应该是代码堵塞的解决方案2019轮1B问题manhaton可以找到问题这里 。如果你能帮助我,我会非常感激。



 import java.util。*; 

公共类Main {

public static void main(String [] args){
Scanner input = new Scanner(System.in);
int cases = input.nextInt();

for(int a = 1; a< = cases; a ++){
int people = input.nextInt();
int length = input.nextInt();

int [] xa = new int [length];
int [] yarray = new int [length];

for(int b = 1; b< = people; b ++){

int xd = input.nextInt();
int yd = input.nextInt();
String direction = input.next();

if(direction ==N){
for(int c =(yd + 1); c< length; c ++){
yarray [c] + = 1;
}
}
else if(direction ==S){
for(int v =(yd - 1); v> = 0; v--) {
yarray [v] + = 1;
}
}
else if(direction ==E){
for(int o =(xd + 1); o< length; o ++){
xa [o] + = 1;
}
}
else if(direction ==W){
for(int m =(xd - 1); m> = 0; m--) {
xa [m] + = 1;
}
}
}
int max = xa [0];
int xcord = 0;
for(int i = 0; i< length; i ++)
{
if(max< xa [i])
{
max = xa [一世];
xcord = i;
}
}

int maxy = yarray [0];
int ycor = 0;

for(int i = 0; i< length; i ++)
{
if(maxy< yarray [i]){
maxy = yarray [一世];
ycor = i;
}
}

System.out.println(案例#+ a +:+ xcord ++ ycor);

}

}
}





样本输入:< br $>

 3 
1 10
5 5 N
4 10
2 4 N
2 6 S
1 5 E
3 5 W
8 10
0 2 S
0 3 N
0 3 N
0 4 N
0 5 S
0 5 S
0 8 S
1 5 W





什么我试过了:



我试过更改增量语句,我尝试更改循环语句但没有成功。

解决方案

Quote:

由于某种原因,代码不会增加2个数组,即xa []和yarray满足条件时的[]。



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做什么,你的任务是comp它应该做什么。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过显示帮助你你发生了什么事。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


< blockquote>使用.equals()代替==完成工作。


For some reason the code does not increment the 2 arrays i.e xa[] and yarray[] when the conditions are met. This is supposed to be the solution to the code jam 2019 round 1B question manhaton the question can be found here. If you could help me I would he highly grateful.

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int cases = input.nextInt();

        for (int a = 1; a <= cases; a++) {
            int people = input.nextInt();
            int length = input.nextInt();

            int[] xa = new int[length];
            int[] yarray = new int[length];

            for (int b = 1; b <= people; b++) {

                int xd = input.nextInt();
                int yd = input.nextInt();
                String direction = input.next();

                if (direction =="N") {
                    for (int c = (yd + 1); c < length; c++) {
                        yarray[c]+=1;
                    }
                }
                else if (direction == "S") {
                    for (int v = (yd - 1); v >= 0; v--) {
                        yarray[v]+=1;
                    }
                }
                else if (direction == "E") {
                    for (int o = (xd + 1); o < length; o++) {
                        xa[o]+=1;
                    }
                }
                else if (direction == "W") {
                    for (int m = (xd - 1); m >= 0; m--) {
                        xa[m]+=1;
                    }
                }
            }
            int max = xa[0];
            int xcord = 0;
            for(int i = 0; i < length; i++)
            {
                if(max < xa[i])
                {
                    max = xa[i];
                    xcord=i;
                }
            }

            int maxy = yarray[0];
            int ycor=0;

            for(int i = 0; i < length; i++)
            {
                if(maxy < yarray[i]) {
                    maxy = yarray[i];
                    ycor=i;
                }
            }

            System.out.println("Case #" + a + ": " + xcord + " "+ ycor);

        }

    }
}



Sample Input :

3
1 10
5 5 N
4 10
2 4 N
2 6 S
1 5 E
3 5 W
8 10
0 2 S
0 3 N
0 3 N
0 4 N
0 5 S
0 5 S
0 8 S
1 5 W



What I have tried:

I have tried changing the increment statements and I have tried changing the loop statements but had no success.

解决方案

Quote:

For some reason the code does not increment the 2 arrays i.e xa[] and yarray[] when the conditions are met.


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


Using .equals() instead of == does the job.


这篇关于如何修复代码(JAVA)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆