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

请各位java高手帮帮忙~

[复制链接]

3

主题

0

好友

893

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

跳转到指定楼层
1#
发表于 2010-6-9 01:53 PM |只看该作者 |倒序浏览
请各位JAVA高手帮帮忙~
我做到 public static int getDigit(int even)到了第二组我就不会了~
小弟先在这里谢谢高手的帮忙

Credit card numbers follwoing cretain pattern. A credit card number must have between 13 and 16 digits. It must start with:
             4 for Visa cards
             5 for Master cards
             37 for American Express cards
             6 for Discover cards
             In 1954, Hans Lubn of IBM proposed an algorithm for validating credit card numbers.  The algorithm is useful to determine if a card number
             is entered correctly or if a credit card is scanned correctly by a scanner.  Almost all credit card numbers are generated follwoing this
             validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustraiton, consider the card
             numebr 4388576018402625):
            
            1) Double every second digit form right to left.  It doubling of a digit results in a two-digit number, add up the two digits to get a
             single-digit number.
               2 * 2 = 4
               2 * 2 = 4
               4 * 2 = 8
               1 * 2 = 2
               6 * 2 = 12(1 + 2 = 3)
               5 * 2 = 10(1 + 0 = 1)
               8 * 2 = 16(1 + 6 = 7)
               4 * 2 = 8
           2) Now add all single-digit numbers from Step 1.   
               4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
           3) Add all digits int the odd places from right toleft in the card number.  
               5 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 37
           4) Sum the results from Step 2 and Step 3.
               37 + 37 = 74
           5)If the result from Step 4 is divisible by 10, the card number is valid: otherwise, it is invalid.  For example 4388576018402625 is invalid,
            but the number 4388576018410707 is valid.
            Write a programt hat prompts the user to enter a credit card number as a long integer. Display whether the number is valid.
            Design your program to use the following methods:

            /** Return true if the card number is valid */
                //public boolean isValid(long number)

           /** Get the result from Step 2*/
               //public static int sumOfEvenPlace(long number)

           /** Return this number if it is a single digit, otherwise,
               return the sum of the two digits */
              //public static int getDigit(int Number)

          /** Return sum of odd place digits in number */
             //public static int sumOfOddPlace(long number)

import java.util.*;
public class qqq
{

    public static void main(String[] args)
    {
            String cardno;
            int even;
            int odd;
            Scanner scan= new Scanner(System.in);
            System.out.println("lease Enter The  Credit Card Number: ");
           
        cardno=scan.nextLine();
      
        System.out.println("\nEven number: ");
            for(int i=(cardno.length()-1);i>=0;i=i-1)
            {
               even=Integer.parseInt(cardno.substring(--i,(i+1)));
               System.out.print(even);
               System.out.println(" is "+getDigit(even));
              
            }
           
            System.out.println("\nOdd number: ");
            for(int j=(cardno.length()-1);j>=0;j=j-2)
            {
                    odd=Integer.parseInt(cardno.substring(j,(j+1)));
                    System.out.print(odd);
            }
   
    }
    public static int getDigit(int even)
    {
            int sum=0;
        even*=2;
        if(even>9)
        {
                do{
                        sum=(int)(sum+even%10);
                        even=even/10;
                  }while(even%10!=0);
        }
        else
                sum=even;
        return sum;
     }
}




收藏收藏0

2

主题

0

好友

2793

积分

白金长老

Rank: 10

2#
发表于 2010-6-9 02:12 PM |只看该作者
可惜我的java也是很差~


回复

使用道具 举报

3

主题

0

好友

893

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

3#
发表于 2010-6-9 02:41 PM |只看该作者
可惜我的java也是很差~
小恋人 发表于 2010-6-9 02:12 PM



  我也是啊~
大多数我都是请教别人教我做的~
哈哈~


回复

使用道具 举报

6

主题

0

好友

397

积分

超级会员

Rank: 5Rank: 5Rank: 5Rank: 5Rank: 5

4#
发表于 2010-6-9 04:39 PM |只看该作者
大概是这样,语法可能错误,也省列了数字与string之间的转换,毕竟对java不熟,做个参考吧:

public static void main(String[] args)
{
....
if isValid(cardno) //验证cardno
{
      //如果是正确就。。。。
}
....
}

