【发布时间】:2019-03-17 05:02:46
【问题描述】:
我是编程新手,刚开始学习 Java。
我想做一个程序是
- 要求用户输入一个包含数字序列的字符串,然后
- 获取该序列的第一个和最后一个数字,并
- 检查这些数字是奇数还是偶数
根据这些信息,它会做某些事情。
这是我的代码:
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String n = kb.nextLine();
Integer x = Integer.valueOf(n.charAt(n.length() - 1));
Integer y = Integer.valueOf(n.charAt(0));
String out;
if (y % 2 == 0 && x % 2 == 0) {
out = "$" + n.substring(1, n.length() - 1) + "$";
} else if (y % 2 > 0 && x % 2 > 0) {
out = "X" + n.substring(1, n.length() - 1) + "X";
} else if (x == 0); {
out = n.substring(0, n.length() - 1) + "#";
}
System.out.println(out);
}
我不确定是什么问题。我认为是关于这两行
Integer x = Integer.valueOf(n.charAt(n.length()-1));
Integer y = Integer.valueOf(n.charAt(0));
输出值与输入值不同..
【问题讨论】:
-
这个答案对您有帮助吗?
-
绝对的,非常感谢你!虽然很抱歉回复晚了..
标签: java string char numeric value-of