繁体   English   中英

二进制运算符> =,-,*的错误操作数类型

[英]bad operand types for binary operator >=, -, *

我在弄清楚如何解决我的代码中不断遇到的错误时遇到了麻烦

import java.util.Scanner;

public class Unit02Prog1 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String name;
        System.out.print("name");
        name = input.next();

        String catagory;
        System.out.print("Catagory");
        catagory = input.next();

        String numWords;
        System.out.print("numWords");
        numWords = input.next();

        int price;

        if (numWords >= 50) {
            price = ((numWords -50) * .08) + 5.00;
            System.out.println("customer:" + name);
            System.out.println("Placed an ad in catagory:" + catagory);
            System.out.println("Ad length is" + numWords + "words, at a price of $" + price);
        }
        else {
            price = numWords * .10;
            System.out.println("customer:" + name);
            System.out.println("Placed an ad in catagory:" + catagory);
            System.out.println("Ad length is" + numWords + "words, at a price of $" + price);
        }

    }
}

这些行会出现相同的错误,但结尾处带有不同的符号

  • if (numWords >= 50) {

    二进制运算符'<='的错误操作数类型

  • price = ((numWords -50) * .08) + 5.00;

    二进制运算符'-'的错误操作数类型

  • price = numWords * .10;

    二进制运算符'*'的错误操作数类型

我该如何解决

您的变量numWords是字符串,请先将其解析为整数以进行比较。 然后,将price也更新一倍。 示例代码如下。

    int numWords;
    System.out.print("numWords");
    numWords = input.nextInt();

    double price;

    if (numWords >= 50) {
        price = ((numWords -50) * .08) + 5.00;
        System.out.println("customer:" + name);
        System.out.println("Placed an ad in catagory:" + catagory);
        System.out.println("Ad length is" + numWords + "words, at a price of $" + price);
    }

numWords是一个String ,而不是一个int 解析String以获得一个整数。

int num_words = Integer.parseInt(numWords);

你在做什么错

if (numWords >= 50) {

numWords是一个字符串, >=仅适用于数字。

怎么修

您有2个解决方案:

  • 以字符串形式读取数字并将其转换为数字

     temp = input.nextLine(); numWords = Integer.parseInt(temp); 

    这种方式意味着您可以手动检查,并且如果号码错误则不需要捕获异常。

  • 立即将数字读为数字

     numWords = input.nextInt(); 

    这样可以减少代码量,但是如果输入不是整数,则需要捕获NumberFormatException

其他注意事项

  • input.next()使用input.next() ,根据您的输入,您可能想使用nextLine代替
  • 您可能要使用System.out.printf清理打印代码

在这种情况下,您需要从字符串解析为整数。 这里有一个例子:

String simplenumber= "10";
int num = Integer.parseInt(simplenumber);

在您的代码中:

public static void main(String [] args){扫描仪输入=新的Scanner(System.in);

    String name;
    System.out.print("name");
    name = input.next();

    String catagory;
    System.out.print("Catagory");
    catagory = input.next();

    String numWords;
    System.out.print("numWords");
    numWords = input.next();

    int price;
    int numWordsInt = Integer.parseInt(numWords);

    if (numWordsInt >= 50) {
        price = (int) (((numWordsInt -50) * .08) + 5.00);
        System.out.println("customer:" + name);
        System.out.println("Placed an ad in catagory:" + catagory);
        System.out.println("Ad length is" + numWords + "words, at a price of $" + price);
    }
    else {
        price = (int) (numWordsInt * .10);
        System.out.println("customer:" + name);
        System.out.println("Placed an ad in catagory:" + catagory);
        System.out.println("Ad length is" + numWords + "words, at a price of $" + price);
    }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM