简体   繁体   English

代码编辑协助

[英]Code editing assistance

import java.util.*;
public class TarotCard
{
   public static void main(String[] args)
   {
   Scanner scanner = new Scanner(System.in);
   Number();
   int a = placeholder;
   Number();
   int b = placeholder;
   Number();
   int c = placeholder;
   Number();
   int d = placeholder;
   int total = a + b + c + d;
   if (toatal <= 9)
      {
      Hearts();
      }
   else
      {
      if (total <= 18)
      {
      Spades();
      }
      else
         {
         if(total <= 27)
            {
            Clubs();
            }
         else
            {
            Diamonds();
            }
         }
      }
   }
}

  public int Number()
  {
  System.out.println("please enter a one digit number");
 import java.util.*;
public class TarotCard
{
   public static void main(String[] args)
   {
   Scanner scanner = new Scanner(System.in);
   int placeholder = 0;
   Number(placeholder, scanner);
   int a = placeholder;
   int total = a;
   Number(placeholder, scanner);
   int b = placeholder;
   total = b + total;
   Number(placeholder, scanner);
   int c = placeholder;
   total = c + total;
   Number(placeholder , scanner);
   int d = placeholder;
   total = d + total;
   if(total <= 9)
      {
      Hearts();
      }
   if(total > 9 && total <= 18)
      {
      Clubs();
      }
   if(total > 18 && total <= 27)
      {
      Spades();
      }
   if(total > 27 && total <= 36)
      {
      Diamonds();
      }


   }


  public static int Number(int placeholder,Scanner scanner)
  {
  System.out.println("please enter a one digit number");
  placeholder = scanner.nextInt();
  return placeholder;
  }

  public static void Hearts()
  {
  Random deck = new Random();
  int cardtype = deck.nextInt(52)+1;
         if(cardtype > 13);
            {
            cardtype = deck.nextInt(2); 
            if(cardtype == 0)
               {
               System.out.println("your tarrot is king of hearts");
               }
            if(cardtype == 1)
               {
               System.out.println("your tarrot is queen of hearts");
               }
            if(cardtype == 2)
               {
               System.out.println("your tarrot is jack of hearts");
               }
            }
         if(cardtype < 12);
            {
            cardtype = deck.nextInt(10)+1;
            if(cardtype == 1);
               {
               System.out.println("your tarot is ace of hearts");
               }
            if(cardtype == 2);
               {
               System.out.println("your tarot is two of hearts");
               }
            if(cardtype == 3);
               {
               System.out.println("your tarot is three of hearts");
               }
            if(cardtype == 4);
               {
               System.out.println("your tarrot is four of hearts");
               }
            if(cardtype == 5);
               {
               System.out.println("your tarrot is 5 of hearts");
               }
            if(cardtype == 6);
               {
               System.out.println("your tarrot is 6 of hearts");
               }
            if(cardtype == 7);
               {
               System.out.println("your tarrot is 7 of hearts");
               }
            if(cardtype == 8);
               {
               System.out.println("your tarrot is 8 of hearts");
               }
            if(cardtype == 9);
               {
               System.out.println("your tarrot is 9 of hearts");
               }
            if(cardtype == 10);
               {
               System.out.println("your tarrot is 10 of hearts");
               }
            }

  }
  public static void Spades()
  {
  Random deck = new Random();
  int cardtype = deck.nextInt(52)+1;
         if(cardtype > 13);
            {
            cardtype = deck.nextInt(2); 
            if (cardtype == 0)
               {
               System.out.println("your tarrot is king of spades");
               }
            if (cardtype == 1)
               {
               System.out.println("your tarrot is queen of spades");
               }
            if (cardtype == 2)
               {
               System.out.println("your tarrot is jack of spades");
               }
            }
         if(cardtype < 12);
            {
            cardtype = deck.nextInt(10)+1;
            if (cardtype == 1);
               {
               System.out.println("your tarot is ace of spades");
               }
            if (cardtype == 2);
               {
               System.out.println("your tarot is two of spades");
               }
            if (cardtype == 3);
               {
               System.out.println("your tarot is three of spades");
               }
            if (cardtype == 4);
               {
               System.out.println("your tarrot is four of spades");
               }
            if (cardtype == 5);
               {
               System.out.println("your tarrot is 5 of spades");
               }
            if (cardtype == 6);
               {
               System.out.println("your tarrot is 6 of spades");
               }
            if (cardtype == 7);
               {
               System.out.println("your tarrot is 7 of spades");
               }
            if (cardtype == 8);
               {
               System.out.println("your tarrot is 8 of spades");
               }
            if (cardtype == 9);
               {
               System.out.println("your tarrot is 9 of spades");
               }
            if (cardtype == 10);
               {
               System.out.println("your tarrot is 10 of spades");
               }
            }  
  }
  public static void Clubs()
  {
  Random deck = new Random();
  int cardtype = deck.nextInt(52)+1;
         if(cardtype > 13);
            {
            cardtype = deck.nextInt(2); 
            if (cardtype == 0)
               {
               System.out.println("your tarrot is king of clubs");
               }
            if (cardtype == 1)
               {
               System.out.println("your tarrot is queen of clubs");
               }
            if (cardtype == 2)
               {
               System.out.println("your tarrot is jack of clubs");
               }
            }
         if(cardtype < 12);
            {
            cardtype = deck.nextInt(10)+1;
            if (cardtype == 1);
               {
               System.out.println("your tarot is ace of clubs");
               }
            if (cardtype == 2);
               {
               System.out.println("your tarot is two of clubs");
               }
            if (cardtype == 3);
               {
               System.out.println("your tarot is three of clubs");
               }
            if (cardtype == 4);
               {
               System.out.println("your tarrot is four of clubs");
               }
            if (cardtype == 5);
               {
               System.out.println("your tarrot is 5 of clubs");
               }
            if (cardtype == 6);
               {
               System.out.println("your tarrot is 6 of clubs");
               }
            if (cardtype == 7);
               {
               System.out.println("your tarrot is 7 of clubs");
               }
            if (cardtype == 8);
               {
               System.out.println("your tarrot is 8 of clubs");
               }
            if (cardtype == 9);
               {
               System.out.println("your tarrot is 9 of clubs");
               }
            if (cardtype == 10);
               {
               System.out.println("your tarrot is 10 of clubs");
               }
            }
  }
  public static void Diamonds()
  {
  Random deck = new Random();
  int cardtype = deck.nextInt(52)+1;
         if(cardtype > 13);
            {
            cardtype = deck.nextInt(2); 
            if (cardtype == 0)
               {
               System.out.println("your tarrot is king of diamonds");
               }
            if (cardtype == 1)
               {
               System.out.println("your tarrot is queen of diamonds");
               }
            if (cardtype == 2)
               {
               System.out.println("your tarrot is jack of diamonds");
               }
            }
         if(cardtype < 12);
            {
            cardtype = deck.nextInt(10)+1;
            if (cardtype == 1);
               {
               System.out.println("your tarot is ace of diamonds");
               }
            if (cardtype == 2);
               {
               System.out.println("your tarot is two of diamonds");
               }
            if (cardtype == 3);
               {
               System.out.println("your tarot is three of diamonds");
               }
            if (cardtype == 4);
               {
               System.out.println("your tarrot is four of diamonds");
               }
            if (cardtype == 5);
               {
               System.out.println("your tarrot is 5 of diamonds");
               }
            if (cardtype == 6);
               {
               System.out.println("your tarrot is 6 of diamonds");
               }
            if (cardtype == 7);
               {
               System.out.println("your tarrot is 7 of diamonds");
               }
            if (cardtype == 8);
               {
               System.out.println("your tarrot is 8 of diamonds");
               }
            if (cardtype == 9);
               {
               System.out.println("your tarrot is 9 of diamonds");
               }
            if (cardtype == 10);
               {
               System.out.println("your tarrot is 10 of diamonds");
               }
            }

  }
}

This code runs, my problem is that it doesn't run how I want it to. 这段代码可以运行,但我的问题是它无法按我希望的方式运行。 What I want is for the if statements and methods to lead to only one tarot being printed out. 我想要的是if语句和方法导致只打印一张塔罗牌。 Right now when the program is run it prints out all of the tarots. 现在,当程序运行时,它会打印出所有的塔罗牌。 I'm thinking my mistakes lie within how i structured my if statements, any suggestions? 我认为我的错误在于我如何构造if语句,有什么建议吗?

Your if and else conditions are structured incorrectly. 您的ifelse条件的结构不正确。 The correct syntax for if else is something like this: if else的正确语法如下所示:

if (condition1) {
    // do something
}
else if (condition2) {
    // do something else
}
else {
    // other
}

Try this version of your code: 尝试使用此版本的代码:

public static void main (String[] args) {
    Scanner scanner = new Scanner(System.in);
    Number();
    int a = placeholder;
    Number();
    int b = placeholder;
    Number();
    int c = placeholder;
    Number();
    int d = placeholder;
    int total = a + b + c + d;

    if (toatal <= 9) {
        Hearts();
    }
    else if (total <= 18) {
        Spades();
    }
    else if (total <= 27) {
        Clubs();
    }
    else {
        Diamonds();
    }
}

You have to assign the returned value of your Number() method to an int variable, the parameter placeholder won't change the value of placeholder in main class. 您必须将Number()方法的返回值分配给一个int变量,参数占位符不会更改主类中占位符的值。

public static void main(String[] args)
   {
   int a = Number();
   int total = a;      
   int b = Number();
   total = b + total;      
   int c = Number();
   total = c + total;
   int d =  Number();
   total = d + total;
   if(total <= 9)
      {
      Hearts();
      }
   if(total > 9 && total <= 18)
      {
      Clubs();
      }
   if(total > 18 && total <= 27)
      {
      Spades();
      }
   if(total > 27 && total <= 36)
      {
      Diamonds();
      }
   }


  public static int Number()
  {
      Scanner scanner = new Scanner(System.in);
      System.out.println("please enter a one digit number");
      return scanner.nextInt();
  }

And of course remove semicolons after the if. 当然,如果要删除分号。

For parameter passing in Java: Is Java "pass-by-reference" or "pass-by-value"? 对于Java中的参数传递:Java 是“按引用传递”还是“按值传递”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM