【问题标题】:Java scanner.nextLine() is skipping [duplicate]Javascanner.nextLine() 正在跳过[重复]
【发布时间】:2013-12-05 06:33:03
【问题描述】:

我正在编写一个井字游戏程序,由于某种原因,它跳过了玩家 1 的扫描仪.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);
}
}

【问题讨论】:

  • out.println("Nice to meet you " + Getp1Name()); 打印什么?
  • 此刻,它打印出“Nice to meet you”
  • @RyanHosford:我假设您在此之前还有其他输入。也许是.next().nextInt()?
  • 你以前用过扫描仪吗?
  • @RyanHosford 你能显示整个课程代码吗?

标签: java methods next


【解决方案1】:

也许尝试改变:

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

到:

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

与其他 setName 方法相同。

【讨论】:

    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 2015-03-24
    • 2013-01-07
    相关资源
    最近更新 更多