【问题标题】:This else statement is stopping me from going to bed [duplicate]这个else语句阻止我上床睡觉[重复]
【发布时间】:2013-12-04 04:19:45
【问题描述】:

我知道它可能是世界上最无脑的答案,但我不明白为什么在适合答案下的第一个 if 条件之后的 else 语句不起作用,我只是想尽快上床睡觉,我想至少运行一次。

package PokerApp;
import java.util.Scanner;
public class PokerApp {

public static void main(String[] args) {

int card1 = 0;
int card2 = 0;
int play;
String fc1 = "", fc2 ="";
String answer = "";

Scanner scan = new Scanner(System.in);
System.out.println("Press 1 to evalaute your cards: ");
play = scan.nextInt();
while (play != 0){

System.out.println("First Card: ");
if (scan.hasNextInt())
{
    card1 = scan.nextInt(9)+2;
}
else{
    fc1 = scan.next();
    switch(fc1)
    {
    case "A":
        card1 = 14;
        break;
    case "K":
        card1 = 13;
        break;
    case "Q":
        card1 = 12;
        break;
    case "J":
        card1 = 11;
        break;
        default:System.out.println("Incvalid entry");
    }
}
System.out.println("Second card: ");
if (scan.hasNextInt())
{
    card2 = scan.nextInt(9) +2;
}
else{
    fc2 = scan.next();
    switch (fc2)
    {
    case "A":
        card2 = 14;
        break;
    case "K":
        card2 = 13;
        break;
    case "Q":
        card2 = 12;
        break;
    case "J":
        card2 = 11;
        break;
    default:System.out.println("Invalid entry.");
    }

}
System.out.println(card1 + "" + card2);
if(card1 == card2)
{
    System.out.println("You have a pair.");
    break;
}
//If no pair, suited engine runs conditions.
else{
    System.out.println("Are your cards suited? (y/n): ");
    answer = scan.next();
}

if (answer == "y");
{
    if(card1 == card2++ || card1++ == card2);
    {
        System.out.println("Suited connectors.");
        break;
    }

    else{
        if(card1 >= 12 && card2 >=12)
        {
            System.out.println("High value suited cards.");
            break;
        }
        else{
            if(card1 >=12 || card2 >= 12)
            {
                System.out.println("In the making for a queen high flush, or better!");
                break;
            }
            else
                System.out.println("Fold");
                break;
            }
}
//If the cards are not suited.
}

}

}
}

【问题讨论】:

  • 你到底想做什么?
  • 在您的情况下“不起作用”是什么意思?
  • 它只是说“这里不需要其他令牌”但没有它就不会运行条件,模式中一定有一些错误或其他东西。
  • switch (fc2) { case "A":{ car​​d2 = 14;打破;}案例“K”:{card2 = 13;打破;}案例“Q”:{card2 = 12;打破;}案例“J”:{card2 = 11; break;} default:{System.out.println("无效条目。");打破;}}
  • 删除你的如果,检查我的答案后的;

标签: java eclipse


【解决方案1】:

比较字符使用单引号,字符串使用双引号。还要删除 If 语句末尾的 ;,因为通过添加 ; 您正在创建一个什么都不做的空语句。

if (answer == 'y') // remove ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2022-09-24
    • 2011-03-22
    • 1970-01-01
    • 2017-10-28
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多