在java中,在这个程序中,它在代码的某个点突然停止正常运行?但它能编译吗?有什么想法吗?可能是什么问题?
我不知道它为什么会停止,但在“/”指示的位置,程序突然停止让我输入信息,并且不继续我希望它执行的过程。我不会费心详细解释这个程序,因为我相信从代码本身就可以很明显地看出我想做什么。在java中,在这个程序中,它在代码的某个点突然停止正常运行?但它能编译吗?有什么想法吗?可能是什么问题?,java,eclipse,Java,Eclipse,我不知道它为什么会停止,但在“/”指示的位置,程序突然停止让我输入信息,并且不继续我希望它执行的过程。我不会费心详细解释这个程序,因为我相信从代码本身就可以很明显地看出我想做什么。 非常感谢你的帮助 字符串比较不正确--需要使用equals()方法比较字符串,如x.equals(“*”),以使它们中的任何一个正常工作。(这是一个很常见的错误,所以即使是家庭作业,freebie:) 没有循环,所以它将在第一次“通过”后停止;这可能是您想要的,也可能不是您想要的。使用 import java.io.
非常感谢你的帮助 字符串比较不正确--需要使用
equals()
方法比较字符串,如x.equals(“*”)
,以使它们中的任何一个正常工作。(这是一个很常见的错误,所以即使是家庭作业,freebie:)
没有循环,所以它将在第一次“通过”后停止;这可能是您想要的,也可能不是您想要的。使用
import java.io.IOException;
import java.util.*;
public class task2 {
public static void main (String [] args) throws IOException {
int a;
int b;
String y;
String x;
Scanner input = new Scanner(System.in);
System.out.println("Please enter number A:");
a = input.nextInt();
System.out.println("\nPlease enter number B:");
b = input.nextInt();
System.out.println("\nLastly, enter A if you wish it to be the dividor and/or subtractor, or if you wish it to be B, please enter B :"); //stops running properly here...
y=input.nextLine();
System.out.println("\nWhat would you like to do? Multiply (*), Divide (/), Subtract (-) or Add (+)? Please enter the symbol of which process you would like to have completed:");
x=input.nextLine();
if (y=="b"+"B") {
if (x=="*") {
System.out.println("\nThe product of these numbers is:" + a*b);}
else
if (x=="/") {
System.out.println("\nThe quotient of these numbers is:" + a/b);}
else
if (x=="+") {
System.out.println("\nThe sum of these numbers is:" + a+b);}
else
if (x=="-") {
System.out.println("\nThe difference of these numbers is:" + (a-b));}}
else
if (y=="a"+"A"){
if (x=="*") {
System.out.println("\nThe product of these numbers is:" + b*a);}
else
if (x=="/") {
System.out.println("\nThe quotient of these numbers is:" + b/a);}
else
if (x=="+") {
System.out.println("\nThe sum of these numbers is:" + b+a);}
else
if (x=="-") {
System.out.println("\nThe difference of these numbers is:" + (b-a));}}
}
}
不是
因为nextLine()跳过输入并将扫描仪设置到下一行,并返回跳过内容的字符串表示形式。您的程序抛出错误,因为下一行不存在nextLine返回一个字符串。。。hasNextLine返回一个布尔值。但是nextline跳过输入,将扫描器设置到下一行,并返回跳过内容的字符串表示形式。好的,在最后一行之前,一切都很好。。。无法输出计算结果:S@TheoLopezdeCastilla那么,我可能会问一个新问题;看不到您的代码,但整个“b”+“b”几乎肯定是不正确的。没问题,要获得输出,您需要将if(y==“b”+“b”)更改为if(y.equals(“b”)| | y.equals(“b”),“a”和“a”也一样。介意接受答案,如果它是有效的工作代码:这不是真正的家庭作业。。。。这是我让自己做的学习编程的任务!
input.next();
input.nextLine();