代码编辑帮助

时间:2017-04-28 09:58:32

标签: java

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");
               }
            }

  }
}

此代码运行,我的问题是它没有按我希望的方式运行。我想要的是if语句和方法导致只打印出一个tarot。现在,当程序运行时,它打印出所有塔罗牌。我认为我的错误在于如何构建我的if语句,任何建议?

2 个答案:

答案 0 :(得分:0)

您的ifelse条件结构不正确。 if else的正确语法如下:

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

试用此版本的代码:

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();
    }
}

答案 1 :(得分:0)

您必须将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();
  }

当然,在if。

之后删除分号

用于在Java中传递参数:Is Java "pass-by-reference" or "pass-by-value"?