在java中,在这个程序中突然停止在某个代码点正常运行? 但它编译? 任何想法可能是什么问题?

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:" + (ab));}} 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:" + (ba));}} } } 

我不知道为什么它会停止但是在“//”指示的地方突然停止让我输入信息并且不继续我希望它做的过程。 我不会在detial中解释这个程序,因为我相信从代码本身可以看出我想要做的事情。 感谢所有的帮助!

使用

 input.next(); 

 input.nextLine(); 

由于nextLine()跳过输入并将扫描器设置为NEXT行并返回跳过的字符串表示。 您的程序会抛出错误,因为NEXT行不存在

你的字符串比较是不正确的 – 你需要使用equals()方法比较字符串,比如x.equals("*") ,以便它们中的任何一个都可以工作。 (这是一个非常常见的错误,所以尽管这是家庭作业,免费赠品:)

没有循环,所以它会在第一次“通过”后停止; 这可能是也可能不是你想要的。