在JAVA,输入一个正整数,求此正整数的中间数(比如12345的中间数是3,1234则没有中间数)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在JAVA,输入一个正整数,求此正整数的中间数(比如12345的中间数是3,1234则没有中间数)相关的知识,希望对你有一定的参考价值。

题目是:The middle digit of an integer number (Middle, for short). For a positive number read from the standard input extract the digit in the middle and display it, if this exists; otherwise display “no middle digit”. For 145, the middle digit is 4, whereas in the case of 1324, there is no middle digit.
我自己编写了个程序,可以编译,可是一运行就出现错误,请帮我修改看看那里错了
//Display Enter an integer number in a message dialog box
String Integernumber = JOptionPane.showInputDialog("Enter an integer number:");
//Calculate how many figure in this integer number
String str1 = String.valueOf(Integernumber);
int str = Integer.parseInt(str1);
//determine the length of number isn't even.
int digit = str % 2;
if (digit == 0)
String output = "There is no middle digit in "+Integernumber;
JOptionPane.showMessageDialog(null, output);

else
double n = str / 2;
double n1 = n - 0.5;
double n2 = n + 0.5;
int n11 = (int)n1;
int n22 = (int)n2;
String middle = Integernumber.substring(n11,n22);
String output = "The middle digit in "+Integernumber+" is "+middle;
JOptionPane.showMessageDialog(null, output);



最好给我全部的代码,谢谢了

参考技术A 首先需要判断的输入的整数是否符合需求,然后再逐步判断,这也是工作中必不可少的步骤,我写的一个简单的如下:
public class Test3

public static void main(String[] args)
//Display Enter an integer number in a message dialog box
String Integernumber = JOptionPane.showInputDialog("Enter an integer number:");
//Calculate how many figure in this integer number
String str1 = String.valueOf(Integernumber);
//判断是否输入正整数
if(str1.length()==0||str1.indexOf("-")>=0)
JOptionPane.showMessageDialog(null, "请输入一个正整数");
return;

//判断是否存在中间的正整数
if(str1.length()%2 == 0)
JOptionPane.showMessageDialog(null, "不存在中间的整数");
return;

//输入一位整数的情况
if(str1.length() == 1)
JOptionPane.showMessageDialog(null, "请输入大于一位的正整数");
return;

//符合需求的情况
if(str1.length()%2 != 0)
String middleNum = str1.substring(str1.length()/2, str1.length()/2+1);
JOptionPane.showMessageDialog(null, str1+"的中间的整数为:"+middleNum);
return;



参考技术B int str = Integer.parseInt(str1);
这句代码把字符串转成了数字,也就是说你后面对str的操作都是基于整形数字的。而你想输出中间数字的话,应该对字符串操作。

所以 String Integernumber = JOptionPane.showInputDialog("Enter an integer number:");
后应该是int len = integernumber.length();
if(len %2 == 0)
//no mid value;

else System.out.println("Mid value is "+integernumber.charat (len /2 +1 ));
参考技术C import javax.swing.JOptionPane;
public class Test
public static void main(String[] args)
String Integernumber = JOptionPane.showInputDialog("Enter an integer number:");
int len = Integernumber.length();
if(len % 2 == 0)
String output = "There is no middle digit in "+Integernumber;
JOptionPane.showMessageDialog(null, output);
else
char middle = Integernumber.charAt(len/2);
String output = "The middle digit in "+Integernumber+" is "+middle;
JOptionPane.showMessageDialog(null, output);



已经运行过了,没问题本回答被提问者采纳
参考技术D //determine the length of number isn't even.
int digit = str % 2;
这个能确定这个数的位数是偶数还是技术吗?不行吧!比如21,他不能被2整除,但是,它却是偶数位。所以,逻辑错误。
1.将int digit=str%2改为,int digit=integernumber.length()%2;
2.else模块建议采用Ncross的代码;
第5个回答  2010-10-27 crystal_186 回答是对的
但是楼主的题目到底是中间位置的数 还是中间的那个平均数?
比如 13245
楼主的意思是找到中间位置的数 2
还是找到 中间的平均数 3 ?

crystal_186 回答是中间位置的数。

Python分两行输入两个正整数输出两个数的和差怎么写?

a, b = map(int, input().split())
print("两个数的和为:", a + b)
print("两个数的差为:", a - b)
该代码将两个数从一行中输入,并使用 map() 函数和 split() 方法将它们分为两个整数。然后,代码计算和差并输出。
参考技术A 可以使用input()函数来分别读取两个正整数的输入,然后计算它们的和与差,最后使用print()函数将结果输出。以下是示例代码:
# 读取第一个正整数
num1 = int(input("请输入第一个正整数:"))
# 读取第二个正整数
num2 = int(input("请输入第二个正整数:"))
# 计算和与差
sum = num1 + num2
diff = num1 - num2
# 输出结果
print("两数之和为:", sum)
print("两数之差为:", diff)
在这个示例代码中,我们使用了int()函数将输入的字符串转换为整数类型。如果输入的字符串不能转换为整数,将会引发ValueError异常。
参考技术B 可以使用以下代码实现:
a, b = input().split()
a = int(a)
b = int(b)
print("两个数的和:", a + b)
print("两个数的差:", a - b)
在这段代码中,首先使用 input() 函数读取两个数字,使用 split() 方法将读入的字符串拆分为两个数字,然后使用 int() 函数将其转换为整数。最后,使用 print() 函数输出两个数的和和差。
参考技术C 下面是 Python 实现两个正整数输入,并输出它们的和与差的代码:
a, b = map(int, input().split())
print(a + b, a - b)
上面的代码先输入两个正整数,然后使用 map 函数将它们转换为整数,并通过 split 方法分开存储在 a 和 b 中。最后,使用 print 输出两个数的和与差。
参考技术D 在下面的代码中,我们使用 input 函数分别获取两个正整数,然后计算它们的和与差,最后打印结果
# Get two positive integers
num1 = int(input("Enter the first positive integer: "))
num2 = int(input("Enter the second positive integer: "))
# Calculate the sum and difference
sum = num1 + num2
diff = num1 - num2
# Print the result
print("The sum of", num1, "and", num2, "is", sum)
print("The difference between", num1, "and", num2, "is", diff)

以上是关于在JAVA,输入一个正整数,求此正整数的中间数(比如12345的中间数是3,1234则没有中间数)的主要内容,如果未能解决你的问题,请参考以下文章

ccf——201612-1中间数

CCF——中间数(2016-12)

java程序在控制台输入一个正整数,要求以二进制的形式输出

c++编程)键盘输入一个高精度的正整数M,去掉其中任意S个数字后使剩下的数最小

java 逆序输出整数

Java中,给一个整数数组,找出其中所有的正整数,并求所有正整数的和