在以下链接中,我在以下问题中做错了什么? [英] What am I doing wrong in the following probkem, on the link below
问题描述
站点是站点上的问题,
[ ^ ]
以下是我尝试过的
我的代码是
Site is,question given on site
[^]
below is what i tried
my code is
public class Person {
private int Age;
public Person(int intialAge) {
// Add some more code to run some checks on initialAge
if(intialAge<0){
System.out.println("Age given is invalid ,setting age to 0");
intialAge=0;
Age=intialAge;
}
}
public void amIOld() {
if(Age<13 && Age>0)
{
System.out.println("You are young.");
}
if(13 < Age && Age < 18){System.out.println("You are teenager.");
}
if(Age>=18){
System.out.println("You are old");
}}
public void yearPasses() {
Age = Age++;
}
我尝试过的事情:
我从很多天开始就一直在解决这个问题,实际上我不明白何时以及如何调用yearPasses()方法,请帮助
What I have tried:
I am stuck on this problem from many days,actually i dont understand when and how yearPasses(),method is being called,please help
推荐答案
我不知道何时以及如何调用yearPasses()方法,请帮助
i dont understand when and how yearPasses(),method is being called,please help
您必须为每个输入调用它(三遍)测试用例(请仔细阅读要求和示例).
You have to call it (three times) for each input test case (read carefully requirements and the examples).
if(年龄< 13&& Age> 0)
if(Age<13 && Age>0)
应该改为
This should be instead
if(Age<13 && Age>=0)
[更新]
[update]
Age = Age ++;
Age = Age++;
这是一个错误.应该是
This is a blunder. It should be either
Age++;
或
or
++Age;
或
or
Age = Age + 1;
[/update]
1.您的构造函数是错误的,因为仅当intialAge
小于0时才设置Age = intialAge;
.请检查右大括号的位置.
2.您的yearPasses
函数需要进行四个调用才能正确增加Age值.阅读有关前缀和后缀运算符的文档.更好的书写方式是:
1. Your constructor is wrong, since it only setsAge = intialAge;
ifintialAge
is less than 0. Check where your closing brace is positioned.
2. YouryearPasses
function requires four calls to increment the Age value correctly. Read the documentation on prefix and postfix operators. A better way of writing it would be:
public void yearPasses() {
// Increment this person's age.
Age += 1;
}
3.您测试Age < 13
和Age > 13
,但不测试Age == 13
.您知道Age总是大于或等于零,因此只需要测试其他值即可.您还应该使用if ... else if ... else
而不是使用很多单个if
子句.
3. You test for Age < 13
and Age > 13
but never Age == 13
. You know that Age will always be greater or equal to zero, so you only need to test for the other values. You should also use if ... else if ... else
rather than a lot of single if
clauses.
错误报告:
您的代码因Age = 13而失败
您应该学会尽快使用调试器.现在是时候查看您的代码执行并确保它执行您期望的操作了,而不是猜测您的代码在做什么.
调试器使您可以逐行执行代码,检查变量,然后您会发现它停止了您所期望的工作.
调试器-维基百科,免费百科全书 [ Visual Studio 2010中的精通调试-入门指南 [ ^ ]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]
https://www.jetbrains.com/idea/help/debugging-your- first-java-application.html [ ^ ]
调试器在这里向您展示您的代码在做什么,并且您的任务是将其与应该做的事情进行比较.
如果代码未按预期执行操作,则说明您接近错误.
Bug report:
Your code fail for Age=13
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.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]
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.
When the code don''t do what is expected, you are close to a bug.
这篇关于在以下链接中,我在以下问题中做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!