小编典典

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

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

我不知道为什么它会停止,但是在“
//”指示的地方程序突然停止让我输入信息,并且没有继续我想要它执行的过程。我不会花太多时间来解释该程序,因为我认为从代码本身来看,我想做什么很明显。感谢您提供的所有帮助!


阅读 198

收藏
2020-11-30

共1个答案

小编典典

input.next();

input.nextLine();

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

2020-11-30