我正在编写我的第一个java代码,但我无法运行它。当我运行代码时,它告诉我有错误,但在底部的控制台中没有任何东西让我看到错误可能是什么。请帮帮忙?
package operators;
import java.util.Scanner;
public class assignment2ifelse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int quantity;
int packages = 99;
quantity = input.nextInt();
double dis1 = .2; // dis2 = .33, dis3 = .42, dis4 = .29;
double price = packages * quantity;
double discount = 0;
double finalprice = price * discount;
if (quantity < 20 && quantity > 9) {
System.out.println(price);
discount = price * dis1;
System.out.println(discount);
System.out.println(finalprice);
}
}
}
答案 0 :(得分:3)
试试这段代码:
package operators;
import java.util.Scanner;
public class assignment2ifelse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int quantity;
int packages = 99;
System.out.println("Enter a quantity : ");
quantity = input.nextInt();
double dis1 = .2; // dis2 = .33, dis3 = .42, dis4 = .29;
double price = packages * quantity;
double discount = 0;
double finalprice = price * discount;
if (quantity < 20 && quantity > 9) {
System.out.println(price);
discount = price * dis1;
System.out.println(discount);
System.out.println(finalprice);
} else {
System.out.println("Quantity doesn't match expectations.");
}
}
}
实际上,该程序运行但它没有向您显示任何内容,因为它会立即通过等待用户的输入开始:
quantity = input.nextInt();
然后,只有当您的条目符合您的if测试条件时,它才会向您显示。
下次尝试通过使用System.out.print()
提示消息来创建与用户的互动。