2013-10-27 104 views
0

問題發生在第七部分。它不會允許我在狀態中讀取,而是打印else語句。跳過代碼

import java.util.Scanner; 

public class ifElseStatements 
{ 
    public static Scanner in = new Scanner (System.in); 

    public static void main (String[] args) 
    { 
     one(); 
     two(); 
     three(); 
     four(); 
     five(); 
     six(); 
     seven(); 
     eight(); 
    } 
    public static void one() 
    { 
    System.out.print("grade: "); 
    int number = in.nextInt(); 
    if(number > 70 && number <=100) 
    { 
     System.out.println("You're passing"); 
    } 
    else 
    { 
     System.out.println("you're not passing!"); 
    } 

    } 

    public static void two() 
    { 
     System.out.print("Number please"); 
     int b = in.nextInt(); 
     if(b <=50) 
     { 
     System.out.println("Go"); 
     } 
     else 
     { 
     System.out.println("STOP"); 
     } 
    } 

    public static void three() 
    { 
     System.out.print("Integer please >"); 
     int c = in.nextInt(); 
     if(c%2 == 0) 
     { 
     System.out.println("Even"); 
     } 
     else 
     { 
     System.out.println("odd"); 
     } 
    } 

    public static void four() 
    { 
     System.out.print("Integer please"); 
     int d = in.nextInt(); 
     if(d%5 == 0) 
     { 
     System.out.println("Multiple of 5"); 
     } 
     else 
     { 
     System.out.println("Not a multiple of 5"); 
     } 
    } 

    public static void five() 
    { 
     System.out.print("number please"); 
     int e = in.nextInt(); 
     if(e< 10) 
     { 
     System.out.println("one digit"); 
     } 
     else if(e>= 10 && e<100) 
     { 
     System.out.println("two digits"); 
     } 
     else 
     { 
     System.out.println("three digits"); 
     } 
    } 

    public static void six() 
    { 
     System.out.print("Jersey Number"); 
     int f = in.nextInt(); 
     if(f == 12 || f == 80 || f == 71) 
     { 
     System.out.println("That number is retired from the seattle seahawks"); 
     } 
     else 
     { 
     System.out.println(""); 
     } 

    } 

    public static void seven() 
    { 
     System.out.print("a state is"); 
     String g = in.nextLine(); 
     if(g .equals ("Washington") || g .equals ("Oregon") || g .equals ("Idaho")) 
     { 
     System.out.println("That state is in the PNW!"); 
     } 
     else 
     { 
     System.out.println("You should move to the PNW; Its great here!"); 
     } 
    } 

    public static void eight() 
    { 
     System.out.print("drink size (SHORT, TALL, GRANDE, VENTI)"); 
     String h = in.nextLine(); 
     if(h .equals ("SHORT")) 
     { 
     System.out.println("8"); 
     } 
     else if(h .equals ("TALL")) 
     { 
     System.out.println("12"); 
     } 
     else if(h .equals ("GRANDE")) 
     { 
     System.out.println("16"); 
     } 
     else if(h .equals ("VENTI")) 
     { 
     System.out.println("20"); 
     } 
    } 


} 

這就是我運行代碼時的樣子。

grade: 70 

you're not passing! 

Number please12 

Go 

Integer please >30 

Even 

Integer please15 

Multiple of 5 

number please33 

two digits 

Jersey Number12 

That number is retired from the seattle seahawks 

a state isYou should move to the PNW; Its great here! 

drink size (SHORT, TALL, GRANDE, VENTI)TALL 

12 oz 
+5

取代

in.nextInt(); 

情況下,如果是在第七部分何必當初你在這裏放置了一百行代碼,而不僅僅是那些相關的8? –

回答

2

上一步少了點in.nextInt(),它讀取下一個整型,但不消耗EOL字符。因此,step7方法讀取下一行,並消耗上一步未消耗的EOL。

您應該在步驟6中添加in.nextLine()以消耗EOL字符,而不僅僅是整數。

+0

+1 OP輸入'12 '不只是'12'和這個''由7()的nextLine()讀取。 –

+0

謝謝!這解決了我的問題。 – user2925747

0

這是由於您同時使用Scanner.nextInt()Scanner.nextLine()而導致的常見問題,該問題並不總是按您所希望的那樣工作。您可以通過通過JavaDoc進行了解掃描儀的工作原理找到這個解釋,但對於一個簡單的解決只是

Integer.parseInt(in.nextLine()); 
+1

'in.nextInt(); in.nextLine();'也行。 –

+0

事實上,雖然我覺得這個解決方案更安全。 – kviiri