//isValid function
public boolean isValid(long number)
{
     valid=false;
     if (number.length()>=13 && number.length()<=16) //必须是13-16位
     {
          int first = number.substring(1,0); //card number的第一个字母
          init second = number.substring(2,0); //card number的首两位数
          if (first==4 || first==5 || first==6 || second==37)
          {
                int resulst = 0;
                result = sumOfEvenPlace(number) + sumOfOddPlace(number); //call sumOfEvenPlace function,sumOfOddPlace function, 把返回的两个数值+起来
                result = result % 10; //module 10
                if (result==0)
                       valid=true;
          }
     }
     return valid;
}

//sumOfEvenPlace function
public static int sumOfEvenPlace(long number)
{
      int even=0;
      for(int i=(number.length()-2);i>=0;i=i-2) // 直接 i=i-2 以达到跳两位数的效果
             even = even + getDigit(number.substring(1,i) * 2); //把substring的结果先 x 2,然后使用getdigit function检测是否是两位数
      return even;
}

//sumOfOddPlace function
public static int sumOfOddPlace(long number)
{
      int odd=0;
      for(int j=(number.length()-1);j>=0;j=j-2) //直接 i=i-2 以达到跳两位数的效果
            odd = even + number.substring(1,j) ;
      return odd;
}

//getDigit function
public static int getDigit(int even)
{
      int len = even.length();
      int result=0;
      if (len>1) //如果2位数
            result=cardno.substring(1,0) + cardno.substring(1,1);  //因为不可能是3位数,所以就直接substring 1 + substring 2
      return result;
}


回复

使用道具 举报

3

主题

0

好友

893

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

5#
发表于 2010-6-9 06:21 PM |只看该作者
大概是这样,语法可能错误,也省列了数字与string之间的转换,毕竟对java不熟,做个参考吧:

public stat ...
weeming21 发表于 2010-6-9 04:39 PM


我试了~
还是有不行啊~
不过也要谢谢你给的solution
  


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

6#
发表于 2010-6-9 11:38 PM |只看该作者
本帖最后由 Super-Tomato 于 2010-6-10 12:02 AM 编辑
请各位JAVA高手帮帮忙~
我做到 public static int getDigit(int even)到了第二组我就不会了~
小弟先在这里谢谢高手的帮忙

Credit card numbers follwoing cretain pattern. A credit card number must have between 13 and 16 digits. It must start with:
             4 for Visa cards
             5 for Master cards
             37 for American Express cards
             6 for Discover cards
             In 1954, Hans Lubn of IBM proposed an algorithm forvalidating credit card numbers.  The algorithm is useful to determineif a card number
             is entered correctly or if a credit card is scannedcorrectly by a scanner.  Almost all credit card numbers are generatedfollwoing this
             validity check, commonly known as the Luhn check or theMod 10 check, which can be described as follows (for illustraiton,consider the card
             numebr 4388576018402625):
            
            1) Double every second digit form right to left.  Itdoubling of a digit results in a two-digit number, add up the twodigits to get a
             single-digit number.
               2 * 2 = 4
               2 * 2 = 4
               4 * 2 = 8
               1 * 2 = 2
               6 * 2 = 12(1 + 2 = 3)
               5 * 2 = 10(1 + 0 = 1)
               8 * 2 = 16(1 + 6 = 7)
               4 * 2 = 8
           2) Now add all single-digit numbers from Step 1.   
               4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
           3) Add all digits int the odd places from right toleft in the card number.  
               5 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 37
           4) Sum the results from Step 2 and Step 3.
               37 + 37 = 74
           5)If the result from Step 4 is divisible by 10, the cardnumber is valid: otherwise, it is invalid.  For example4388576018402625 is invalid,
            but the number 4388576018410707 is valid.
            Write a programt hat prompts the user to enter a creditcard number as a long integer. Display whether the number is valid.
            Design your program to use the following methods:

            /** Return true if the card number is valid */
                //public boolean isValid(long number)

           /** Get the result from Step 2*/
               //public static int sumOfEvenPlace(long number)

           /** Return this number if it is a single digit, otherwise,
               return the sum of the two digits */
              //public static int getDigit(int Number)

          /** Return sum of odd place digits in number */
             //public static int sumOfOddPlace(long number)

                                                                 
~叫我宝哥~ 发表于 2010-6-9 01:53 PM



其實只要了解計算的规则,那麼編寫方面就可以按照自己的逻辑方式来做

