在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 dig
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/06 06:54:25
在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 dig
在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);
}
}
}
最好给我全部的代码,谢谢了
在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 dig
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);
}
}
}
已经运行过了,没问题