这段代码中出现了什么问题 [英] What is exactly malfunctioning in this code

查看:103
本文介绍了这段代码中出现了什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;



public class systemsimulate {
	
public static void main(String[] args) {
	
	String y,ynext;
	
	Scanner scm=new Scanner(System.in);
	System.out.println("enter the initial  state");
	y=scm.next();

	
	System.out.println("enter the disturbance");
    String d=scm.next();
	
if(y=="A" && d=="1"){
		System.out.println("A");
	}
}
}





当我运行此程序时它不打印A ispite og give有效的输入参数。为什么条件没有被评估。任何人都告诉我错误



我尝试过:



i尝试使用char而不是字符串格式但它仍然无法正常工作。我也显示了单独的y和d。它已显示但是如果循环



when i run this program it doesnt print A ispite og giving valid input parameters.why the condition is not being evaluated.can anyone tell me the error

What I have tried:

i tried using char instead of string format but it is still not working.i also displayed indivsual y and d .it got displayed but it is not performing code in if loop

推荐答案

== 运算符测试引用相等性。这意味着它会检查它是否是同一个对象。但是您要检查值是否相等(如果两个对象保持相同的值)。另请参阅比较对象 - 维基教科书,为开放世界打开书籍 [ ^ ]。



对于字符串,使用等于 compareTo 方法(请参阅比较字符串和字符串部分(Java™教程>学习Java语言>数字)和字符串) [ ^ ])。
The == operator tests for reference equality. That means it checks if it is the same object. But you want to check for value equality (if two objects hold the same value). See also Comparing Objects - Wikibooks, open books for an open world[^].

For strings use the equals or compareTo methods (see Comparing Strings and Portions of Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)[^]).


首先使用调试器 - 在 if 行放置断点并运行代码。当调试器到达该行时,它将停止,您可以查看 y d 中的确切内容。条件被检查。

我们不能为你做到这一点:我们无法在你做的条件下运行你的代码,我们可以看到你正在输入的内容!
Start by using the debugger - put a breakpoint on the if line and run your code. When the debugger hits the line, it will stop and you can look at exactly what is in y and d before the condition is checked.
We can't do that for you: we can't run your code under exactly the conditions you do, we can see what you are typing!


在调试器上运行代码并检查 y d 包含。

特别注意变量,如果 y =A,它将按预期打印,但文本将失败。



有一个工具可以让你看到代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

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

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第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 [ ^ ]

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

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Run your code on debugger and inspect y and d contains.
Pay extra attention to the variables, if y="A ", it will print as you expect, but the text will fail.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
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.

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天全站免登陆