目前我這個系统没安裝 Java 所以我只能大略寫一寫我的方式

  1.     public static void main(String[] args)
  2.     {
  3.             String cardno;
  4.             int digit, total = 0;

  5.             Scanner scan= new Scanner(System.in);
  6.             System.out.println(" Please Enter The  Credit Card Number: ");

  7.             cardno=scan.nextLine();

  8.             for(int i=(cardno.length()-1);i>=0;i=i--)
  9.             {
  10.                digit = Integer.parseInt(cardno.charAt(i));
  11.                if(i % 2)
  12.                    total += digit;
  13.                else
  14.                {
  15.                    digit *= 2;
  16.                    total += Math.floor(digit / 10) + (digit % 10);
  17.                }
  18.             }

  19.             if(total % 10)
  20.                 System.out.print("This is invalid Credit Card No.");
  21.             else
  22.                 System.out.print("Valid Credit Card No.");
  23.     }
复制代码


回复

使用道具 举报

46

主题

6

好友

6456

积分

百变名嘴

Rank: 13Rank: 13Rank: 13Rank: 13

7#
发表于 2010-6-10 06:32 AM |只看该作者
其實只要了解計算的规则,那麼編寫方面就可以按照自己的逻辑方式来做

目前我這個系统没安裝 Java  ...
Super-Tomato 发表于 2010-6-9 11:38 PM


厉害。 没Java , 没篇写工具都写的出Java句。 你写过那么多种语言, 不会混乱哦。大小写方面都正确了。


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

8#
发表于 2010-6-10 10:03 AM |只看该作者
厉害。 没Java , 没篇写工具都写的出Java句。 你写过那么多种语言, 不会混乱哦。大小写方面都正确了。 ...
宅男-兜着走 发表于 2010-6-10 06:32 AM



仔细看的話,其實14行开始到20行是自己寫的,而且没甚么特别函数自然要错的机率也比較低


回复

使用道具 举报

46

主题

6

好友

6456

积分

百变名嘴

Rank: 13Rank: 13Rank: 13Rank: 13

9#
发表于 2010-6-10 07:09 PM |只看该作者

  1. import java.util.*;

  2. public class Assignment11 {

  3.     public static void main(String[] args) {
  4.                 Scanner scan = new Scanner(System.in);

  5.                 System.out.println("Please Key In Card No.");
  6.                 String cardNo = scan.next();

  7.                 if(isValid(Long.parseLong(cardNo)))
  8.                         System.out.println("\n\"" + cardNo + "\"" + " is valid card no.");
  9.                 else
  10.                         System.out.println("\n\"" + cardNo + "\"" + " is invalid card no.");
  11.     }

  12.     public static boolean isValid(long number)
  13.     {
  14.     String cardNoStr = Long.toString(number);
  15.         int cardNoLength = cardNoStr.length();

  16.         String firstDigitValidation , secondDigitValidation;

  17.         firstDigitValidation = cardNoStr.substring(0 , 1);
  18.         secondDigitValidation = cardNoStr.substring(0 , 2);

  19.         int result = sumOfEvenPlace(number) + sumOfOddPlace(number);

  20.         return
  21.                 (
  22.                  cardNoLength >= 13 &&
  23.                  cardNoLength <= 16 &&
  24.                  result % 10 == 0 &&
  25.                  firstDigitValidation.equals("4") ||
  26.                  firstDigitValidation.equals("5") ||
  27.                  firstDigitValidation.equals("6") ||
  28.                  secondDigitValidation.equals("37")
  29.                 );
  30.            }

  31.     public static int sumOfEvenPlace(long number)
  32.     {
  33.     String sNumber = Long.toString(number);
  34.     int result = 0;
  35.         for(int i=sNumber.length()-1 ;i>0; i-=2){
  36.         result += getDigit(Integer.parseInt(sNumber.substring(i-1 , i)));
  37.         }
  38.         return result;
  39.     }

  40.     public static int getDigit(int Number)
  41.     {
  42.         String mulNumber = Integer.toString(Number * 2);
  43.         int result = 0;
  44.         if(mulNumber.length() > 1)
  45.                 result = Integer.parseInt(mulNumber.substring(0, 1)) + Integer.parseInt(mulNumber.substring(1, 2));
  46.         else
  47.                 result = Integer.parseInt(mulNumber);
  48.         return result;
  49.     }


  50.     public static int sumOfOddPlace(long number)
  51.     {
  52.         String sNumber = Long.toString(number);
  53.         int result = 0;
  54.         for(int i=sNumber.length() ;i>0; i-=2)
  55.         {
  56.         result+= Integer.parseInt(sNumber.substring(i-1 , i));
  57.         }
  58.         return result;
  59.     }


  60. }
复制代码


回复

使用道具 举报

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

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

GMT+8, 2024-6-16 09:59 PM , Processed in 0.121891 second(s), 27 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.
回顶部