英文:
Keep getting error with If statement in java
问题
import java.util.Scanner;
public class RefundAmount {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int change;
int dollars, quarters, dimes, nickels, pennies;
System.out.println("Enter the purchase price [0-100]: ");
int price = scan.nextInt();
System.out.println("Enter the amount paid: ");
int paid = scan.nextInt();
change = price - paid;
dollars = (change / 100);
change = change % 100;
quarters = (change / 25);
change = change % 25;
dimes = (change / 10);
change = change % 10;
nickels = (change / 5);
change = change % 5;
pennies = change;
if (change >= 0 && change <= 100) {
System.out.print("Your change of " + change + " cents is given as:");
} else {
System.out.println("Purchase must be between 0 and 100 cents.");
}
System.out.println(" " + dollars + " dollars");
System.out.println(" " + quarters + " Quarters");
System.out.println(" " + dimes + " Dimes");
System.out.println(" " + nickels + " Nickels");
System.out.println(" " + pennies + " Pennies");
}
}
英文:
I am taking a java course in college and one of the homework assignments is to write a program that reads an integer between 0 and 100[inclusive], representing the amount of purchase. The issue I am having is the if-else statement. Every time I compile, it would say the less than equal sign is an illegal start of type. Can someone explain to me why that is and how I can fix it? Thanks in advance!
import java.util.Scanner;
public class RefundAmount {
public static void main(String [] args) {
Scanner scan = new Scanner (System.in);
int change;
int dollars, quarters, dimes, nickels, pennies;
System.out.println("Enter the purchase price [0-100]: ");
int price = scan.nextInt();
System.out.println("Enter the amount paid: ");
int paid = scan.nextInt();
change = price - paid;
dollars = (change / 100);
change = change % 100;
quarters = (change / 25);
change = change % 25;
dimes = (change / 10);
change = change % 10;
nickels = (change / 5);
change = change % 5;
pennies = change;
if (change >= 0 || <= 100){
System.out.print("Your change of " + purchase + " cents is given as:");
}else
System.out.println("Purchase must between 0 and 100 cents.");
System.out.println(" " + dollars + " dollars");
System.out.println(" " + quarters + " Quarters");
System.out.println(" " + dimes + " Dimes");
System.out.println(" " + nickels + " Nickels");
System.out.println(" " + pennies + " Pennies");
}
}
答案1
得分: 1
以下是翻译好的内容:
你的代码有一些问题。
> if (change >= 0 || <= 100)
应改为
> if (change >= 0 || change <= 100)
这就是为什么会出现错误。
除此之外,当你使用 ||(或)时,两个条件都会被验证,但实际上你并不需要这样。相反,最好使用 &&(且)。使用 and 时,如果第一个条件不符合,下一个条件将不会被验证。
还有一件事是,在你的代码中除了 print 语句之外从未引用过 purchase。
附注:不知道你使用的是哪个IDE或文本编辑器,但我建议使用Visual Studio Code或IntelliJ。
英文:
There are some things wrong with your code.
> if (change >= 0 || <= 100)
Should be
> if (change >= 0 || change <= 100)
And that's why you get said error.
Besides that, when you're using || (or) the both conditions will be verified, but you really don't need that. Instead with would be better to use && (and). With and, if the first condition fails, the next won't be verified.
Another thing nothing is that purchase is never referenced in your code besides the print statement.
P.S: Don't know if what IDE or text editor you're using, but I would advise Visual Studio Code or IntelliJ.
答案2
得分: 0
如果你想在代码中寻找数字,应该像这样;
如果(change >= 0 && change <= 100){
}
|| 是或语句,你应该使用 && 来指定大于0且小于100的数字。
英文:
If you want to find numbers between code should be like;
if(change >= 0 && change <= 100){
}
|| is or statement you should use && to specify numbers larger than 0 and smaller than 100.
答案3
得分: 0
每次使用或运算符时,都必须包含变量。这就是为什么它会说你不能以 <= 符号开头。希望这可以帮助。 =)
英文:
Each time you use an or operator you have to include the variable. And that's why it says you can't start with a <= sign. Hope this helps. =)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论