小白基础问题求解
public class Test{
public static void main(String[] args){
java.util.Scanner s = new java.util.Scanner(System.in);
System.out.print("请输入您的年龄: ");
int age = s.nextInt();
String str = "老年人";
if(age < 0 || age > 150);{
str = "您提供的年龄不合法,年龄值需要在之间";}
else if(age <= 5);{
str = "幼儿";}
else if(age <= 10);{
str = "少年";}
else if(age <= 18);{
str = "青少年";}
else if(age <= 35);{
str = "青年";}
else if(age <= 55);{
str = "中年";}
System.out.println(str);
}
}
--------------------
这段编程为啥我一直显示 错误:有'if',但是没有'else'
我需要怎么改,求解求解{:10_269:}{:10_269:}
public class Test {
public static void main(String[] args) {
java.util.Scanner s = new java.util.Scanner(System.in);
System.out.print("请输入您的年龄: ");
int age = s.nextInt();
String str = "老年人";
if(age < 0 || age > 150) {
str = "您提供的年龄不合法,年龄值需要在之间";
} else if(age <= 5) {
str = "幼儿";
} else if(age <= 10) {
str = "少年";
} else if(age <= 18) {
str = "青少年";
} else if(age <= 35) {
str = "青年";
} else if(age <= 55) {
str = "中年";
}
System.out.println(str);
}
} 把if();else if(); 这些小括号后面的分号去掉 baige 发表于 2020-11-15 20:23
把if();else if(); 这些小括号后面的分号去掉
谢谢大佬{:10_256:} baige 发表于 2020-11-15 20:23
把if();else if(); 这些小括号后面的分号去掉
回答简单明了.. baige 发表于 2020-11-15 20:23
代码清晰易懂... 本帖最后由 89684794@qq.com 于 2021-2-6 12:06 编辑
public static void main(String[] args) {
java.util.Scanner s = new java.util.Scanner(System.in);
System.out.print("请输入您的年龄: ");
int age = s.nextInt();
String str = "老年人";
if (age < 0 || age > 150) {
str = "您提供的年龄不合法,年龄值需要在之间";
} else {
if (age <= 5) {
str = "幼儿";
} else {
if (age <= 10) {
str = "少年";
} else {
if (age <= 18) {
str = "青少年";
} else {
if (age <= 35) {
str = "青年";
} else {
if (age <= 55) {
str = "中年";
}
}
}
}
}
}
System.out.println(str);
}
页:
[1]