-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'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...
于 2013-01-18T16:19:46.967 に答える
2

問題は、c変数に初期値を割り当てる必要があることです。Javaでは、ローカル変数の値を使用するときはいつでも、Javaはそれを何かに設定する必要があると主張します。プログラムの下部にcの値を出力しますが、その値を条件内に設定するため、Javaは、cの値がSystem.outで使用される前に、これらの条件の1つが実行されるかどうかを確認できません。声明。これを修正する方法は、cに値を割り当てる条件に「else」を追加するか、実行が保証されている他のポイント(作成時など)にその値を設定することです。

たとえば、コードを次のように変更します

double a, b, c = 0.0;

コンパイルエラーがなくなります。しかし、他の人が指摘しているように、コードには他の問題もあります。

于 2013-01-18T16:23:58.253 に答える
1

明確な理由もなく 2 つの異なるタイプを比較している

試す

 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

ありがとう。結果 ( 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

次の行に進むには、各入力の最後に 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

// メソッドを使用した簡単な電卓

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 に答える