我无法弄清楚如何解决这些错误,我一直在寻找我的代码
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;
二元运算符' *'
的错误操作数类型
如何修复这些
答案 0 :(得分:1)
您的变量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);
}
答案 1 :(得分:1)
numWords
是String
,而不是int
。解析String以获得int。
int num_words = Integer.parseInt(numWords);
答案 2 :(得分:1)
if (numWords >= 50) {
numWords是一个字符串,>=
仅适用于数字。
您有2种方法可以解决此问题:
以字符串形式读取数字并将其转换为数字
temp = input.nextLine();
numWords = Integer.parseInt(temp);
这种方式意味着您可以手动检查,如果数字错误则无需捕获异常。
立即以数字形式读取数字
numWords = input.nextInt();
这种方式代码较少,但如果输入不是整数,则需要捕获NumberFormatException
。
input.next()
,具体取决于您可能想要使用的输入nextLine
System.out.printf
来清理打印代码答案 3 :(得分:0)
在这种情况下,您需要从字符串解析为整数。这里有一个例子:
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);
}}
在您的代码中:
public static void main(String [] args){ 扫描仪输入=新扫描仪(System.in);
[['English 1', 4, 3, 'Fall', '2009'],
['English 2', 3.7, 3, 'Spring', '2010'],
['English 3', 2.7, 3, 'Fall', '2010'],
['English 4', 3.0, 3, 'Spring', '2011'],
['English 5', 3.7, 3, 'Fall', '2011'],
['English 6', 3.3, 3, 'Spring', '2012'],
['Math 1', 3.3, 3, 'Fall', '2009'],
['Math 2', 2.7, 3, 'Spring', '2010'],
['Science 1', 3.7, 4, 'Fall', '2009'],
['Science 2', 4, 4, 'Spring', '2010']]