为什么限制在我编写的代码中不起作用? [英] Why is the limitation not working in the code I have written?

查看:68
本文介绍了为什么限制在我编写的代码中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.lang。*;

import java.util.Scanner;



public class tp {

public static void main(String Args []){



Scanner in = new Scanner(System.in);



双倍限制;



System.out.println(输入整数限制:);

limit = in.nextInt();



int num = 0;



for(int x = 1; num< limit; x = x + 2){

System.out.println(num + x);

num = num + x;

}





}

}



我尝试了什么:



我试过给出要打印的数字的条件必须小于限制。

import java.lang.*;
import java.util.Scanner;

public class tp {
public static void main(String Args[]){

Scanner in = new Scanner(System.in);

double limit;

System.out.println("Enter the limit in integer : ");
limit = in.nextInt();

int num = 0;

for(int x = 1; num < limit; x = x + 2){
System.out.println(num + x);
num = num + x;
}


}
}

What I have tried:

I tried giving the condition of the number to be printed has to be smaller than the limit.

推荐答案

每次循环,x增加2 - 但num增加它的先前值。

所以当你围绕循环:

Each time round the loop, x increases by 2 - but num increases by it's previous value.
So as you go round the loop:
num   x    printed
0     1    1
1     3    4
4     5    9
...

因为你打印的不是你与限制比较的数字,它总是会打印一个你没想到的数字。



我不确定你要打印的是什么序列,但要写出来,并试着找到最简单的算法来打印它,而不是急于代码。

since what you print is not the number you compare against the limit, it always prints a number you didn't expect.

I'm not sure what sequence you are trying to print, but write it out, and try to find the simplest algorithm to print it, instead of rushing into code.


我担心你的代码正在按你的要求做,你应该重新考虑它并使用调试器来看看发生了什么以及为什么。

注意:你忘了给我们一个你期望输出的例子。



当你不明白你的代码在做什么或者为什么它做它做的,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



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

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

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

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

调试器中没有魔法,它没有找到错误,它只是帮助你至。当代码没有达到预期的效果时,你就会接近一个错误。
I fear your code is doing what you requested, you should rethink about it and use the debugger to see what happen and why.
Note: you forgot to give us an example of the output you expect.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger 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, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
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 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天全站免登陆