Java,简单的控制台计算器出了什么问题

时间:2013-01-18 16:15:35

标签: java console calculator

我试图在java中编写一个简单的计算器,因为我想学习这门语言。但我无法计算“C”变量。请帮忙。怎么了?

    import java.util.*;

//Simple calculator

public class calc
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        double a, b, c;

        System.out.print("Write first number");
        a = in.nextDouble();
        System.out.print("and write second");
        b = in.nextDouble();
        System.out.print("Choose the operation   1.Addition  2.Subtraction   >Please write the number of operation ");
        double somethin = in.nextDouble();
        int addition = 1;
        int subtraction = 2;
        if (somethin >= addition) 
        {
        c = a + b; 
        }
        else if (somethin >= subtraction)
        {
        c = a - b;
        }
        {
        System.out.println(c); 
        }
    }
}

6 个答案:

答案 0 :(得分:6)

看起来错误是代码始终落在addition条件下,因为您正在比较最后一个输入值是大于还是等于添加。

if (somethin >= addition) 
{
    c = a + b; 
}
else if (somethin >= subtraction)
{
    c = a - b;
}

因此,如果somethin> = 1将进行添加。当您的输入为2时,它将检查2 >= 1,因此它将属于添加规则。

将您的代码更改为

if (somethin == addition) 
{
    c = a + b; 
}
else if (somethin == subtraction)
{
    c = a - b;
}

此外,在这种情况下,您应该使用int变量而不是double

//double somethin = in.nextDouble();
int somethin = in.nextInt();

正如matt forsythe's answer中所述,必须声明变量并在使用之前对其进行初始化。在你的代码中,你有这个:

double a, b, c; //declaring the variables
//...
if (somethin == addition) 
{
    c = a + b; 
}
else if (somethin == subtraction)
{
    c = a - b;
}
System.out.println(c); //c it's declared but hasn't been initialized =\

为了解决此问题,您应该在c行中使用之前初始化System.out.println(c)变量值。要解决它,您可以在if评估之前给它一个值:

c = 0;
if (somethin == addition) 
//rest of the code...

答案 1 :(得分:2)

问题是您需要为c变量分配初始值。在Java中,只要您使用局部变量的值,Java就会坚持必须将其设置为某种东西。由于您在程序的底部打印出c的值,但是在条件的内部设置了它的值,因此Java不确定在System.out中使用c的值之前是否会执行其中一个条件声明。修复此问题的方法是在条件中添加“else”,也为c赋值,或者在保证执行的其他点设置其值(例如在创建时)。

例如,将代码更改为

double a, b, c = 0.0;

会使编译错误消失。但是,正如其他人所指出的那样,代码也存在其他问题。

答案 2 :(得分:1)

你正在比较两种不同类型,没有明显的理由

尝试

 double somethin = in.nextDouble();
 double addition = 1.0;
 double subtraction = 2.0;
 if (somethin == addition) 
 {
     c = a + b; 
 }
 else if (somethin == subtraction)
 {
     c = a - b;
 }

或所有整数,或投下它们,或解析它们等

答案 3 :(得分:0)

在每个输入的末尾添加一个in.nextLine()以前进到下一行:

System.out.print("Write first number");
a = in.nextDouble();
in.nextLine();
System.out.print("and write second");
b = in.nextDouble();
in.nextLine();
System.out.print("Choose the operation   1.Addition  2.Subtraction   >Please write the number of operation ");
int somethin = in.nextInt(); //Also this should be an int rather than double
in.nextLine();

您也应该检查somthin是否等于您要查找的变量,您可以使用==运算符执行此操作:

if (somethin == addition) 
{
    c = a + b; 
}
else if (somethin == subtraction)
{
    c = a - b;
}

否则应该有效。

答案 4 :(得分:0)

Thanx伙计们。我在结果中添加了操作过程(2 + 2 = 4),最后我做了这个:

import java.util.*;

  //Simple calculator

public class calc
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    double a, b, c = 0.0;

    System.out.print("Write the first number \n");
    a = in.nextDouble();
    System.out.print("And write second  \n");
    b = in.nextDouble();
    System.out.print("Choose the operation  " +
            "\n1.Addition" +
            "\n2.Subtraction" +
            "\n3.Multiplication" +
            "\n4.Division" +
            "\n5.Power" +
            "\n#Please write the number of operation \n");
    double somethin = in.nextDouble();
    double addition = 1;
    double subtraction = 2;
    double multiplication = 3;
    double division = 4 ;
    double power = 5;
    if (somethin == addition) {
        c = a + b;  
        System.out.println(a + " + " + b + " = " + c);  }
    else if (somethin == subtraction)  {
        c = a - b;  
        System.out.println(a + " - " + b + " = " + c);  }
    else if (somethin == multiplication)    {
        c = a * b;   
        System.out.println(a + " * " + b + " = " + c);  }
    else if (somethin == division)   {
        c = a / b;  
        System.out.println(a + " / " + b + " = " + c);  }
    else if (somethin == power) {
        System.out.println("Enhance A or B?" +
                "\n1.A" +
                "\n2.B");
        double enhance = in.nextDouble();
        double first = 1;
        double second = 2;
            if (enhance == first) {
                System.out.println(Math.pow(a, 2)); }   
            else if (enhance == second); {
                System.out.println(Math.pow(b, 2)); }

    }
}

}

答案 5 :(得分:0)

//使用方法的简单计算器

import java.util.*;
class Calc
{
public static int a,b,c;
public static void main(String args[])
{
Calc o=new Calc();
System.out.println("Enter the numbers:");
Scanner s=new Scanner(System.in);
a=s.nextInt();
b=s.nextInt();
System.out.println("Enter the operation:1.add 2.sub 3.mul 4.div");
int op=s.nextInt();
switch(op)
    {
        case 1:
        {   
        o.add();
        break;
        }
        case 2:
        {
        o.sub();
        break;
        }
        case 3:
        {
        o.mul();
        break;
        }
        case 4:
        {
        o.div();
        break;
        }
        case 5:
        {
        o.mod();
        break;
        }
        default:
        {
        System.out.println("None Selected");
        break;
        }
    }   
}       
    void mod()
    {
    c=a%b;
    System.out.println("Mod is"+c);
    }
    void add()
    {
    c=a+b;
    System.out.println("Add is"+c);
    }
    void sub()
    {
    c=a-b;      
    System.out.println("Sub is"+c);
    }
    void mul()
    {
    c=a*b;
    System.out.println("Mul is"+c);
    }
    void div()
    {
    c=a/b;
    System.out.println("Div is"+c);
    }

}

相关问题