输入单个数字作为输入。输出将转换为单词,如果输入为0,程序将退出

时间:2015-06-15 11:19:35

标签: java java.util.scanner

实施例:              输入一位数字:2              你已输入第二名              输入一位数字:4              你输入了第四名              输入一位数字:0              您已输入数字零              该程序将退出/退出..

3 个答案:

答案 0 :(得分:1)

查看switch-case语句here

类似的东西:

switch(input){

  case 0: return;
  case 1: //do each case separately.
  case 2: // or
  default://do something here if you want to parse any number to text.
}

更新:有一个答案可以将数字解析为单词here

答案 1 :(得分:0)

import java.util.Scanner;
public class GetInputFromUser{
    public static void main(String args[]){
      Scanner in = new Scanner(System.in);
        System.out.print("Enter a number:");
          int s = in.nextInt();
  System.out.println("You entered number "+s);
}

}

答案 2 :(得分:-1)

您可以使用波纹管代码作为您想要的输入输出:

    Scanner conin=new Scanner(System.in);
    System.out.println("Enter single digit number:");
      String[] array = {"exit/quit","one", "two", "three","four","five","six","seven","eight","nine"};
    int a=conin.nextInt();
    if(a<10){
     while (a!=0) {            
      System.out.println("You have entered number:"+array[a]);
      a=conin.nextInt();

    }
   System.out.println("Program "+array[a]);
   System.exit(0);   
    }else{
        System.out.println("not allow"); 
        System.exit(0);
    }