gpt4 book ai didi

java - 需要在GWT中运行Java代码

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:10 25 4
gpt4 key购买 nike

我创建了一个新的 google web 应用程序项目(使用 eclipse),并注意到代码在 .server 包中运行。我有一个用 java 编写的老虎机游戏,并试图将其实现到 GWT 中,但语法似乎完全不同。 (例如,我注意到\n 不起作用)

这是我想要实现的代码 - 我到底该怎么做?

// clear console
static void clearConsole()
{
for (int i=0; i<25; i++)
{
System.out.println("\n");
}
}


// actual slot machine algorithm
static void slots()
{

Scanner input = new Scanner (System.in);

char newGame = ' ';

int chips = 100;
int bet = 0;


do{
System.out.println( "Try your luck on the SLOT MACHINE." +
"\nLine up (=^^=) or ))<>(( symbols to win big!\n\n" +
"You currently have " + chips + " chips.\nHow much do you want to bet? ");



//check for accidental char input
try{
bet = input.nextInt();

}
catch(Exception e){

input.nextLine();
System.out.println("NOT A VALID NUMBER\nHow much do you want to bet? ");
bet = input.nextInt();
}



if (bet<=chips && bet>0){

// to add some realism, slot machine will not execute until 'enter' pressed
// then console cleared
input.nextLine();
System.out.println( "\nPress 'enter' to start the slot machine.");
input.nextLine();
clearConsole();

String[] machine = {"(=^^=)", "(=^^=)", "))<>((", " XXXX ", " XXXX ", " :^{) ",
" :^{) ", " (>_<)", " (>_<)", " [-O-]", " [-O-]"};
Random rand = new Random();
int slot1 = rand.nextInt(11);
int slot2 = rand.nextInt(11);
int slot3 = rand.nextInt(11);
int slot4 = rand.nextInt(11);
int slot5 = rand.nextInt(11);
int slot6 = rand.nextInt(11);
int slot7 = rand.nextInt(11);
int slot8 = rand.nextInt(11);
int slot9 = rand.nextInt(11);

System.out.println( "-----------------------");
System.out.printf( "%-7s %-7s %-7s %n%n", machine[slot1], machine[slot2], machine[slot3]);
System.out.printf( "%-7s %-7s %-7s %n%n", machine[slot4], machine[slot5], machine[slot6]);
System.out.printf( "%-7s %-7s %-7s %n", machine[slot7], machine[slot8], machine[slot9]);
System.out.println( "-----------------------\n\n\n\n");

// 3 wild cards
if (slot4 == 2 && slot5 == 2 && slot6 == 2 ){
bet = bet*100;
chips = chips + bet;
System.out.println( "JACKPOT! ");
}

//3 cats (inclusive of wild card)
else if ( slot4 <3 && slot5 <3 && slot6 <3 ){
bet = bet*5;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}

// 3 of any other symbol (inclusive of wild card)
else if( ((slot4==2 || slot4==3 || slot4==4) && (slot5==2 || slot5==3 ||
slot5==4) && (slot6==2 || slot6==3 || slot6==4))
|| ((slot4==2 || slot4==5 || slot4==6) && (slot5==2 || slot5==5 ||
slot5==6) && (slot6==2 || slot6==5 || slot6==6))
|| ((slot4==2 || slot4==7 || slot4==8) && (slot5==2 || slot5==7 ||
slot5==8) && (slot6==2 || slot6==7 || slot6==8))
|| ((slot4==2 || slot4==9 || slot4==10) && (slot5==2 || slot5==9 ||
slot5==10) && (slot6==2 || slot6==9 || slot6==10)) ){
bet = bet*3;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}

// 2 cats
else if ( slot4<2 && slot5<2 || slot4<2 && slot6<2 || slot5<2 && slot6<2){
bet = bet*2;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}

// 1 cat
else if ( slot4 < 2 || slot5 < 2 || slot6 < 2 ){
chips = chips + bet;
System.out.println( "YOU WIN! ");
}

// nothing
else{
chips = chips - bet;
System.out.print( "You Lose... ");
}

// display current amount of chips
System.out.println( "You have " + chips + " chips.");


}else{
System.out.println( "You do not have enough chips to make that bet!");
}


if (chips > 0){
System.out.println( "\nDo you wanna play this game again? (y/n): ");
newGame = input.next().charAt(0);
clearConsole();
}

} while (newGame =='y' && chips > 0 );


input.close();

System.out.println( "\nGame Over\n\n");
}

最佳答案

您可以将 System.out.println 替换为将原始文本(甚至不是 HTML)放入具有固定空间等的元素中(类似于“pre”元素)。

我建议您切换到“输入”按钮、“y”/“n”...和 ​​DoubleBox 等按钮来表示他们想要下注的金额。

浏览器方法中可能有一个更通用的“控制台”,但这会有点奇怪?

也许最好从 GWT 示例之一开始,然后慢慢转换为您想要的内容?需要相当多的引导才能启动并运行 GWT 应用程序。

关于java - 需要在GWT中运行Java代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com