2015-04-03 56 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