Java跳过输入,直接转到if语句,

Java跳过输入,直接转到if语句,,java,input,Java,Input,我正在制作一个石头、布、剪刀游戏来练习我的Java技能。但是现在,我面临着一个我无法解决的问题。当我要求用户输入某个内容时,他们无法输入该内容,它会直接转到后面的if语句 代码: 包装rps_游戏 // Rock, Paper, Scissors game import java.util.Scanner; import java.util.Random; import java.util.*; public class rock_paper_scissors { public s

我正在制作一个石头、布、剪刀游戏来练习我的Java技能。但是现在,我面临着一个我无法解决的问题。当我要求用户输入某个内容时,他们无法输入该内容,它会直接转到后面的if语句

代码: 包装rps_游戏

// Rock, Paper, Scissors game


import java.util.Scanner;
import java.util.Random;
import java.util.*;

public class rock_paper_scissors {
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        Random rand = new Random();

        String choices[] = {"rock", "paper", "scissors"};

        int winRounds = 0;
        boolean running = true;

        int rounds = 0;
        int wins = 0;
        int loses = 0;

        String yourChoice="";
        String compChoice="";
        while (running = true){ // main loop

            try{
                System.out.println("Enter the amount of rounds you want to play: ");
                rounds = input.nextInt(); // gets input
                winRounds = (rounds/2)+1;
                System.out.println("You are playing best of " + winRounds + " out of " + rounds);

                running = false; // breaks off from loop

            }catch(Exception e){ // if error pops up
                System.out.println("You DID NOT enter a WHOLE NUMBER.");
                // still runs b/c criteria has not been met.
            }

            while (wins < winRounds && loses < winRounds){
                // problem arises here
                System.out.println("Enter either Rock, Paper or Scissors: ");
                yourChoice = input.nextLine();
                input.nextLine();
                yourChoice.toLowerCase();



                if (yourChoice.equals(Arrays.asList(choices).contains(yourChoice))){ // if array contains what use entered
                    break;
                }else{
                    System.out.println("You did not enter either Rock, Paper or Scissors.");
                    running = false; // exit program
                }

                compChoice = choices[rand.nextInt(choices.length)];
                System.out.println(compChoice);

            }

        }

    }
}
//石头、布、剪刀游戏
导入java.util.Scanner;
导入java.util.Random;
导入java.util.*;
公共级摇滚剪子{
公共静态void main(字符串参数[]){
扫描仪输入=新扫描仪(System.in);
Random rand=新的Random();
字符串选择[]={“石头”、“布”、“剪刀”};
int=0;
布尔运行=真;
整数=0;
int=0;
int=0;
字符串yourChoice=“”;
字符串compChoice=“”;
while(running=true){//主循环
试一试{
System.out.println(“输入您想要玩的回合数:”;
rounds=input.nextInt();//获取输入
winRounds=(轮数/2)+1;
System.out.println(“你在“+回合数+中的“+回合数”中打得最好);
running=false;//从循环中断
}捕获(异常e){//如果出现错误
System.out.println(“您没有输入整数。”);
//仍然运行未满足b/c标准。
}
while(赢

我还没有完成,但发生了什么事?

您必须阅读第行
String temp=input.nextLine()
,然后在本地创建另一个
扫描仪
,以读取
int
。这样,用户按下的enter(\n)将被读取为一行,否则按下的enter将被解释为
input.nextLine()中的另一行


同样,虽然循环是无限循环,但您可能也希望纠正它。

此代码纠正了您的错误,尽管您仍然需要做很多事情

问题首先是输入扫描仪。(您需要两个输入扫描仪)

其次,根据您的选择.toLowerCase(),它必须是您的选择=您的选择.toLowerCase(); 因为字符串是不可变的

最后是yourChoice.equals(Arrays.asList(choices).contains(yourChoice))。实际上,它只能是(Arrays.asList(choices).contains(yourChoice)

Try-Catch位于错误的位置,因此您的整个代码都在运行,请参阅以下没有问题的代码

import java.util.Scanner;
import java.util.Random;
import java.util.*;

public class sda {
    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        Scanner lineInput = new Scanner(System.in);
        Random rand = new Random();

        String choices[] = { "rock", "paper", "scissors" };

        int winRounds = 0;
        boolean running = true;

        int rounds = 0;
        int wins = 0;
        int loses = 0;

        String yourChoice = "";
        String compChoice = "";
        while (running = true) { // main loop

            try {
                System.out
                        .println("Enter the amount of rounds you want to play: ");
                rounds = input.nextInt(); // gets input
                winRounds = (rounds / 2) + 1;
                System.out.println("You are playing best of " + winRounds
                        + " out of " + rounds);

                running = false; // breaks off from loop

                while (wins < winRounds && loses < winRounds) {
                    System.out
                            .println("Enter either Rock, Paper or Scissors: ");
                    yourChoice = lineInput.nextLine();
                    yourChoice= yourChoice.toLowerCase();
                    if (Arrays.asList(choices).contains(
                            yourChoice)) { // what use entered
                        break;
                    } else {
                        System.out
                                .println("You did not enter either Rock, Paper or Scissors.");
                        running = false; // exit program
                    }

                    compChoice = choices[rand.nextInt(choices.length)];
                    System.out.println(compChoice);

                }
            } catch (Exception e) { // if error pops up
                System.out.println("You DID NOT enter a WHOLE NUMBER.");
                break;
            }

        }

        input.close();
        lineInput.close();
    }
}
import java.util.Scanner;
导入java.util.Random;
导入java.util.*;
公共类sda{
公共静态void main(字符串参数[]){
扫描仪输入=新扫描仪(System.in);
扫描仪lineInput=新扫描仪(System.in);
Random rand=新的Random();
字符串选择[]={“石头”、“布”、“剪刀”};
int=0;
布尔运行=真;
整数=0;
int=0;
int=0;
字符串yourChoice=“”;
字符串compChoice=“”;
while(running=true){//主循环
试一试{
系统输出
.println(“输入要玩的回合数:”);
rounds=input.nextInt();//获取输入
winRounds=(轮数/2)+1;
System.out.println(“你玩得最好”+winRounds
+“超出”+轮数);
running=false;//从循环中断
while(赢
while(running=true)
这将始终导致一个无限循环,除非您显式地
中断;
。代码中存在很多很多问题。例如:
yourChoice.toLowerCase();
为了澄清@fge所说的内容,
string.toLowerCase()
以小写形式返回相同的字符串,但不修改字符串本身。您要做的是
yourChoice=yourChoice.toLowerCase()