简体   繁体   English

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

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

I'm having trouble figuring out how to fix these errors I keep getting for my code 我在弄清楚如何解决我的代码中不断遇到的错误时遇到了麻烦

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);
        }

    }
}

These are the lines that are getting the same error but with a different symbol at the end 这些行会出现相同的错误,但结尾处带有不同的符号

  • if (numWords >= 50) {

    bad operand types for binary operator '<=' 二进制运算符'<='的错误操作数类型

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

    bad operand types for binary operator '-' 二进制运算符'-'的错误操作数类型

  • price = numWords * .10;

    bad operand types for binary operator '*' 二进制运算符'*'的错误操作数类型

How can I fix these 我该如何解决

your variable numWords is string, please parse it to integer first for comparing. 您的变量numWords是字符串,请先将其解析为整数以进行比较。 Then, update price to double as well. 然后,将price也更新一倍。 Sample code is as per below. 示例代码如下。

    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 is a String , not an int . numWords是一个String ,而不是一个int Parse the String to get an int. 解析String以获得一个整数。

int num_words = Integer.parseInt(numWords);

What you are doing wrong 你在做什么错

if (numWords >= 50) {

numWords is a string, and >= only works on numbers. numWords是一个字符串, >=仅适用于数字。

How to fix 怎么修

You have 2 options for fixing this: 您有2个解决方案:

  • Read the number in as a String and convert it to a number 以字符串形式读取数字并将其转换为数字

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

    This way means you can check manually, and do not need to catch an exception if the number is wrong. 这种方式意味着您可以手动检查,并且如果号码错误则不需要捕获异常。

  • Read the number in as a number straight away 立即将数字读为数字

     numWords = input.nextInt(); 

    This way is less code, but you will need to catch a NumberFormatException if the input is not an integer. 这样可以减少代码量,但是如果输入不是整数,则需要捕获NumberFormatException

Other notes 其他注意事项

  • You use input.next() a lot, depending on your inputs you may want to use nextLine instead input.next()使用input.next() ,根据您的输入,您可能想使用nextLine代替
  • You may want to use System.out.printf to clean up your printing code 您可能要使用System.out.printf清理打印代码

In this situation you need parse from string to integer. 在这种情况下,您需要从字符串解析为整数。 Here you have an example: 这里有一个例子:

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

In your code: 在您的代码中:

public static void main(String[] args) { Scanner input = new Scanner(System.in); 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