在Java中使用If语句时不断出现错误。

huangapple go评论65阅读模式
英文:

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(&quot;Enter the purchase price [0-100]: &quot;);
int price = scan.nextInt();
System.out.println(&quot;Enter the amount paid: &quot;);
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 &gt;= 0 || &lt;= 100){
System.out.print(&quot;Your change of &quot; + purchase + &quot; cents is given as:&quot;);
}else
System.out.println(&quot;Purchase must between 0 and 100 cents.&quot;);
System.out.println(&quot; &quot; + dollars + &quot; dollars&quot;);
System.out.println(&quot; &quot; + quarters + &quot; Quarters&quot;);
System.out.println(&quot; &quot; + dimes + &quot; Dimes&quot;);
System.out.println(&quot; &quot; + nickels + &quot; Nickels&quot;);
System.out.println(&quot; &quot; + pennies + &quot; Pennies&quot;);
}
}

答案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. =)

huangapple
  • 本文由 发表于 2020年9月7日 04:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63768509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定