-2

我试图用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); 
        }
    }
}
4

6 回答 6

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 的回答中所述,您必须在使用之前声明变量并对其进行初始化。在你的一段代码中,你有这个:

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...
于 2013-01-18T16:19:46.967 回答
2

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

例如,将您的代码更改为

double a, b, c = 0.0;

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

于 2013-01-18T16:23:58.253 回答
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;
 }

或所有整数,或强制转换,或解析它们等

于 2013-01-18T16:20:58.997 回答
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;
}

应该以其他方式工作。

于 2013-01-18T16:19:44.483 回答
0

谢谢各位。我在结果中添加了操作过程( 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)); }

    }
}

}

于 2013-01-19T00:21:34.707 回答
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);
    }

}

于 2014-11-29T07:24:55.830 回答