而带有hasNextInt()的循环实际读取?

时间:2016-02-25 17:15:15

标签: java loops while-loop

你好我的问题在于这个程序。

int integer = 0;
int evenInts = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
integer = in.nextInt();
while(in.hasNextInt()){
     System.out.print("Enter an integer: ");
     integer = in.nextInt(); }

在询问“输入整数:”之后应该读入一个int。之后,只要将整数放入输入中,就会重复此过程。 实际上它打印的内容如下:

Enter an integer: 10
10 // so basically the 
  

“输入一个整数:”

消息此时无法打印

  

输入一个整数:12

如果我在while循环之前遗漏integer = in.nextInt();代码部分,一切正常。为什么程序的行为如此?是因为缓冲还是逻辑错误。

谢谢!

2 个答案:

答案 0 :(得分:1)

这是因为你已经用完输入流中唯一的整数而<?PHP if ($_SERVER['REQUEST_METHOD']=='POST') { $Name = $_POST['Name']; $Surname = $_POST['Surname']; $Street = $_POST['Street']; $Hometown = $_POST['Hometown']; if ($Name == '' || $Surname == '' || $Street== '' || $Hometown == '') { echo 'please fill all values'; } else { require_once('dbConnect.php'); $sql = "INSERT INTO T_Student(Name,Surname,Street,Hometown) VALUES('$Name','$Surname','$Street','$Hometown')"; $sql .= "INSERT INTO T_University(ID, Teacher, Subject , Classroom, F_ID_Student) VALUES ("","","","","",LAST_INSERT_ID())"; if(mysqli_multi_query($con,$sql)){ echo 'successfully registered'; } else { echo 'oops! Please try again!'; } } mysqli_close($con); } echo "Data Inserted"; ?> 返回false,因为输入流中没有下一个int。

hasNextInt()

你可以使用do while循环

System.out.print("Enter an integer: ");
 integer = in.nextInt();// here you have read the int there is no more next int

答案 1 :(得分:0)

将它放入do-while循环中:

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.*;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.text.*;
import flash.display.Stage;

var objImage1:logo1 = new logo1;
var objImage2:pic2 = new pic2;
var objImage3:pic3 = new pic3;
var objImage4:logo2 = new logo2;
var objImage5:pic5  = new pic5;
var objImage6:pic6 = new pic6;
var objImage7:pic7 = new pic7;
var objImage8:logo3 = new logo3;
var objImage9:pic9 = new pic9;
var objImage10:pic10 = new pic10;
var objImage11:logo4 = new logo4;
var objImage12:pic12 = new pic12;
var objImage13:pic13 = new pic13;
var objImage14:pic14 = new pic14;
var objImage15:pic15 = new pic15;
var objImage16:logo5 = new logo5;
var objImage17:pic17 = new pic17;
var objImage18:logo6 = new logo6;
var objImage19:pic19 = new pic19;
var objImage20:pic20 = new pic20;
var objImage21:logo7 = new logo7;
var objImage22:pic22 = new pic22;
var objImage23:pic23 = new pic23;
var objImage24:pic24 = new pic24;
var sourceDeck:Array =[objImage1,objImage2,objImage3,objImage4,objImage5,objImage6,objImage7,objImage8,objImage9,objImage10,objImage11,objImage12,objImage13,objImage14,objImage15,objImage16,objImage17,objImage18,objImage19,objImage20,objImage21,objImage22,objImage23,objImage24];
var shuffledDeck:Array = new Array ();
var xValues:Array =["121","321","521","731","937","1136","120","320","520","732","938","1138","120","321","520","730","938","1135","121","322","520","732","938","1135"];
var yValues:Array =["173","173","173","173","173","173","346","346","346","346","346","346","534","534","534","534","534","534","716","716","716","716","716","716"];

function placeCards():void
{
for (var i:int = 0; i<sourceDeck.length; ++i)
{
    addChild(sourceDeck[i]);
    sourceDeck[i].x = int(xValues[i])+20;
    sourceDeck[i].y = int(yValues[i]);  
}
}
placeCards();
function removeCards():void
{
for (var i:int=0;  i<sourceDeck.length; ++i)

{
    removeChild(sourceDeck[i]);

}
}
    function shuffleCards():void
{
while(sourceDeck.length >0)
{
    var r:int=Math.floor(Math.random()*sourceDeck.length);
    shuffledDeck.push(sourceDeck[r]);
    sourceDeck.splice(r,1);
}
}
function placeShuffledCards():void
{
for (var i:int=0; i<shuffledDeck.length; ++i)
{
    addChild(shuffledDeck[i]);
    shuffledDeck[i].x = int(xValues[i]);
    shuffledDeck[i].y = int(yValues[i]);

}
}


function showShuffledCards():void

{   
removeCards();
shuffleCards();
placeShuffledCards();

}
showShuffledCards();
foo();

function foo():void {
    var x:int = 2; // you can use variables as you would normally

    // do something here

    var timer:Timer = new Timer(5000);
    var afterWaiting:Function = function(event:TimerEvent):void {
   timer.removeEventListener(TimerEvent.TIMER, afterWaiting);
   timer = null;
   // 5 seconds have passed, now do something more


   objImage1.gotoAndPlay(10);
   objImage2.gotoAndPlay(10);
   objImage3.gotoAndPlay(10);
   objImage4.gotoAndPlay(10);
   objImage5.gotoAndPlay(10);
   objImage6.gotoAndPlay(10);
   objImage7.gotoAndPlay(10);
   objImage8.gotoAndPlay(10);
   objImage9.gotoAndPlay(10);
   objImage10.gotoAndPlay(10);
   objImage11.gotoAndPlay(10);
   objImage12.gotoAndPlay(10);
   objImage13.gotoAndPlay(10);
   objImage14.gotoAndPlay(10);
   objImage15.gotoAndPlay(10);
   objImage16.gotoAndPlay(10);
   objImage17.gotoAndPlay(10);
   objImage18.gotoAndPlay(10);
   objImage19.gotoAndPlay(10);
   objImage20.gotoAndPlay(10);
   objImage21.gotoAndPlay(10);
   objImage22.gotoAndPlay(10);
   objImage23.gotoAndPlay(10);
   objImage24.gotoAndPlay(10);
   // the scope is retained and you can still refer to the variables you
   // used earlier
   x += 2;
 }
 timer.addEventListener(TimerEvent.TIMER, afterWaiting);
    timer.start();

}

请注意,值必须用空格分隔才能继续循环。