Java 赢的高低博弈逻辑
迷路的新手! 请改进代码,因为它从不显示条件。以及我需要把四个胜利在一个连续的用户赢得。请帮忙Java 赢的高低博弈逻辑,java,Java,迷路的新手! 请改进代码,因为它从不显示条件。以及我需要把四个胜利在一个连续的用户赢得。请帮忙 import java.util.Scanner; import java.util.Random; public class HiLoGame { public static void main (String[] args) { String guess=null; String high= "high"; String low="low"; Stri
import java.util.Scanner;
import java.util.Random;
public class HiLoGame {
public static void main (String[] args) {
String guess=null;
String high= "high";
String low="low";
String equal = "equal";
int nextCard;
int card=3;
System.out.println("Current card is: "+ card);
if(card==11){
System.out.println ("Which means it is card jack!");
}else if(card==12){
System.out.print("which means it is card queen!");
}else if(card==13){
System.out.println("Which means it is card king!");
}else if (card==14){
System.out.println("Which means it is card Ace!");
}
System.out.println("WELCOME! to High-Low game.");
System.out.println("Guess four times in a row to win.");
while(true){
Random generator = new Random();
nextCard = generator.nextInt(14)+1;
System.out.println("Next card will be high, low or equal?");
Scanner input = new Scanner(System.in);
guess = input.next();
System.out.println("It is --> "+ nextCard);
card = generator.nextInt(14)+2;
nextCard = generator.nextInt(14)+2;
System.out.println("Next card will be high, low or equal?");
Scanner input1 = new Scanner(System.in);
guess = input1.next();
System.out.println("It is --> "+ nextCard);
while(true){
if (guess.equals(high))
{
if (card < nextCard)
{
System.out.println("NICE GUESS ");
System.out.println("KEEP PLAYING");
break;
}
else
{
System.out.println("Sorry WRONG GUESS!");
System.out.println("Better luck next time");
System.exit(0);
}
}
else if (guess.equals(low))
{
if (card > nextCard)
{
System.out.println("NICE GUESS");
System.out.println("KEEP PLAYING");
break;
}
else
{
System.out.println("Sorry WRONG GUESS!");
System.out.println("Better luck next time");
}
}
else if(guess.equals(equal))
{
if (card==nextCard)
{
System.out.println("NICE GUESS");
System.out.println("KEEP PLAYING");
break;
}
else
{
System.out.println("Sorry WRONG GUESS");
System.out.println("Better luck next time!");
System.exit(0);
}
}
}}}}
import java.util.Scanner;
导入java.util.Random;
公共类HiLoGame{
公共静态void main(字符串[]args){
字符串猜测=null;
字符串高=“高”;
字符串low=“low”;
字符串equal=“equal”;
int nextCard;
积分卡=3;
System.out.println(“当前卡为:“+卡”);
如果(卡==11){
System.out.println(“这意味着它是卡插孔!”);
}否则,如果(卡==12){
System.out.print(“这意味着它是卡片女王!”);
}否则,如果(卡==13){
System.out.println(“这意味着它是卡王!”);
}否则,如果(卡==14){
System.out.println(“这意味着它是卡片Ace!”);
}
System.out.println(“欢迎来到高低游戏”);
System.out.println(“连续猜四次就赢了”);
while(true){
随机生成器=新随机();
nextCard=generator.nextInt(14)+1;
System.out.println(“下一张卡将是高、低还是相等?”);
扫描仪输入=新扫描仪(System.in);
guess=input.next();
System.out.println(“它是-->”+nextCard);
卡=发电机。nextInt(14)+2;
nextCard=generator.nextInt(14)+2;
System.out.println(“下一张卡将是高、低还是相等?”);
扫描仪输入1=新扫描仪(System.in);
guess=input1.next();
System.out.println(“它是-->”+nextCard);
while(true){
如果(猜测等于(高))
{
如果(卡下一卡)
{
System.out.println(“很好的猜测”);
System.out.println(“继续播放”);
打破
}
其他的
{
System.out.println(“对不起,猜错了!”);
System.out.println(“下次好运”);
}
}
else如果(猜测等于(相等))
{
如果(卡==nextCard)
{
System.out.println(“很好的猜测”);
System.out.println(“继续播放”);
打破
}
其他的
{
System.out.println(“对不起,猜错了”);
System.out.println(“祝你下次好运!”);
系统出口(0);
}
}
}}}}
您的代码有点奇怪和混乱,但我花了时间仔细阅读了一遍,有任何问题请随时提问:
import java.util.Scanner;
import java.util.Random;
public class HiLoGame {
public static void main(String[] args) {
Random generator = new Random();
Scanner input = new Scanner(System.in);
String guess = null, result = null;
Boolean won = false;
int nextCard, card = 3, count = 0;
System.out.println("Current card is: " + card);
switch (card) {
case 11:
System.out.println("Which means it is card jack!");
break;
case 12:
System.out.print("which means it is card queen!");
break;
case 13:
System.out.print("which means it is card king!");
break:
case 14:
System.out.println("Which means it is card Ace!");
break;
}
System.out.println("WELCOME! to High-Low game.\nGuess four times in a row to win.");
while (!won) {
nextCard = generator.nextInt(14) + 1;
System.out.println("You current card is: " + card + "\nWill the next card be high, low or equal?");
guess = input.next().toLowerCase();
System.out.println("The next card is:" + nextCard);
if(card<nextCard){
result = "high";
}
else if(card>nextCard){
result = "low";
}
else if(card==nextCard){
result = "equal";
}
if(guess.equals(result)){
System.out.println("NICE GUESS\nKEEP PLAYING");
card = nextCard;
count++;
}
else {
System.out.println("Sorry WRONG GUESS!\nBetter luck next time");
count=0;
}
if(count==4){
System.out.println("Congratulations, you have beaten the game!!!\nWould you like to play again? Yes/No");
guess = input.next().toLowerCase();
if(guess.equals("yes"))
{
count = 0;
card = generator.nextInt(14) + 1;
}
else{
System.out.println("Thank you for playing, goodbye");
System.exit(0);
}
}
}
}
}
import java.util.Scanner;
导入java.util.Random;
公共类HiLoGame{
公共静态void main(字符串[]args){
随机生成器=新随机();
扫描仪输入=新扫描仪(System.in);
字符串猜测=null,结果=null;
布尔值=false;
int nextCard,卡片=3,计数=0;
System.out.println(“当前卡为:“+卡”);
开关(卡){
案例11:
System.out.println(“这意味着它是插卡器!”);
打破
案例12:
System.out.print(“这意味着它是卡片女王!”);
打破
案例13:
System.out.print(“这意味着它是卡王!”);
中断:
案例14:
System.out.println(“这意味着它是卡片Ace!”);
打破
}
System.out.println(“欢迎!进入高低游戏。\n连续四次获胜。”);
而(!元){
nextCard=generator.nextInt(14)+1;
System.out.println(“您当前的卡是:“+card+”\n下一张卡是高、低还是相等?”);
guess=input.next().toLowerCase();
System.out.println(“下一张卡是:“+nextCard”);
如果(cardnextCard){
结果=“低”;
}
else if(卡==nextCard){
结果=“相等”;
}
如果(猜测等于(结果)){
System.out.println(“很好的猜测\n继续播放”);
card=nextCard;
计数++;
}
否则{
System.out.println(“对不起,猜错了!\n祝下次好运”);
计数=0;
}
如果(计数=4){
System.out.println(“祝贺你,你赢了比赛!!!\n你想再玩一次吗?是/否”);
guess=input.next().toLowerCase();
如果(猜测等于(“是”))
{
计数=0;
卡=发电机。nextInt(14)+1;
}
否则{
System.out.println(“感谢您的参与,再见”);
系统出口(0);
}
}
}
}
}
祝你学习顺利
与所讨论的代码相比,本代码中使用的一些内容包括:
我使用了一个开关块,它代替了多个if语句,你可以先传入变量,然后再传入行:如果你输入了你想要匹配的内容,这看起来比使用多个if语句更好,效率也更高
我还在打印行中使用了\n这是换行符的转义xhar。您可以使用它来代替多个打印行语句
您应该尝试在代码中尽量减少重复文本的数量,我的代码远不是完美的,但如果它是完美的,那么您就没有什么可研究的了
与其调用多个if语句,还不如尝试执行以下操作