| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1314 人关注过本帖
标题:求助,如何结束SWITCH结构下的DO-WHILE循环
只看楼主 加入收藏
jack11430
Rank: 2
等 级:论坛游民
帖 子:16
专家分:17
注 册:2009-10-6
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:7 
求助,如何结束SWITCH结构下的DO-WHILE循环
import java.util.Scanner;

public class liling33{
    public static void main (String[] args){

        
        
        System.out.println("\t\t\t\t欢迎使用我行我素购物管理系统>真情回馈\n\n");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        System.out.println("\t\t\t\t\t1. 幸运大放送\n\n");
        System.out.println("\t\t\t\t\t2. 幸运抽奖\n\n");
        System.out.println("\t\t\t\t\t3. 生日问候\n\n");

        System.out.println("请输入数字:\n\n");
        System.out.println("请选择,输入数字或按0返回上一级菜单\n\n");
        Scanner input =new Scanner (System.in);
        int num=input.nextInt();
        switch(num){
            case 0:
                System.out.println("执行返回上一级菜单\n\n");
                System.out.println("程序结束");
            case 1:

                System.out.println("执行幸运大放送\n\n");
                System.out.println("程序结束");
                break;
            case 2:
   
                System.out.println("执行幸运抽奖\n\n");


                    System.out.println("请输入4位会员号:\n");

                    int custNo=input.nextInt();
                    int qianwei=custNo/1000;
                    int baiwei=custNo%1000/100;
                    int shiwei=custNo%100/10;
                    int gewei=custNo%10;
                    int total;
                    total=qianwei+baiwei+shiwei+gewei;
                    if (total<=20){
                        System.out.println(custNo+"\t\t谢谢您的支持\n\n");
                        }else {
                        System.out.println(custNo+"\t\t是幸运客户,获精美MP3一个。\n\n");
                        }

                    String answer ="y";
                    System.out.println("\n是否继续(y/n)");
                    answer =input.next();
                    do{
                            if (total<=20){
                                System.out.println(custNo+"\t\t谢谢您的支持\n\n");
                            }else {
                                System.out.println(custNo+"\t\t是幸运客户,获精美MP3一个。\n\n");
                            }
                        

                    }while(answer.equals("y"));
                    System.out.println("程序结束");
                break;

            

                    

            case 3:
            
                System.out.println("执行生日问候\n\n");
                System.out.println("程序结束");
                break;

            default:
            
                System.out.println("输入错误。\n\n");
                System.out.println("程序结束");
                break;
               
        }

    }
}

各位大虾,我每次运行程序到:while(answer.equals("y"));按Y都会陷入死循环,请问:如何结束它并输入下一个?????(按N会循环一次)


谢谢!!

搜索更多相关主题的帖子: 结束 结构 SWITCH 
2009-10-06 22:34
jack11430
Rank: 2
等 级:论坛游民
帖 子:16
专家分:17
注 册:2009-10-6
收藏
得分:0 
忘了说运行环境了,在J2EE上面。
呵呵,我刚学的。
2009-10-06 22:35
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:5 
只要别人输入y了,你的循环就开始了,然而你在循环里却没有改变y值,当然会死循环

[url=http://shop63425653./]/cvbnm/a6/1d/f4/7dd1720119cf3b1dcfb570b467b24051.jpg" border="0" />[/url]
2009-10-07 17:32
jack11430
Rank: 2
等 级:论坛游民
帖 子:16
专家分:17
注 册:2009-10-6
收藏
得分:0 
是不是可以用CONTINUE跳出循环?
2009-10-08 22:56
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:5 
CONTINUE的作用可以理解成起到个过滤的作用。

C#超级群 74862681,欢迎大家的到来!
2009-10-08 23:50
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
你的do放错位置咯。
do{
    //你想要的循环体        

}while(条件);

C#超级群 74862681,欢迎大家的到来!
2009-10-08 23:56
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 

    String answer = "y";
        while (answer.equals("y")) {
            System.out.println("请输入您的会员号: ");
            int vip = input.nextInt();
            int gewei = vip % 10;
            int shiwei = vip / 10 % 10;
            int baiwei = vip / 100 % 10;
            int qianwei = vip / 1000;
            int sum = gewei + shiwei + baiwei + qianwei;
            if (sum > 20) {
                System.out.println("恭喜您中奖了。请去领奖,精美MP3一个哦。\n\n");
 
            } else {
                System.out.println("感谢您的支持!欢迎下次再来!\n\n");
 
            }
            System.out.println("是否继续(y/n)");
            answer = input.next();
        }
      

C#超级群 74862681,欢迎大家的到来!
2009-10-08 23:57
包涵
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-1-25
收藏
得分:0 
import java.util.Scanner;
 
public class A{
     public static void main (String[] args){
 
        
         
         System.out.println("\t\t\t\t欢迎使用我行我素购物管理系统>真情回馈\n\n");
         System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
         System.out.println("\t\t\t\t\t1. 幸运大放送\n\n");
         System.out.println("\t\t\t\t\t2. 幸运抽奖\n\n");
         System.out.println("\t\t\t\t\t3. 生日问候\n\n");
 
        System.out.println("请输入数字:\n\n");
         System.out.println("请选择,输入数字或按0返回上一级菜单\n\n");
         Scanner input =new Scanner (System.in);
         int num=input.nextInt();
         switch(num){
             case 0:
                 System.out.println("执行返回上一级菜单\n\n");
                 System.out.println("程序结束");
                 break;
             case 1:
 
                System.out.println("执行幸运大放送\n\n");
                 System.out.println("程序结束");
                 break;
             case 2:
                 String answer ="y";
                 
                System.out.println("执行幸运抽奖\n\n");
                   do{

                    System.out.println("请输入4位会员号:\n");
                    int custNo = input.nextInt();

                  
                  
                     int qianwei=custNo/1000;
                     int baiwei=custNo%1000/100;
                     int shiwei=custNo%100/10;
                     int gewei=custNo%10;
                     int total;
                     total=qianwei+baiwei+shiwei+gewei;
               if (total<=20){
                         System.out.println(custNo+"\t\t谢谢您的支持\n\n");
                         }else {
                         System.out.println(custNo+"\t\t是幸运客户,获精美MP3一个。\n\n");
                        }
                  
                  
                  
 
                    }while(answer.equals("y"));
                     System.out.println("\n是否继续(y/n)");
                     answer =input.next();
                     System.out.println("程序结束");
                     
                 break;
 
            
               
 
            case 3:
            
                 System.out.println("执行生日问候\n\n");
                 System.out.println("程序结束");
                 break;
 
            default:
            
                 System.out.println("输入错误。\n\n");
                 System.out.println("程序结束");
                 break;
                 
        }
 
    }
 }
2012-01-25 15:47
快速回复:求助,如何结束SWITCH结构下的DO-WHILE循环
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019409 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved