从扫描仪读入值,直到我点击进入

时间:2017-02-10 15:58:49

标签: java

function showModal(){

          var pk = $(this).find('img').attr('id');
          $.ajax({
            url : '/photo/' + pk + '/',
            async: false,
            success: function(data){
              data= JSON.stringify(data)
              var source_slug = data[0]["source_slug"];
              var source_name = data[0]["source_name"];
              var description = data[0]["description"];
              var subject_slug = data[0]["subject_slug"];
              var subject_name = data[0]["subject_name"];
              }
          });
var src = $(this).find('img').attr('src');
          var largeImg = $(this).find('img').attr('data-bsp-large-src');
          if(typeof largeImg === 'string'){
                src = largeImg;
          }
          var index = $(this).attr('data-bsp-li-index');
          var ulIndex = $(this).parent('ul').attr('data-bsp-ul-index');
          var ulId = $(this).parent('ul').attr('data-bsp-ul-id');
          var theImg = $(this).find('img');
          var pText = $(this).find('.text').html();        
          var modalText = typeof pText !== 'undefined' ? pText : 'undefined';
          var alt =  typeof theImg.attr('alt') == 'string' ? theImg.attr('alt') : null;

          clicked.img = src;
          clicked.prevImg = parseInt(index) - parseInt(1);
          clicked.nextImg = parseInt(index) + parseInt(1);
          clicked.ulIndex = ulIndex;
          clicked.ulId = ulId;


          $('#bsPhotoGalleryModal').modal();

          var html = '';
          var img = '<img src="' + clicked.img + '" class="img-responsive"/>';

          html += img;
          html += '<span class="' + settings.iconClose + ' bsp-close"></span>';
          html += '<div class="bsp-text-container">';
          html += '<li><span> 출처 : <a href="/tag/' + source_slug + '">' + source_name + '</a>'
          html += '</span></li>'
          html += '<li><span> 과목 및 단원 :<a href="/tag/' + subject_slug + '">' + subject_name + '</a>'
          html += '<li><span>' + description + '</span></li>'
          html += '</div>';
          html += '<a class="bsp-controls next" data-bsp-id="'+clicked.ulId+'" href="'+ (clicked.nextImg) + '"><span class="' + settings.iconRight + '"></span></a>';
          html += '<a class="bsp-controls previous" data-bsp-id="'+clicked.ulId+'" href="' + (clicked.prevImg) + '"><span class="' + settings.iconLeft + '"></span></a>';

          $('#bsPhotoGalleryModal .modal-body').html(html);
          $('.bsp-close').on('click', closeModal);
          showHideControls();
      }

我有一个名为“sc”的扫描仪,它接收输入。我希望能够键入“1 2 3 4 5”,然后输入“Enter”。这将让我的代码突破while循环并继续下面的其他代码。但是,当我输入“1 2 3 4 5”和“Enter”时,我的代码不会突破循环。它只会在我输入类似“1 2 3 4 5 a”的内容时爆发。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

由于hasNextInt()nextInt()隐藏了您的分隔符(这是Scanner应该做的),您的代码无法区分最终用户按输入与数字之间的空格。

但是,您可以阅读nextLine(),并将其一次性转换为int的数组:

var line = sc.nextLine();
int[] innerArr = Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray();