2017-10-15 101 views
-4

我在Java的新的和更熟悉C++的Java否則,如果和掃描儀

import java.util.Scanner; 
class Test{ 
public static void main(String args[]){ 
Scanner sc=new Scanner(System.in); 
System.out.println("Pick option:\n 1-Option one\n 2-Option two"); 
int x= sc.nextInt(); 
if (x=1); 
{ 
System.out.println("You select option one"); 
} 
else if(x=2);{ 
     System.out.println("You select option two"); 
} 
else{ 
System.out.println("Please select option one or two by typing 1 or 2");}     

} 
} 

應該是什麼樣子:

pick選項:

1- Option one 
2- Option two 

//用戶類型1 :

You select option one. 

//用戶類型2:

You select option two. 

//用戶類型別的

Please select option one or two by typing 1 or 2. 

我怎樣才能做到這一點與Java代碼。

+1

什麼是你的問題? – Sweeper

+2

'='是一項任務。 – Ivar

+0

[if'語句結束時的分號]的可能重複(https://stackoverflow.com/questions/14112515/semicolon-at-end-of-if-statement) – Tom

回答

0

您應該使用==到copmare不使用=;if statment不正確。

你可以看到像這樣的編輯後的代碼:

import java.util.Scanner; 
    class Test{ 
    public static void main(String args[]){ 
    Scanner sc=new Scanner(System.in); 
    System.out.println("Pick option:\n 1-Option one\n 2-Option two :"); 
    int x= sc.nextInt(); 
    if (x==1) 
    { 
    System.out.println("You select option one"); 
    } 
    else if(x==2){ 
      System.out.println("You select option two"); 
    } 
    else{ 
    System.out.println("Please select option one or two by typing 1 or 2"); 
}     

    } 
    }