序数指示符的后缀

时间:2016-10-13 22:11:23

标签: java loops if-statement fibonacci

有关为数字提供后缀的任何建议吗?

我正在为我的代码提供以下输出:

实施例

输入一个整数(1-46):6 Fibonacci序列中的第6个数字是:8

以下是我目前所完成的内容:

import java.util.*;

public class Somethingpart2 {

    public static void main(String[] args) {

        Scanner kbd = new Scanner(System.in);

        //Variable Declaration 
        int number;
        long Fibnumber;


        Boolean accepted, limit;

        //Beginning of user input for the Fibonacci sequence 
        System.out.print("Enter an integer (1-46): ");
        number = kbd.nextInt();

        Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5)); 
        accepted = number >= 1 && number <= 46;
        limit = number == Fibnumber;


        if (accepted) {
            do {
                System.out.println("The " + number +" number in the Fibonacci sequence is: "+Fibnumber);
                //if ())
                return;
            }
            while (limit);
        }
        else
            System.out.println("Not a valid number.");

我正在考虑//if ()所在的位置,我可以想出一种方法来使用.containsindexOf来帮助,例如,如果变量号包含3作为最后一位数字,然后应用&#34; rd&#34;就在3之后。

******* ******更新 我似乎遇到的最后一个问题是例外数字:11,12和13。

如何确保在以下if语句中忽略11,12和13:

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);

    //Variable Declaration 
    int number;
    long Fibnumber;


    Boolean accepted, limit;

    //Beginning of user input for the Fibonacci sequence 
    System.out.print("Enter an integer (1-46): ");
    number = kbd.nextInt();

    Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5)); 
    accepted = number >= 1 && number <= 46;
    limit = number == Fibnumber;


    if (accepted) {
        do {
            //System.out.println("The " + number +" number in the Fibonacci sequence is: "+Fibnumber);
            if (number % 10 == 3 && number % 10 !=13)
                System.out.println("The "+ number+"rd number in the Fibonacci sequence is: "+ Fibnumber);
            else
                if (number % 10 == 2 && number % 10 != 12)
                    System.out.println("The "+ number+ "nd number in the Fibonacci sequence is: " +Fibnumber);
                else
                    if (number % 10 == 1 && number % 10 != 11)
                        System.out.println("The "+ number+ "st number in the Fibonacci sequence is: " +Fibnumber);
                    else
                        System.out.println("The "+ number+ "th number in the Fibonacci sequence is: " +Fibnumber);
            return;
        }
        while (limit);
    }
    else
        System.out.println("Not a valid number.");

我以为我是以正确的方式去做的。这是我弄乱的括号吗?我刚试过不同的组合但没有成功。

2 个答案:

答案 0 :(得分:1)

这就是我提出的:

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);

    //Variable Declaration 
    int number;
    long Fibnumber;


    Boolean accepted, limit;

    //Beginning of user input for the Fibonacci sequence 
    System.out.print("Enter an integer (1-46): ");
    number = kbd.nextInt();

    System.out.println("");//Provides a space between the two print out statements within the program. 

    Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5)); 
    accepted = number >= 1 && number <= 46;
    limit = number == Fibnumber;


    if (accepted) {
        do {
            if (number % 10 == 3 && number !=13)
                System.out.println("The "+ number+"rd number in the Fibonacci sequence is: "+ Fibnumber);
            else
                if (number % 10 == 2 && number != 12)
                    System.out.println("The "+ number+ "nd number in the Fibonacci sequence is: " +Fibnumber);
                else
                    if (number % 10 == 1 && number != 11)
                        System.out.println("The "+ number+ "st number in the Fibonacci sequence is: " +Fibnumber);
                    else
                        System.out.println("The "+ number+ "th number in the Fibonacci sequence is: " +Fibnumber);
            return;
        }
        while (limit);
    }
    else
        System.out.println("Not a valid number."); 

答案 1 :(得分:0)

String getSuffix(long num) {
   long lastDigit = num % 10;

   switch (lastDigit) {
      case 0:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
         return "th";

      case 1:
         return "st";

      case 2:
         return "nd";

      case 3:
         return "rd";

      default:
         assert false : "Shouldn't get here";
         return "";
   }
}