此问题已存在:
Scanner issue when using nextLine after nextXXX [duplicate]
9年前关闭。
我正在写一个TicTacToe程序,由于某种原因,它跳过了玩家1的scanner.nextLine(),但在玩家2时停止了。代码如下:有问题的方法名为“Names”
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);
}
}
1条答案
按热度按时间yruzcnhs1#
不妨尝试改变:
致:
另一个setName方法也是如此。