我在接受采访时在java中得到了一个问题 [英] i was given a question in java in an interview

查看:77
本文介绍了我在接受采访时在java中得到了一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class myclass{

int count;
while() {//

count ++;
}//


Public static void main(string args[]){
myclass m= new myclass();
system.out.println(m.count);
}





什么是输出?他们在评论中保留但我们有计数++语句



what will be the out put??they kept while in the comment but we had count++ statement

推荐答案

输出将是编译时错误。查看(首先) ,同时 [ ^ ]循环结构。虽然循环需要一个参数(我会说它是一个参数,因为它在括号中传递)到一个参数,它应该解析为一个布尔值。如果你没有传递一个表达式,它将显示错误消息,非法表达式启动



Java中的while看起来像,



The output would be a compile time error. Have a look at (first of all) your while[^] loop structure. While loop requires a parameter (I would say it a parameter because it is passed in a parenthesis) to work one, which should resolve to a boolean value. If you do not pass an expression, it will show an error with message, illegal start of expression.

The while in Java looks like,

while (expression) {
   // Statement(s)
}





所以,你的代码将被编辑出去,





So, your code would be edited out to,

int count = 0;
private static final int i = 0;
while (i < 10) {
   count++; // Increment, until i is 9
}





后来一个,Java中没有字符串,有一个 字符串 [ ^ ]。这也是代码中的问题,这将导致麻烦,编译器错误。这是因为Java是区分大小写的语言,而String对于什么字符串有不同的含义。它们是不同的。



因此,更改主要功能的声明,





Later one, there is no string in Java, there is a String[^] in Java. That is also a problem in the code, which will cause a trouble, same, compiler error. This would be because Java is a case-sensitive language and String has a different meaning as to what string has. They are different.

So, change the declaration of the main function also,

public static void main(String[] args) { }





这里的另一点,在Java或C#中,数组的 [] 运算符紧跟在类型之后,而不是在标识符C或C ++之后。那是另一个错误。了解有关 Java阵列的更多信息[ ^ ]。



长话短说,程序根本就错了,也许开发人员正在看着你说编译错误! - 作为正确的答案。 :-)



如果你写了那个程序,(即使你没有;因为你自己没有太多关于Java的语言知识),我会建议你您从一个研究所重新获得Java会话,或者如果您想要一个在线资源,请查看Oracle语言的Java语言。 https://docs.oracle.com/javase/tutorial/java/concepts/index.html [ ^ ]



此外,如果在源代码的灯具之后正确运行,程序将会产生意外结果。暴露的循环不得出现在类本身中,必须隐藏这些成员(封装在OOP中 [ ^ ];阅读相关内容),然后可以写成员作为静态(而不是实例)并删除该类实例化,直接调用它。



Another point here, in Java or C#, the array's [] operator comes right after the type, not after the identifier as C or C++. That was another mistake. Read more about Java arrays[^].

Long story short, the program at all was wrong, maybe there developer was looking at you to say, "Compiler error!" -- as the correct answer. :-)

If you wrote that program, (and even if you didn't; since you yourself do not have much language knowledge of Java) I will recommend that you retake the Java sessions from an institute or if you want an online resource please look at Oracle documentations for Java language. https://docs.oracle.com/javase/tutorial/java/concepts/index.html[^]

Also, the program as it is would give unexpected result if ran correctly after those fixtures to the source code. Exposed loops must not be present in the class itself, such members must be hidden (encapsulation in OOP[^]; read about it), then the member can be written as a static (rather than an instance) and remove that class instantiation, call it directly.


您报告的代码不产生输出,它进入无限循环。

正如 Afzaal Ahmad Zeeshan 所指出的,这样的代码甚至不会编译。
The code you reported produces no output, it goes in a infinite loop.
As pointed out by Afzaal Ahmad Zeeshan, such a code won't even compile.


这篇关于我在接受采访时在java中得到了一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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