gpt4 book ai didi

Java scanner.nextLine() 正在跳过

转载 作者:行者123 更新时间:2023-11-29 08:56:26 25 4
gpt4 key购买 nike

<分区>

我正在编写一个 TicTacToe 程序,由于某种原因,它跳过了玩家 1 的 scanner.nextLine(),但正在为玩家 2 停止。整个代码如下: 有问题的方法称为“名称”

package tictactoe;

import static java.lang.System.*;
import java.io.*;
import java.util.Scanner;

public class TicTacToe {

static String p1name;
static String p2name;
static String p1marker;
static String p2marker;

public static Scanner qwe = new Scanner(in);

public static void title(){
out.println("\tTicTacToe by Ryan Hosford");
out.print("\n\n\n");
out.println("1)Start"+"\t\t" + "2)How to play\t\t" + "3)Quit ");
int title = qwe.nextInt();
if(title == 1)
Names();
else if(title ==2)
Info();
else if(title ==3)
Quit();
else
title();

}

public static void Names(){
out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
out.println("Player 1, What is your name?");
Setp1Name(qwe.nextLine());
out.println("Nice to meet you " + Getp1Name());
out.println("");
out.println("Player 2, what is your name?");
Setp2Name(qwe.next());
out.println("Nice to meet you " + Getp2Name());
qwe.next();
Markers();
}



public static void Markers(){
out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
out.println(Getp1Name()+", Would you like to X's or O's?");
Setp1Marker(qwe.next());
if(Getp1Marker().equals("X") || Getp1Marker().equals("x")){
Setp2Marker("O");
}
else if(Getp1Marker().equals("O") || Getp1Marker().equals("o")){
Setp2Marker("X");
}
else{
Markers();
}
out.println(Getp1Name() + " is playing with: " +Getp1Marker());
out.println(Getp2Name() + " is playing with: " + Getp2Marker());
out.println("");
out.println("Press enter to continue...");
qwe.nextLine();
Game();
}





/////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void main(String[] args) {
title();
}

public static void Setp1Name(String player1name){
p1name = player1name;
}

public static String Getp1Name(){
return p1name;
}

public static void Setp2Name(String player2name){
p2name = player2name;
}

public static String Getp2Name(){
return p2name;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void Setp1Marker(String p1Mark){
p1Mark.toUpperCase();
p1marker = p1Mark;
}

public static String Getp1Marker(){
return p1marker;
}

public static void Setp2Marker(String p2Mark){
p2Mark.toUpperCase();
p2marker = p2Mark;
}

public static String Getp2Marker(){
return p2marker;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void Info(){
out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
out.println("TicTacToe is a game where you have to try and get 3 of your markers in a row; Be that diagonally, vertically, or horizontally.");
out.println("The two markers in TicTacToe are X and O");
out.println("The two players take turns placing one of their markers, and the game is over once a player gets 3 in a row.");
out.println("");
out.println("Press enter to continue...");
qwe.next();
title();
}

public static void Quit(){
out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
out.println("Quitting game...");
exit(0);
}
}

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