有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java简单计算器无法通过println

这是我的代码

https://ideone.com/ok42QZ

    System.out.println("setcaculatorinput");
    Scanner sc = new Scanner(System.in);
    int z = sc.nextInt();
    System.out.println("setvalueA");
    int a = sc.nextInt();
    System.out.println("setvalueB");

主要的问题是我无法通过这个系统。出来println(“setcaculatorinput”),因为它正好在该行之后崩溃


共 (5) 个答案

  1. # 1 楼答案

    System.out.println("setcaculatorinput");
    Scanner sc = new Scanner(System.in);
    int z = Integer.parseInt(sc.nextLine());   
    System.out.println("setvalueA");
    int a = Integer.parseInt( sc.nextLine());
    System.out.println("setvalueB");
    

    基本上,当你以sc.nextInt()的双重身份读取时,你只读取int,但是当你在流中点击enter时,会有一个不被sc.nextInt()读取的结束行字符

  2. # 2 楼答案

    1)对于初学者,将第17行更改为String z = sc.next(); 您正在读取整数,传递字符,并进一步将其视为字符串

    2)在each if语句之后,有;,所以后面的每个块都将被执行

  3. # 3 楼答案

    问题在于试图将输入+解析为代码第13行的整数(以及stdin上的第一个输入)。您可以将调用从.nextInt()切换到.nextLine(),并将z的类型更改为String

    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println("setcaculatorinput");
        Scanner sc = new Scanner(System.in);
        //switch z out for a String instead of an int, and use .nextLine()
        String z = sc.nextLine();
        System.out.println("setvalueA");
        int a = sc.nextInt();
        System.out.println("setvalueB");
        int b = sc.nextInt();
        ...
    }
    
  4. # 4 楼答案

    让我格式化你的代码。。。 首先:

    if ("+".equals(z))
        ; // it's an empty condition!
    
    { // this is an init block
     System.out.println(c);
    }
    

    您所有的代码都已“格式化”:

    public static void main(String[] args) throws java.lang.Exception {
            System.out.println("setcaculatorinput");
            Scanner sc = new Scanner(System.in);
            int z = sc.nextInt();
            System.out.println("setvalueA");
            int a = sc.nextInt();
            System.out.println("setvalueB");
            int b = sc.nextInt();
            int c, d, e, f;
    
            c = (a + b);
            d = (a - b);
            e = (a * b);
            f = (a / b);
    
            if ("+".equals(z))
                ;
            if ("-".equals(z))
                ;
            if ("*".equals(z))
                ;
            if ("/".equals(z))
                ;
    
            { // init block
                System.out.println(c);
                System.out.println(d);
                System.out.println(e);
                System.out.println(f);
            }
            System.exit(0);
        }
    

    显示该错误是因为您试图读取一个非整数字符,读取new Scanner("").nextInt();的Javadoc,您将看到它抛出:“InputMismatchException-如果下一个标记与整数正则表达式不匹配,或者超出范围”

    我给你一个简短的解决方案,但我不推荐,因为没有对输入进行检查

    public static void main(String[] args) {
            System.out.println("setcaculatorinput");
            Scanner sc = new Scanner(System.in);
            int result = 0;
    
            System.out.println("setvalueA=");
            int a = sc.nextInt();
    
            System.out.println("setOperator=");
            String op = sc.next();
    
            System.out.println("setvalueB=");
            int b = sc.nextInt();
    
            if ("+".equals(op))
                result = a + b;
            else if ("-".equals(op))
                result = a - b;
            else if ("*".equals(op))
                result = a * b;
            else if ("/".equals(op))
                result = a / b;
    
            System.out.println("The result is=" + result);
            System.exit(0);
        }
    
  5. # 5 楼答案

    此代码将起作用:

    import java.util.Scanner;
    
    class Ideone
    {
        public static void main (String[] args) throws java.lang.Exception
        {
            System.out.println("setcaculatorinput");
            Scanner sc = new Scanner(System.in);
            String z = sc.next();
            System.out.println("setvalueA");
            int a = sc.nextInt();
            System.out.println("setvalueB");
            int b = sc.nextInt();
            int c;
            int d;
            int e;
            int f;
            c = ( a + b );
            d = ( a - b );
            e = ( a * b );
            f = ( a / b );
            if("+".equals(z))
            {
            System.out.println(c);
            }
            if("-".equals(z))
            {
            System.out.println(d);
            }
            if("*".equals(z))
            {
            System.out.println(e);
            }
            if("/".equals(z))
            {
            System.out.println(f);
            }
            System.exit(0);
        }
    }
    

    谢谢!