3的倍数打印“hippity”,4的倍数打印“hop”。

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

Multiples of 3 print hippity and on multiples of 4 print hop?

问题

//程序名称:HippityHop.java
//输入者:无名氏
//日期:10/19/2020

import java.util.Scanner;  // 需要用于Scanner类


public class HippityHop
{
    public static void main(String[] args)
    {
        // 获取扫描仪以读取输入
        Scanner keyboard = new Scanner(System.in);
      
        for(int x=1; x <= 100; x++){
            if(x % 3 != 0 && x % 4 != 0) {
                System.out.println(x);
            }else{
                if(x % 3 == 0){
                    System.out.println("Hippity");
                }
                if(x % 4 == 0){
                    System.out.println("Hop");
                }
            }
        }
    } 
}    
英文:
//Name of Program:  HippityHop.java
//Entered by:   No Name
//Date:  10/19/2020

import java.util.Scanner;  // Needed for the Scanner class


public class HippityHop
{
public static void main(String[] args)
{
 
  
 // get scanner to read input
 Scanner keyboard = new Scanner(System.in);
  
for(int x=1; x &lt;= 100; x++){
  if(x % 3 &amp;&amp; x % 4) {
     System.out.println(x);
  }else{
     if(x % 3 == 0){
        System.out.println(&quot;Hippity&quot;);
     }
     if(x % 4 == 0){
        System.out.println(&quot;Hop&quot;);
        
        }
      }
  }

}
}

I am trying to create a program that on multiples of 3 it prints "Hippity" and on multiples of 4 it prints "hop". I seem to be getting a bad operand error. What can I do to fix it?

答案1

得分: 2

以下是翻译的代码部分:

以下是翻译的代码部分

```java
if(x % 3 && x % 4) {

这不是一个正确的表达式。x % 3 正在计算余数。您从未将其与任何东西进行比较,因此它会引发错误的操作数错误。这就像在现实生活中说:

if x 取模 3 then 做这个

或者,仅出于论证的目的(并且为了更容易理解),就像是说:

if x 减去 3 then 做这个

相反,应该是 if(x % 3 != 0 && x % 4 != 0),如下所示:

import java.util.Scanner;  // 为了使用 Scanner 类

public class HippityHop {
    public static void main(String[] args) {
        // 获取扫描器以读取输入
        Scanner keyboard = new Scanner(System.in);
        
        for(int x=1; x <= 100; x++) {
            if(x % 3 != 0 && x % 4 != 0) {
                System.out.println(x);
            } else {
                if(x % 3 == 0) {
                    System.out.println("Hippity");
                }
                if(x % 4 == 0) {
                    System.out.println("Hop");
                }
            }
        }
    }
}

希望这有所帮助!

英文:

The following expression:

if(x % 3 &amp;&amp; x % 4) {

Isn't a proper one. What x%3 is doing is calculating the modulus. You never compared it to anything, so it's throwing a bad operand error. That's like saying in real life:

if x modulus 3 then do this

Or, just for the sake of the argument (and to make it easier to understand), it's like saying:

if x subtract 3 then do this

Instead, it should be if(x%3!=0 &amp;&amp; x%4!=0), like so:

import java.util.Scanner;  // Needed for the Scanner class


public class HippityHop
{
public static void main(String[] args)
{
 
  
 // get scanner to read input
 Scanner keyboard = new Scanner(System.in);
  
for(int x=1; x &lt;= 100; x++){
  if(x % 3 !=0 &amp;&amp; x % 4 !=0) {
     System.out.println(x);
  }else{
     if(x % 3 == 0){
        System.out.println(&quot;Hippity&quot;);
     }
     if(x % 4 == 0){
        System.out.println(&quot;Hop&quot;);
        
        }
      }
  }
} }

huangapple
  • 本文由 发表于 2020年10月20日 09:09:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64437111.html
匿名

发表评论

匿名网友

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

确定