2016-04-30 33 views
3
import java.util.Scanner; 
public class whileLoopOlympics 
{ 
public static void main (String[] args) 
{ 
    System.out.println ("Which year was the most recent London Olympics?"); 
    Scanner scanner = new Scanner(System.in);   
    String yearAsString = scanner.nextLine(); 
    int year = Integer.parseInt(yearAsString); 


    while (year!=2012) 
    { 
     System.out.println ("Which year was the most recent London Olympics?"); 
     Scanner next = new Scanner(System.in); 
     String yearAsString2 = scanner.nextLine(); 
     int year1 = Integer.parseInt(yearAsString2); 

} 
} 
} 

我想循環,直到用戶輸入2012,但是我無法弄清楚如何做一些事情,如一段時間(一年!= 2012 & & YEAR1!= 2012)我只是不知道如何當「year」不在while循環中時執行此操作如何循環直到用戶進入2012年爲止?

+1

不要用'year'和'year1'做兩次。只需實現你的循環,將輸入讀入'year'。在循環之前,將'year'初始化爲'null'。然後只是在'year!= 2012'循環。 – RJM

+0

你可以將while(...)'loop循環切換到'do {...} while(...)' – ochi

+0

使用break語句! – Devavrata

回答

4

您不需要創建新的掃描器和新變量,可以重複使用。

import java.util.Scanner; 
public class whileLoopOlympics 
{ 
public static void main (String[] args) 
{ 
    Scanner scanner = new Scanner(System.in);  
    int year = 0; 


    while (year!=2012) 
    { 
     System.out.println ("Which year was the most recent London Olympics?"); 
     String yearAsString = scanner.nextLine(); 
     year = Integer.parseInt(yearAsString); 
    } 
} 
} 
+0

如果用戶輸入的內容不是int,只是說 – ochi

+2

@ochi - OP的發佈代碼有同樣的問題,你會得到NumberFormatException。這個答案並沒有引入作爲一個錯誤,它只是解決了被問到的問題。 – RJM

-1

我將其更改爲do/while

public static void main (String[] args) 
    { 
     int year = -1; 

     do{ 
      System.out.println ("Which year was the most recent London Olympics?"); 
      Scanner scanner = new Scanner(System.in); 
      String yearAsString = scanner.nextLine(); 

      try{ 
       year = Integer.parseInt(yearAsString); 

      }catch (NumberFormatException nfe){ 
       System.out.println ("Invalid year; numbers only please"); 
      } 
     }while (year != 2012); 
     System.out.println ("Correct! Thanks"); 
    } 
+0

也許你可以評論爲什麼downvote? – ochi

+0

我沒有投過票,但是當我們拿到2012年的時候,這個循環也會執行 – Devavrata

+1

1.你沒有明確地說出問題所在;相反,你只是在代碼中修復它; 2.你提出了一種完全不必要的不​​同方法。 – mre

0
import java.util.Scanner; 
public class whileLoopOlympics 
{ 
public static void main (String[] args) { 
    int year = 0; 
    Scanner next = new Scanner(System.in); 
    while (year!=2012) { 
     System.out.println ("Which year was the most recent London Olympics?"); 
     String yearAsString = scanner.nextLine(); 
     year = Integer.parseInt(yearAsString2); 

    } 
} 

試試這個

+0

你會得到一個NumberFormatException如果用戶輸入的內容不是int,只是說 – ochi

0

總結一些想法,我用我自己的意見看到:

您應儘量不要經常重複你的代碼。你不必使用這麼多的變量和掃描儀(你甚至可以聲明你是從不使用)。

// Do not declare a new scanner for any line you read 
Scanner scan = new Scanner(System.in); 
// If you're as maniac as I am, you can use only ONE string as well (or even no String...) 
String user_input; 
int year = 0; 

while (year != 2012) 
{ 
    System.out.println ("Which year was the most recent London Olympics?"); 
    user_input = scan.nextLine(); 
    year = Integer.parseInt(yearAsString2); 
}