为什么变量的值总是-1? [英] Why is the value of my variable always -1?

查看:133
本文介绍了为什么变量的值总是-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnCalculateMouseClicked(java.awt.event.MouseEvent evt) {                                          
        String hexNumber;
        int decNumber = 0;
        int counter;

        hexNumber = txtHexNumber.getText();

        for (counter = (hexNumber.length() - 1); counter >= 0; counter--);
        {
            System.out.println(counter);
        }
        txtDecNumber.setText(decNumber + "");
    } 





它不关心String hexNumber的长度,计数器的值总是-1!

但是我不明白为什么!



我尝试了什么:



我尝试以单步模式运行代码,但我找不到问题。



It doesn't care how long the String hexNumber is, the value of counter is always -1!
But I don't understand why!

What I have tried:

I tried to run the code in the single-step mode but I couldn't find the problem.

推荐答案

罪魁祸首是终止你的循环的分号:

The culprit is that semi-colon that terminates your loop:
for (counter = (hexNumber.length() - 1); counter >= 0; counter--);


问题是这一行末尾的半个问题。

The problem is the semi at the end of this line.
for (counter = (hexNumber.length() - 1); counter >= 0; counter--);





你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码正在执行并确保它能达到预期的效果。



Debugger - 维基百科,免费的百科全书 [ ^ ]

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



调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。



You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

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 find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于为什么变量的值总是-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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