2016-09-03 54 views

回答

1

你可以這樣做如下:

while(true) { 
    System.out.println("Please enter I or C or E for Exit"); 
    String input = sc.nextLine(); 
    if("I".equals(input)) { 
     //perform operation 
     break; 
     } 
    else if("C".equals(input)) { 
     //perform operation 
     break; 
    } 
    else if("E".equals(input)){ 
     break; 
    } 
    System.out.println("Please Enter the correct option");   
} 
0

我跳我的解決方案幫助你解決你的問題,你的解決方案如下。

包鐵;

import java.util.Scanner; 

public class Selection { 

public static void main(String[] args) { 
    System.out.println("Please enter i or c ."); 
    while (true){ 
    Scanner scan=new Scanner(System.in); 
    String result=scan.nextLine(); 
    if (!(result.equalsIgnoreCase("c")||result.equalsIgnoreCase("i"))){ 
     System.out.println("Please enter i or c"); 
    } 
    else{ 
    System.out.println("you have entered i or c"); 
    break; 
    } 
    } 
} 

}

0

簡單,我們可以用如下switch語句做到這一點:

Scanner s=new Scanner(System.in); 
Char a=s.next(); 
Switch(a) 
{ 
case 'I': 
//operation 
break; 
case 'C': 
//operation 
break; 
default: 
System.out.println("Please choose either I or C"); 
} 
相關問題