zl程序教程

您现在的位置是:首页 >  其它

当前栏目

第三章习题3-第三题=改写彩票例题

习题 第三 第三章 例题 改写
2023-09-27 14:27:29 时间

问题:

用if-else if-else 多条件分支语句代替switch语句来判断彩票的中奖情况,允许彩票号码中含有0.


代码:

Test .java

import java.util.Scanner;  
public class Test {  
    public static void main(String[]args){  
        Scanner reader=new Scanner(System.in);  
        System.out.println("Please enter your caipiao number: ");  
        int number=reader.nextInt();  
        Administractor person=new Administractor();  
        person.getmess(number);  
          
          
    }  
}  
Administractor.java

public class Administractor {  
    void getmess(int number){  
        if(number>100000||number<10000){  
            System.out.print("please enter your number: ");  
        }  
        else  
        {  
        int one=number%10;  
        int two=number%100;  
        int three=number%1000;  
       if(one==9||one==3||one==1){  
            System.out.println("Good luck! you won the third prize! ");  
       }else if(two==29||two==46||two==21){  
             System.out.println("Good luck! you won the second prize! ");
       }else if(three==875||three==326||three==596){  
           System.out.println("Good luck! you won the first prize! ");      
        }   else{
            System.out.println("Sorry,very pity,it is not the second prize! ");        
        }     
        }     
    }  
      
}  


运行结果:






总结:

要理清循环之间的关系。