JAVA找不到符号 – 变量ans

huangapple go评论69阅读模式
英文:

JAVA cannot find symbol - variable ans

问题

import java.util.Scanner;

/**
 * Write a description of class main here.
 *
 * author (your name)
 * version (a version number or a date)
 */
public class main {
  public static void main(String[] args){
      Scanner input = new Scanner(System.in);
      
      double inp1;
      double inp2;
      String opo;
      
      Scanner one = new Scanner(System.in);
      System.out.println("enter first input");
      inp1 = one.nextInt();
      
      Scanner two = new Scanner(System.in);
      System.out.println("enter second input");
      inp2 = two.nextInt();
      
      Scanner three = new Scanner(System.in);
      System.out.println("choose an operation add/a sub/s mult/m div/d: ");
      
      opo = three.next();

      if (opo.equals("a")){
          double ans = inp1 + inp2;
      }
      else if (opo.equals("s")){
          double ans = inp1 - inp2;
      }
      else if (opo.equals("m")){
          double ans = inp1 * inp2;
      }  
      else if (opo.equals("d")){
          double ans = inp1 / inp2;
      }
        
      System.out.println("your answer is " + ans);
    }  
}
英文:

This is my code and I'm getting an error on the last line stating cannot find "symbol - variable ans"

I am new to java and I am trying to write a simple program that will allow the user input integers and perform operations on those integers

import java.util.Scanner;
/**
 * Write a description of class main here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class main {
  public static void main(String[] args){
      Scanner input = new Scanner(System.in);
      
      double inp1;
      double inp2;
      String opo;
      
      Scanner one = new Scanner(System.in);
      System.out.println("enter first input");
      inp1 = one.nextInt();
      
      Scanner two = new Scanner(System.in);
      System.out.println("enter second input");
      inp2 = two.nextInt();
      
      Scanner three = new Scanner(System.in);
      System.out.println("choose an oporation add/a sub/s mult/m div/d: ");
      
      opo = three.next();

      if (opo == "a"){
          double ans = inp1 + inp2;
      }
      else if (opo == "s"){
          double ans = inp1 - inp2;
      }
      else if (opo == "m"){
          double ans = inp1 * inp2;
      }  
      else if (opo == "d"){
          double ans = inp1 / inp2;
      }
        
      System.out.println("your answer is " + (String)ans);
    }  
}

答案1

得分: 1

将 if/else 链改为以下内容,应该可以正常工作:

      double ans;
      if (opo == "a"){
          ans = inp1 + inp2;
        }
      else if (opo == "s"){
          ans = inp1 - inp2;
        }
      else if (opo == "m"){
          ans = inp1 * inp2;
        }  
      else if (opo == "d"){
          ans = inp1 / inp2;
        }
英文:

Change the if/else chain to this and it should work:

      double ans;
if (opo == "a"){
ans = inp1 + inp2;
}
else if (opo == "s"){
ans = inp1 - inp2;
}
else if (opo == "m"){
ans = inp1 * inp2;
}  
else if (opo == "d"){
ans = inp1 / inp2;
}

答案2

得分: 0

为什么为每个变量都定义一个Scanner对象,只需定义一个对象,然后将该对象用于每个变量。我也赞同Nathan Mills建议你在代码中要做的更改。

现在你可能没有遇到太多Scanner对象的问题,但想象一下,如果你正在解决一个涉及多个变量的问题,那时你可能会面临问题。因为它会消耗你的内存。

可以一次性定义Scanner对象。

英文:

Why your defining Scanner object for each variable just define it ones and use that object for every variable. I also go with what Nathan Mills suggests you do change in your code.

You don't face the problem now of that many scanner objects but just think if your solving one problem there are many variables then at that time you will face the problem. Because it will consume your memory

To define the Scanner object at ones.

huangapple
  • 本文由 发表于 2020年9月24日 11:29:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64039118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定