2015-04-03 125 views
1

我的这段时间有没有问题,或偶尔发生循环,或者我错过了什么?经过精细的作品,但在第二首先来看,我得到这样的:为什么我在使用(scanner).nextLine()时一次循环打印2个提示?

Enter course NAME: class name 
Enter course HOURS: 4 
Enter course GRADE: 4.0 
You have entered class: jeff graves, class hours: 4 and, class grade 4.0 
Do you want to continue: 
y 
Enter course NAME: Enter course HOURS: 

只是正常使用(scanner).next();但当时我只能采取从用户的一个词,当它滑过它将从nextInt抛出一个错误。

public class GetGrades { //open class GetGrades 

    public static void main(String[] args) { 
/**creating new instance of scanner named input to read user input*/ 
     Scanner input = new Scanner(System.in); // create new instance of scanner object 
     boolean repeat = true; //boolean value for while loop holding array input 
     String s; // create string to store user input value for exiting loops 
/** create 3 arrays to store course name, hours, and grades*/ 
     String[] name = new String[20]; //type string array for class name 
     int[] hours = new int[20]; // type int array for class hours 
     float[] grade = new float[20]; //type float array for class grade 


     outerloop: // set break point for nested for loops exit on user input 
      while(repeat != false) {// while loop with boolean value to let user exit array input 
     for (int i=0; i<name.length; i++) { //for loop for name array 

      System.out.print("Enter course NAME: "); //prompt user for input 
      name[i] = input.nextLine(); //read next line value and store in array name 
      System.out.print("Enter course HOURS: "); //prompt user for input 
      hours[i] = input.nextInt(); //read the next int and store in array hours 
      System.out.print("Enter course GRADE: "); //prompt user for input 
      grade[i] = input.nextFloat(); //read the next float value and store in array grade 

     /**Print line to console summing um what the user has entered*/ 
      System.out.println("You have entered class: " + name[i] + ", class hours: " + hours[i] + 
        " and, class grade " + grade[i]); 

/**prompt user if wanted to enter more grades, break loop on n or N*/ 
      System.out.println("Do you want to continue:"); 
      s = input.next(); 
      if (s.equals("y") || s.equals("Y")) { //open if statement 
       repeat = true; 
      } else { //close if and open else 
       break outerloop; 
      } //close else statement 


      }//close for loop with i as count 
     }//close while 

回答

2

input.next()将读取下一个单词。 input.nextLine()将会读取,直到下一次按回车。

这意味着当您写入“y”并按回车键时,您既输入了单词“y”,又输入了下一个输入,同时填写了两个提示,并使下一个提示成为书面。

你可以简单地取代你next()nextLine()当你问到继续:

 System.out.println("Do you want to continue:"); 
     s = input.next(); 

成为

 System.out.println("Do you want to continue:"); 
     s = input.nextLine(); 

从而读取两个 “Y” 和输入。下一个提示现在可以自由接受新的输入。

+0

**改为input.nextLine()现在它只是吹过一切,而不需要一个“你要继续提示” ......跳过一路走到最后 – 2015-04-03 04:18:20

+0

在'grade [i] = input.nextFloat();之后添加'input.nextLine()'; '出于同样的原因 – 2015-04-03 04:21:47

+0

omg我吓坏了爱你! 4天的折磨现在结束了! – 2015-04-03 08:14:18

0

当您输入等级时,例如12.3并输入“input.nextFloat();”只会采用“12.3”而不是“输入”,因此下一台扫描仪将采用“输入”。

在我看来,

第一,变 “S = input.next()” 到 “S = input.nextLine()”,但它会采取以前的扫描仪的 “输入”,“品位[我] = input.nextFloat();“,所以,第二,把它放到while循环中作为条件。像这样

while((s = input.nextLine()).equals("")) {} 

因此,它不会停止直到获得期望的输入。

试试这个..

System.out.print("Do you want to continue:"); 
while((s = input.nextLine()).equals("")) {} 
if (s.equals("y") || s.equals("Y")) { // open if statement 
    repeat = true; 
} else { // close if and open else 
    break outerloop; 
} // close else statement 
+0

输入4.2,而不是4,2 – 2015-04-03 10:01:55