如何修复这个程序 [英] How to fix this program

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

问题描述

我遇到的问题是布尔变量循环将冻结程序,它不会打印出字符串变量er。注意这是使用GUI窗口将文本字段放在



这是代码



 number = textField.getText(); 
do {
try {
actualN = Integer.parseInt(number);
loop = false;
} catch(NumberFormatException g){

textField_1.setText(er);

loop = true;
g.printStackTrace();
}
} while(loop == true);
answer = Integer.toBinaryString(actualN);
textField_1.setText(回答);





我尝试了什么:



我尝试了一些但没有任何工作

解决方案

如果我看起来正确:如果输入中textField无效,无法解析为整数,你去catch块。在catch块中你将loop设置为true,这会导致循环进入第二轮并再次尝试相同的转换。



我认为问题是,为什么你尝试在循环内进行整数转换?如果失败,则显示错误,如果成功,则显示结果。但是在你设置loop = true的catch块之间的代码中,循环是什么?


,所以当有一个数字格式异常时,代码将永远循环异常(无限循环) 。

我可以从你的代码中获得的基本思想是你要将textfield中的文本转换为二进制文件,如果有异常,你将显示错误信息。



没有必要为此目的使用do。因为

 toBinaryString(str)

str 中的所有文本转换为二进制文件。



但是根据你的方法尝试使用以下代码



  do  {
尝试 {
actualN = Integer.parseInt(number);
loop = false;
answer = Integer.toBinaryString(actualN);
// 设置答案
} catch (NumberFormatException g){
loop = false;
g.printStackTrace();
answer = er;
// 设置错误
}
} while (循环);

jTextField_1.setText(answer);







或不用时



 尝试 {
actualN = Integer.parseInt(number);
answer = Integer.toBinaryString(actualN);
} catch (NumberFormatException g){
g.printStackTrace();
answer = er;
}
jTextField_1.setText(回答);





希望这会对你有帮助......


使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量。



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

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



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

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


The problem i am having is that the boolean variable loop will freeze the program and it will not print out the String variable er. Note this is using a GUI window to place the textfield in

Here is the code

number = textField.getText();
					do{
					try{
					actualN = Integer.parseInt(number);
					loop = false;
					}catch(NumberFormatException g){
						
						textField_1.setText(er);
						
						loop = true;
						g.printStackTrace();
					}
					}while(loop == true);
					answer = Integer.toBinaryString(actualN);
					textField_1.setText(answer);



What I have tried:

I have tried a few things but nothing has worked

解决方案

If I'm looking correctly: If the input in textField is invalid and cannot be parsed to integer, you go to catch block. In catch block you set loop to true which causes the loop to go to the second round and try the same conversion again and again and again.

I think the question is, why do you try to do the integer conversion inside a loop? If it fails, show the error, if it succeeds, show the result. But what is the loop for?


in your code between catch block you are setting loop=true, so when there is a number format exception the code will loop the exception forever(infinite loop).
The basic idea I can get from your code is you are going to convert the text in textfield to binary and if there is an exception you are going to show an error message.

There is no need to use a do while for this purpose. because

toBinaryString(str)

converts all the text in str to binary.

but with your approach try to use following code

do {
           try {
               actualN = Integer.parseInt(number);
               loop = false;
               answer = Integer.toBinaryString(actualN);
               //set the answer
           } catch (NumberFormatException g) {
               loop = false;
               g.printStackTrace();
               answer=er;
               //set the error
           }
       } while (loop);

       jTextField_1.setText(answer);




or without do while

try {
            actualN = Integer.parseInt(number);
            answer = Integer.toBinaryString(actualN);
        } catch (NumberFormatException g) {
            g.printStackTrace();
            answer = er;
        }
        jTextField_1.setText(answer);



Hope this will help you...


Use the debugger to see what your code is doing. It 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[^]

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.


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

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