Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 914|回复: 2
打印 上一主题 下一主题

帮忙!!!

[复制链接]

51

主题

0

好友

1336

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

跳转到指定楼层
1#
发表于 2009-8-23 10:37 PM |只看该作者 |倒序浏览
public class Ques3_1{

        private String s;
        private static int vowel;
        private static int consonant;

        Ques3_1(){
        }

        Ques3_1(String s){
                this.s=s;

                String change=s.toLowerCase();

                int[] list = new int[s.length() - 1];
               int counter = 1;

               for(int i = 0; i < s.length()-1; i++){
                        for(int j = i + 1; j < s.length(); j++){
                        if(s.charAt(i) == s.charAt(j)){
                                        counter = counter + 1;
                        }
                    }
                    list = counter;
                    if (s.charAt(i) != ' ')//{
                                //System.out.println(s.charAt(i) + " = " + list);
                    //}
                    counter = 1;
                }
        }

        public String getS(){
                return s;
        }

        public void setS(String s){
                this.s=s;
        }

        public int getCountVowel(){
                int vowel = 0;
                String change=s.toLowerCase();
                for(int i = 0; i < change.length(); i++ ){
                        if((change.charAt(i) =='a' ) || (change.charAt(i) =='e' ) || (change.charAt(i) =='i' ) ||
                        (change.charAt(i) =='o' ) || (change.charAt(i) =='u' && change.charAt(i) >=97 && change.charAt(i)<=122)){
              vowel++;
                    }
                }
                return vowel++;
        }

        public int getCountConsonant(){
                int vowel = 0;
                int consonant =0;
                String change=s.toLowerCase();
                for(int i = 0; i < change.length(); i++ ){
                        if((change.charAt(i) =='a' ) || (change.charAt(i) =='e' ) || (change.charAt(i) =='i' ) ||
                        (change.charAt(i) =='o' ) || (change.charAt(i) =='u' && change.charAt(i) >=97 && change.charAt(i)<=122)){
                                vowel++;
                    }
                    else if(change.charAt(i) !=' ' && change.charAt(i) >=97 && change.charAt(i)<=122){
                        consonant++;
                  }
                }
                  return consonant++;
         }

         public int getCountVowelConsonant(){
                int vowel = 0;
                int consonant =0;
                String change=s.toLowerCase();
                for(int i = 0; i < change.length(); i++ ){
                        if((change.charAt(i) =='a' ) || (change.charAt(i) =='e' ) || (change.charAt(i) =='i' ) ||
                        (change.charAt(i) =='o' ) || (change.charAt(i) =='u' && change.charAt(i) >=97 && change.charAt(i)<=122)){
                                vowel++;
                        }
                        else if(change.charAt(i) !=' ' && change.charAt(i) >=97 && change.charAt(i)<=122){
                                consonant++;
                    }
                }
                 return (int)vowel+consonant;
        }
}











//wong sit keng (147717)

import java.util.Scanner;

public class testQues3{

        public static void main(String[]args){

                Scanner input=new Scanner(System.in);
                System.out.print("Please enter a string: ");
                String str=input.nextLine();
                Ques3_1 s= new Ques3_1(str);


                do{
                System.out.println("1) Count the number of vowels\n2) Count the number of consonants\n3) Count both vowels and consonants\n4) Enter another string\n5) Exit the program");
                System.out.print("Your choose is: ");
                int press=input.nextInt();
                if(press==1)
                {
                        //s. getCountVowel();
                        System.out.println("The number of vowel is "+s.getCountVowel());
                        System.out.println("");
                }

                else if(press==2)
                {
                        //s.getCountConsonant();
                        System.out.println("The number of consonant is "+s.getCountConsonant());
                        System.out.println("");
                }

                else if(press==3)
                {
                        s.getCountVowelConsonant();
                        System.out.println("The number of both vowel and consonant is "+s.getCountVowelConsonant());
                        System.out.println("");
                }

                else if(press==4)
                {
                        System.out.print("Please enter another string: ");
                         str=input.nextLine();

                }

                else if(press==5)
                {
                        System.out.println("Exit!");
                        System.exit(0);

                }

                else
                {
                        System.out.println("No such command");
                        System.out.println("");
                }
        }while(str!="null");

        }
}






***为么我不能 4.ENTER ANOTHER STRING




收藏收藏0

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

2#
发表于 2009-8-23 11:30 PM |只看该作者
原帖由 lmy5360 于 2009-8-23 10:37 PM 发表
                else if(press==4)
                {
                        System.out.print("Please enter another string: ");
                         str=input.nextLine();

                }
...



抱歉, 今天很累沒甚麼精神看樓主的 coding, 但樓主是指這裡無法進行輸入嗎??
如果是的話那麼就是你的 Scanner 問題, 只要renew Scanner 即可, 如

input=new Scanner(System.in);
                        System.out.print("Please enter another string: ");
                        str=input.nextLine();


回复

使用道具 举报

51

主题

0

好友

1336

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

3#
发表于 2009-8-24 12:53 PM |只看该作者
谢谢!!!!supertomato


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-27 02:31 PM , Processed in 0.089509 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部