丢了新来的!请改进代码,它从不显示条件。此外,我需要把四连胜为用户赢得。救命啊!!
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);
}
}
}}}}发布于 2014-11-09 17:30:51
您的代码有点奇怪和混乱,但我已经花了时间仔细研究它,任何问题都可以随意问:
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);
}
}
}
}
}祝你学习顺利!
与所讨论的代码相比,该代码中使用的一些内容如下:
我使用了一个开关块,这是用来代替多个if语句的,您可以传入变量,然后在行中:如果您想要匹配它,这看起来要好得多,而且比使用多个if语句高效得多。
我还在打印行中使用了一个\n,这是换行符的转义xhar。您可以使用它来代替多个打印行语句。
您应该尝试在您的代码中尽量减少重复文本的数量,我的代码远非完美,但如果是的话,您就没有什么可查的了。
不调用多个if语句也尝试执行一组if语句来检查一个条件,然后预先保存结果,这会改变比较的数量.
希望这对你有意义。
https://stackoverflow.com/questions/26830377
复制相似问题