练习写一个控制台上的简单计算器
下面是代码:红色部分编译不过关,找人帮忙看下
import java.util.Scanner;public class Test1
{
public static void main(String [] args)
{

System.out.println("请输入两个要进行计算的数:");
Scanner sc=new Scanner(System.in);//键盘输出
//输入一个数
double a=sc.nextDouble();
//输出第二个数
double b=sc.nextDouble();
System.out.println("请输入运算符");
string c=sc.substring(0,1);
                   yunsuan t=new yunsuan();
if(c!="+"||c!="-"||c!="*"||c!="/")
{
System.out.println("请输入正确的运算符");
}
if(c=="+")
{
t.jia(a,b);
}
if(c=="-")
{
t.jian(a,b);
}
if(c=="*")
{
t.cheng(a,b);
}
else
{
t.chu(a,b);
}


}
}
class yunsuan
{
public void jia(double x,double y)
{
double z=x+y;
System.out.println(z);
}
public void jian(double x,double y)
{
double z=x-y;
System.out.println(z);
}
public void cheng(double x,double y)
{
double z=x*y;
System.out.print(z);
}
public void chu(double x,double y)
{
double z=x/y;
System.out.println(z);
}
}

解决方案 »

  1.   

    在读入的字符串上调用substring,而不是在Scanner的实例调用
      

  2.   

    string c=sc.substring(0,1);
    改为
    String c=sc.substring(0,1);
    String第一个字母大写
      

  3.   


    System.out.println("请输入运算符");
    String c = sc.next();稍作修改一下
    另外程序貌似还存在问题,楼主注意查看一下!
      

  4.   

    string c=sc.substring(0,1);
    改成
    String c=sc.toString().substring(0,1); 
    再试试看呢?
      

  5.   

    double b=sc.nextDouble();
    System.out.println("请输入运算符"); 
    string c=sc.substring(0,1);把double b转成Double的。再toString() 
      

  6.   

    string c=sc.substring(0,1); -> String c=sc.next().substring(0,1);
      

  7.   

    java中貌似String开头字母要大写哎.
      

  8.   

    嗯,楼主写错了。。sc是Scanner对象,不是String对象。
      

  9.   


    char c = sc.next().charAt(0);
            yunsuan t = new yunsuan();        if (c == '+')
            {
                t.jia(a, b);
            }
            else if (c == '-')
            {
                t.jian(a, b);
            }
            else if (c == '*')
            {
                t.cheng(a, b);
            }
            else if (c == '/')
            {
                t.chu(a, b);
            }
            else
            {
                System.out.println("请输入正确的运算符");
            }
    首先获取char可以这样:char c = sc.next().charAt(0);
    然后你的if判断逻辑有问题
      

  10.   

    不要用=,!=比较字符串,用equals
      

  11.   

    只给楼主改了两个小地方,都给注释了.请楼主查阅.
    import java.util.Scanner;

    public class Test {
    public static void main(String [] args) {

    System.out.println("请输入两个要进行计算的数:");
    Scanner sc=new Scanner(System.in);//键盘输出
    //输入一个数
    double a=sc.nextDouble();
    //输出第二个数
    double b=sc.nextDouble();
    boolean flag;

    System.out.println("请输入运算符"); 
    String c=sc.next().substring(0,1);   //此处给你改成String c = sc.next().substring(0,1);
    yunsuan t=new yunsuan();
     
    if(c!="+"||c!="-"||c!="*"||c!="/")
    {
    System.out.println("请输入正确的运算符");
    flag = true;
    } else     //此处增加else 否则无论运算符输入是否正确 都会继续执行剩下的if语句

    if(c=="+"){
    t.jia(a,b); 
    }else
    if(c=="-")
    {
    t.jian(a,b);
    }else
    if(c=="*")
    {
    t.cheng(a,b);
    }
    else
    {
    t.chu(a,b);
    }


    }
    }




    class yunsuan
    {
    public void jia(double x,double y)
    {
    double z=x+y;
    System.out.println(z);
    }
    public void jian(double x,double y)
    {
    double z=x-y;
    System.out.println(z);
    }
    public void cheng(double x,double y)
    {
    double z=x*y;
    System.out.print(z);
    }
    public void chu(double x,double y)
    {
    double z=x/y;
    System.out.println(z);
    }
    }