Java scanner.nextLine()正在跳过

时间:2013-11-21 00:18:59

标签: java methods next

我正在编写一个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);
}
}

1 个答案:

答案 0 :(得分:0)

也许尝试改变:

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

为:

public static void Setp1Name(){
    p1name = qwe.nextLine();
}

和其他setName方法相同。