gpt4 book ai didi

java - 序数指示符的后缀

转载 作者:行者123 更新时间:2023-11-30 07:06:32 27 4
gpt4 key购买 nike

对于提供数字后缀有什么建议吗?

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

示例

输入一个整数(1-46):6斐波那契数列中的第 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 的方法> 例如,如果变量 number 的最后一位数字为 3,则在 3 后面应用“rd”。

*****已更新*****我似乎遇到的最后一个问题是异常编号:11、12 和 13。

如何确保 11、12 和 13 在下面的 if 语句中被忽略:

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.");

我以为我正在以正确的方式处理这件事。是我把括号弄乱了吗?我刚刚尝试了不同的组合但没有成功。

最佳答案

这是我想到的:

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.");

关于java - 序数指示符的后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40031880/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com