在java中,在这个程序中突然停止在某个代码点正常运行?但它编译?任何想法可能是什么问题? [英] In java, in this program it suddenly stops running properly at a certain point of code? but it compiles? any ideas what could be the issue?

查看:167
本文介绍了在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));}}
}
}

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

I dont know why it stops but where indicated by "//" the program suddenly stops letting me input information and does not continue the processes i want it to do. I wont bother explaining the program in detial because i believe it is fairly obvious from the code itself what i want to do. Thanks in adavance for all the help!

推荐答案

使用

input.next();

input.nextLine();

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

Since nextLine() skips over the input and sets the scanner to the NEXT line and returns a string representation of what was skipped. Your program throws the errow because the NEXT line does not exist

这篇关于在java中,在这个程序中突然停止在某个代码点正常运行?但它编译?任何想法可能是什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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