如何通过扫描仪在没有Split()的情况下从同一行读取多个字符串;

时间:2015-11-08 00:57:15

标签: java string char indexof

.split() Method is NOT Allowed

我从一个乐于助人的人那里得到了一些帮助!我只是想知道是否有人可以帮我修改一下这个,

代码用于分配,它基于扫描仪的输入。它有另外两个类,但这是感兴趣的类。

此代码正在运行,但是必须输入的内容是** U5, D10**等等。这样可行。但是我需要代码能够从一行中读取多个String,同时将它们分开,就像它们现在一样。比如说**D10 U5 L4**,例如,只有一个玩家中的两个玩家。目前的代码并没有将其识别为一行,而是将第二种类型的东西分配给第二个玩家。

任何提示?

由于

import java.util.Scanner;

class Asgn2
{
  public static void main(String[] args)
  {
  Scanner scan = new Scanner (System.in);

Player me = new Player("Player1");
Player opponent = new Player("player2");

int startingLoop = 0;
String strA;
int turn =1;

System.out.print("How many turns will the game have: ");
int turnsInGame = scan.nextInt();

System.out.print("How many moves does each player have each turn: ");
int numberOfTurns = scan.nextInt();



for(int i = turnsInGame;  startingLoop < i; startingLoop++)
{
        System.out.print("Turn " + turn++ + "\n");

        System.out.print("Player 1 what are your " + numberOfTurns + "   move(s): ");
        String userInput = scan.next();

        System.out.print("Player 2 what are your " + numberOfTurns + " move(s): ");
        String userInputOne = scan.next();


        for (int j = 0; j < userInput.length() - 1; j++) 
        {
            char letter = userInput.charAt(j);
            String num = "";


            for(int k= j + 1; k < userInput.length(); k++)
            {
                j++;
                if(userInput.charAt(k)!=' ')
                {
                    num+=userInput.charAt(k);
                }
                else
                {
                    break;
                }
            }
            int integer = Integer.parseInt(num + "");
            strA = Character.toString(letter);

            switch(strA)   //For player oneChooses which value to add or subtract from based on what is input.
            {

            case "U":

                me.move(moveSteps.UP , integer);
                break;

            case "D":

                me.move(moveSteps.DOWN, integer);
                break;

            case "L":

                me.move(moveSteps.LEFT, integer);
                break;  

            case "R":

                me.move(moveSteps.RIGHT, integer);
                break;
            }

          //Player 2  
            for (int playerTwo = 0; playerTwo < userInputOne.length() - 1; playerTwo++) 
            {
                char letterTwo = userInputOne.charAt(0);
                String numTwo = "";
                String strB = Character.toString(letterTwo);
                for(int m= playerTwo + 1; m<userInput.length(); m++)
                {
                    playerTwo++;
                    if(userInputOne.charAt(playerTwo)!=' ')
                    {
                        numTwo+=userInputOne.charAt(playerTwo);
                    }
                    else
                    {
                        break;
                    }
                }
                    int stepsMoved = Integer.parseInt(numTwo + "");




                    switch(strB)   //For player two
                    {

                    case "U":

                        opponent.move(moveSteps.UP , stepsMoved);
                        break;

                    case "D":

                        opponent.move(moveSteps.DOWN, stepsMoved);
                        break;

                    case "L":

                        opponent.move(moveSteps.LEFT, stepsMoved);
                        break;  

                    case "R":

                        opponent.move(moveSteps.RIGHT, stepsMoved);
                        break;
              }


    }
}





 System.out.print(me);

 System.out.print(opponent);






 }
}
 }

3 个答案:

答案 0 :(得分:0)

将输入分配给String后,使用.split()方法将字符串拆分为数组。要使用.split(),请输入要将其拆分的字符。在这种情况下一个空间。例如,将此用于当前项目:.split(" ")。拆分后,您可以像任何阵列一样访问它。

更新
首先使用.nextLine()并将其分配给临时字符串变量。然后 你可以创建另一个扫描仪并输入一个字符串。例如:

Scanner sc = new Scanner(YOUR TEMPORARY VARIABLE);

您现在可以使用.next()获取单个字符串。

答案 1 :(得分:0)

这是Asgn2类

import java.util.Scanner;

public class Asgn2 {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("What is your name player 1: ");
    String p1name = scan.nextLine();
    System.out.print("What is your name player 2: ");
    String p2name = scan.nextLine();
    Player p1 = new Player(p1name);
    Player p2 = new Player(p2name);

    System.out.print("How many turns will the game have: ");
    int numTurns = scan.nextInt();
    System.out.print("How many moves does each player have each turn: ");
    int numMoves = scan.nextInt();

    for (int turn = 1; turn <= numTurns; turn++) {
        System.out.println("----------------");
        System.out.println("Turn number " + turn);
        System.out.println("----------------");

        for (int player = 1; player <= 2; player++) {
            System.out.print("Player " + player + " what are your " + numMoves + " move(s): ");
            for(int move=1;move<=numMoves;move++){
                String currMove = scan.next();//splits at space;
                char dir = currMove.charAt(0);//gets dir
                String temp="";
                for(int index=1;index<currMove.length();index++){
                    temp+=currMove.charAt(index);
                }
                int dist = Integer.parseInt(temp);
                if(player==1){
                    p1.move(dir, dist);
                }else if(player==2){
                    p2.move(dir, dist);
                }
            }

            System.out.println("Player 1 is at " + p1.getPos() + " and Player 2 is at " + p2.getPos());
            System.out.println();
        }
    }
}
}

这是Player类

public class Player {
    private String name;

    private int locX = 0;
    private int locY = 0;

    public Player(String name) {
        this.name = name;
    }

    public void move(char dir, int numSteps) {
        switch (dir) {
        case 'U':
            locY += numSteps;
            break;
        case 'D':
            locY -= numSteps;
            break;
        case 'L':
            locX -= numSteps;
            break;
        case 'R':
            locX += numSteps;
            break;
        }
    }

    public String getPos() {
        return "(" + locX + ", " + locY + ")";
    }

    public String getName() {
        return name;
    }
}

在任何人去之前说发布代码块对OP没有帮助,我和他在聊天室里解释这个东西,所以不要讨厌:)

答案 2 :(得分:-1)

调用方法.nextLine()而不是.next()。我认为这应该可以解决你的问题。