打印代码时,它会跳过第一人称输入并转到第二输入 [英] when code prints it skips the first persons input and goes to second input

查看:54
本文介绍了打印代码时,它会跳过第一人称输入并转到第二输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;
public class Lab3
{
    public static void main (String[]args)
    {
     Scanner in = new Scanner(System.in);
             System.out.println("enter a value for x:");
             int x = in.nextInt();
             System.out.println("enter a value for y:");
             int y = in.nextInt();
             if (x <= y )
             {
                 System.out.println("the smallest value was:"+ x);
             }
             else
             {
                 System.out.println("the smallest value was:"+ y);
             }




             String choice12;
             String choice234;
              System.out.println("Player 1: Choose rock, scissors, or paper:");
            String  choice1 = in.nextLine().toLowerCase();
            System.out.println(choice1);

              System.out.println("Player 2: Choose rock, scissors, or paper:");
              String choice2 = in.nextLine().toLowerCase();
              System.out.println(choice2);
               if (choice1.equals("rock") && choice2.equals("rock"))
        {
           System.out.println("It is a tie.");

        }
        if (choice1.equals("rock") && choice2.equals("scissors"));
        {
           System.out.println("Player 1 wins.");

        }
           if (choice1.equals("rock") && choice2.equals("paper"));
        {
           System.out.println("Player 2 wins.");
        }
           if (choice1.equals("paper") && choice2.equals("paper"));
        {
           System.out.println("it is a tie.");
        }
           if (choice1.equals("paper") && choice2.equals("rock"));
        {
           System.out.println("Player 1 wins.");
        }
           if (choice1.equals("paper") && choice2.equals("scissors"));
        {
           System.out.println("Player 2 wins.");
        }
           if (choice1.equals("scissors") && choice2.equals("paper"));
        {
           System.out.println("Player 1 wins.");
        }
           if (choice1.equals("scissors") && choice2.equals("scissors"));
        {
           System.out.println("it is a tie.");
        }
           if (choice1.equals("scissors") && choice2.equals("rock"));
        {
           System.out.println("Player 2 wins.");
        }


        }
    }

推荐答案

这是一个经典问题:

输入整数时,请输入两个内容
1.整数部分
2.换一行.


和nextInt函数仅读取整数部分,而忽略换行部分.因此,在完成读取之后,当您调用nextLine方法时,它将读取新行并完成读取过程.因此,为避免在读取choice1之前出现此问题,请一次调用nextLine方法,您无需将行保存在任何变量中.您的问题将得到解决
This is a classic problem:

When you enter Integer number you enter two things
1. the integer part
2. a new line.


and nextInt function only read the integer part and ignore the newline part. So, after finishing reading when you call the method nextLine, it reads the new line and complete the reading process. So to avoid this problem before reading choice1, call the nextLine method once, you don''t need to save the line in any variable. Your problem will be solved


这篇关于打印代码时,它会跳过第一人称输入并转到第二输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